使用springaop报错如下:
rg.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3aa9e816: startup date [Mon Nov 05 17:43:13 CST 2018]; root of context hierarchy
十一月 05, 2018 5:43:13 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'userDao' is expected to be of type 'com.xyd.aop.demo02.UserDaoImpl' but was actually of type 'com.sun.proxy.$Proxy7'
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:384)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:205)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1091)
at com.xyd.aop.demo02.TestAop.demo01(TestAop.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
xml配置如下:
<!-- 配置目标对象 -->
<bean id="userDao" class="com.xx.aop.demo02.UserDaoImpl"></bean>
<!-- 配置通知对象 -->
<bean id="myAdvice" class="com.xx.aop.demo02.MyAdvice"></bean>
<!--配置 将 通知 织入目标对象 -->
<aop:config>
<!-- 配置切点 -->
<aop:pointcut id="pc" expression="execution(* com.xx.aop.demo02.UserDaoImpl.*(..))" />
<aop:aspect ref="myAdvice">
<!-- 配置前置前置通知 -->
<aop:after method="before" pointcut-ref="pc"/>
<!-- 配置通知的方法 配置的切点 对象-->
<aop:after method="after" pointcut-ref="pc"></aop:after>
</aop:aspect>
</aop:config>
测试代码如下:
@Test
public void demo01(){
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = app.getBean("userDao", UserDaoImpl.class);
userDao.insert();
}
解决方法1:
配置文件添加 下面配置 使用cglib 的aop实现
<aop:aspectj-autoproxy proxy-target-class="true"/>
解决方法2:
修改获取的对应的class
UserDao userDao = app.getBean("userDao", UserDao.class);
错误原因: 找不对应的接口 UserDao