1

我正在尝试将 AOP 用于现有的(大)Spring 项目。问题是我不希望 Spring 为 ApplicationContext 中的所有对象创建代理,主要是为了性能,但也因为存在我无法修改的最终类。

我试图通过定义以下方面来仅在“com.foo.bar.*”内进行 Spring 搜索:

com.baz.MyAspect

@Aspect
public class MyAspect {

    private static final Logger LOGGER = LoggerFactory.getLogger(MyAspect.class);

    @Before("within(com.foo.bar.*) && " +
        "execution(* com.foo.bar.MyController.handleRequest(..))")
    public void getData() {
        // Nothing yet
    }
}

我已将这些行添加到配置中:

<?xml version="1.0" encoding="utf-8"?>
<beans ...>
    <aop:aspectj-autoproxy proxy-target-class="true" />
    <bean id="myAspect" class="com.baz.MyAspect"/>
</beans>

但是当我运行应用程序时,我得到以下异常:

Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.foobar.FinalController]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class com.foobar.FinalController

因此,似乎 Spring 正在扫描除 within 表达式中定义的包之外的包。我想知道是否有办法指定要扫描的包或任何其他方法来解决这个问题。

4

1 回答 1

0

是的,你可以像这样定义你的切入点

执行 bar 包或子包中定义的任何方法:

执行(* com.foo.bar.. ( ..))点击这里查看详情

于 2013-08-27T11:38:38.397 回答