spring2.5.6实现注解AOP出现奇怪错误
代码如下:
拦截类
package com.spring.aop.service;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;@Aspectpublic class InterCeptor { @Pointcut("execution (public void com.spring.aop.service.impl.PersonServiceBeanImpl.*(..))") private void anyMethod(){}//声明一个切入点 @Before("anyMethod()")//放入切入点的名称带() public void doBeforCheck(){ System.out.println("我是前置通知!!"); } }
package com.spring.aop.service;public interface PersonServiceBean { public void save(String name); public void update(String name,Integer id); public String getPersonName(Integer id);}
package com.spring.aop.service.impl;import com.spring.aop.service.PersonServiceBean;public class PersonServiceBeanImpl implements PersonServiceBean{ public void save(String name) { System.out.println("我是存入方法:"+name); } public void update(String name, Integer id) { System.out.println("我是更新方法:"+id); } public String getPersonName(Integer id) { return "测试:"+id; }}
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- <bean name="myTest" class="com.service.impl.PersonServiceImpl"> </bean> <bean id="myTest3" name="myTest4" class="com.service.impl.PersonServiceImpl"/> <bean id="myTest2" name="myTest" class="com.service.impl.PersonServiceImpl"/> <bean id="personServiceImpl" class="com.service.impl.PersonServiceImpl" lazy-init="false" scope="prototype"></bean> --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <bean id="interCeptor" class="com.spring.aop.service.InterCeptor"></bean> <bean id="personServiceBeanImpl" class="com.spring.aop.service.impl.PersonServiceBeanImpl"></bean></beans>
package mytest;import java.lang.reflect.Method;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.spring.aop.service.PersonServiceBean;import com.spring.aop.service.impl.PersonServiceBeanImpl;//import com.service.impl.PersonServiceImpl;public class SpringTest { @Test public void myTest(){ ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"bean.xml"});// PersonServiceImpl personServiceFactory = (PersonServiceImpl)ctx.getBean("personServiceImpl");// PersonServiceImpl personServiceFactory2 = (PersonServiceImpl)ctx.getBean("personServiceImpl");// if(personServiceFactory == personServiceFactory2){// System.out.println("====2=====");// }// Class<PersonServiceImpl> cPersonServiceImpl = PersonServiceImpl.class;// personServiceFactory.save(); PersonServiceBean personServiceBeanImpl = (PersonServiceBeanImpl)ctx.getBean("personServiceBeanImpl"); personServiceBeanImpl.save("test"); }}
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
如果我去掉这个实现,就一切正常了。这个是什么原因呢?
记得CGlib这个包可以实现没有接口的AOP,难道是这个原因么?那么我把CGlib这个包去掉一样存在这个问题。。。
不太懂了。。。。。。
[解决办法]
PersonServiceBean personServiceBeanImpl = (PersonServiceBeanImpl)ctx.getBean("personServiceBeanImpl");
恩是的,这样要报错。
所以改为:
PersonServiceBean personServiceBeanImpl = (PersonServiceBean)ctx.getBean("personServiceBeanImpl");
personServiceBeanImpl.save("test");
[解决办法]
楼主不知道用你要管理的bean上面加上@Component,xml文件只需<context:annotation-config />
<context:component-scan base-package="com.bjsxt" />
总之统一比较好,不会出莫名其妙的错误