最近项目中遇到了如下需求:对部分函数的调用进行日志记录,这些需要记录日志的函数分散在各个包中,如果手动添加,工作量很大,也不灵活,故决定采用AOP来解决。
 
在配置aop后,原来的ioc注入却出现了问题。
 
xml文件:
 
<bean id="testLog" class="com.mainbo.u3.common.web.MyLog"></bean> <!--将日志类注入到bean中。-->
      <aop:config >
          <aop:aspect id="b" ref="testLog"><!--调用日志类-->
           <aop:pointcut id="log" expression="execution(* com.mainbo.u3..*.*(..))"/>
           <aop:before pointcut-ref="log" method="before"/>
           <aop:after pointcut-ref="log" method="after"/>
          </aop:aspect>
       </aop:config>
 
execution(* com.mainbo.u3..*.*(..)表达式匹配u3包下面所有子包的所有类的所有方法。
 
具体问题是:
 
1.将表达式改为:execution(* com.mainbo.u3..*Controller.*(..),项目能正常启动;
 
2.将表达式改为:execution(* com.mainbo.u3..*Service.*(..),项目启动报错。错误如下:
 
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'accountExportService' must be of type [com.mainbo.u3.manage.priviledge.AccountExportService], but was actually of type [$Proxy24]
  at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:306)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
  at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
  at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
  at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
  at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
  at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:384)
  at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
  at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
  at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4791)
  at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285)
  at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
 
3.将表达式改为:execution(* com.mainbo.u3..*.*(..),项目启动报错。错误同上。
 
 
 
查了网上关于aop配置导致ioc注入失败的相关文章,碰到类似问题的朋友也很多,似乎没有一个合理的解决方案。
 
后来看有网友说<aop:config>里加 proxy-target-class="true",增加以后,问题2得到解决,问题3仍然存在。
 
跪求遇见类似情况得到解决的大侠?给个合理的解释吧!