扫描Spring AOP的特定包
我正在尝试将AOP应用到现有的(大型)Spring项目中。问题是,我不希望Spring为ApplicationContext中的所有对象创建代理,这主要是为了性能,但也因为还有一些我无法修改的最终类 通过定义以下方面,我尝试仅在“com.foo.bar.*”中进行Spring搜索:扫描Spring AOP的特定包,spring,spring-aop,Spring,Spring Aop,我正在尝试将AOP应用到现有的(大型)Spring项目中。问题是,我不希望Spring为ApplicationContext中的所有对象创建代理,这主要是为了性能,但也因为还有一些我无法修改的最终类 通过定义以下方面,我尝试仅在“com.foo.bar.*”中进行Spring搜索: com.baz.MyAspect @Aspect public class MyAspect { private static final Logger LOGGER = LoggerFactory.ge
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>
因此,Spring似乎正在扫描的包不是在表达式中定义的包。我想知道是否有一种方法可以指定要扫描的包,或者有任何其他方法可以解决此问题。是的,您可以这样定义切点 执行bar包或子包中定义的任何方法:
execution(*com.foo.bar…(…))不确定我是否完全理解了这个问题,Spring不会为所有内容创建代理,只为与切入点匹配的bean创建代理,因此您可以使用包前缀声明切入点,并且只有那些bean应该得到代理。谢谢您的回答。我正在定义切入点,但是Spring正在尝试在其他包中创建类的代理。我已经更新了问题的更多细节。根据异常,您正在尝试为最终类创建代理对象。为什么你要宣布一个控制员是最终的?
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