我試圖使用AOP到現有的(大)春天項目。問題是我不希望Spring爲ApplicationContext中的所有對象創建代理,主要是爲了性能,還因爲有無法修改的最終類。掃描特定的軟件包春天AOP
我試圖讓春天的搜索裏面只有通過定義以下方面「com.foo.bar。*」:
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並沒有爲所有事情創建代理,只爲與切入點匹配的bean創建代理,因此您可以使用包前綴聲明切入點,並且只有這些bean應該代理。 –