使用了AOP 后啟動報錯
九月 27, 2016 2:29:46 下午 org.springframework.context.support.AbstractApplicationContext refresh 警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'acAction': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountService' defined in file [D:\workspace_eclipse_mars\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\bpmp\WEB-INF\classes\com\bkc\bpmp\xdj\zc\service\impl\AccountServiceImpl.class]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut 九月 27, 2016 2:29:46 下午 org.springframework.web.servlet.FrameworkServlet initServletBean 嚴重: Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'acAction': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountService' defined in file [D:\workspace_eclipse_mars\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\bpmp\WEB-INF\classes\com\bkc\bpmp\xdj\zc\service\impl\AccountServiceImpl.class]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:308) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1208) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
在使用聲明方式的AOP編程中,遇到以下問題,解決方法如下:
(1)error at ::0 formal unbound in pointcut
解決方法:去掉函數通知函數中的參數,比如:將
@Before("execution(public void com.bjsxt.dao.impl.UserDAOImpl.*(..))")
public void beforeMethod(Method method){
System.out.println("method before");
}
改為
@Before("execution(public void com.bjsxt.dao.impl.UserDAOImpl.*(..))")
public void beforeMethod(){
System.out.println("method before");
}
情況二:
原寫法:
@Before("@annotation(org.haha.web.annotation.BrowAuth)
public void beforeExec(HttpServletRequest request) {
......
}
會報以下錯誤:
0 formal unbound in pointcut
原因是應該用args指明參數,改成以下就可以了:
@Before("@annotation(org.haha.web.annotation.BrowAuth) && (args(request,..) || args(..,request))")
public void beforeExec(HttpServletRequest request) {
......
}
方法中未帶參數,本bug 非此原因
(2)可能原因(我的猜測,未確認)
使用了AOP 之后(spring),實現注解是采用代理的方式,而代理有兩種jdk自帶代理和 cglib,而在springmvc 中直接使用自動注解的時候,沒有使用這一層代理。接口、接口的實現類,其注解的命名方式不一致,造成了自動注入的時候,代理不知道該選擇哪一個類……
可解決方法:一、使用AOP注解,將原有的業務上的接口、接口實現類的命名改統一。假設接口為 IUserDao,實現類為 UserDaoImpl ,自動注入的時候寫成 IUserDao userDaoImpl (原因:不清楚)
方法二、不要使用AOP注解,在xml 中配置需要的AOP 方式,如下:自定義LoggingInterceptor 中有個around 方法
<bean id="loggingInterceptor" class="com.bkc.core.aspectj.LoggingInterceptor" />
<aop:config> <aop:aspect id="loggingAspect" ref="loggingInterceptor"> <aop:pointcut id="loggingIn" expression="execution(* com.bkc.oa.controller..*.*(..))" /> <!-- <aop:before method="before" pointcut-ref="loggingIn"/> <aop:after method="after" pointcut-ref="loggingIn"/> --> <aop:around method="around" pointcut-ref="loggingIn"/> </aop:aspect> </aop:config>