读书人

spring2.5.6实现注解AOP出现奇怪异常

发布时间: 2012-04-16 16:20:04 作者: rapoo

spring2.5.6实现注解AOP出现奇怪错误
代码如下:
拦截类

Java code
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("我是前置通知!!");    }            }

业务接口:
Java code
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);}

实现类:
Java code
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 code
<?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>

测试类:
Java code
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");    }} 


现在出现这个问题,如果PersonServiceBeanImpl implements PersonServiceBean也就是继承这个接口,就会报:
java.lang.ClassCastException: $Proxy7
at mytest.SpringTest.myTest(SpringTest.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:592)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
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)
类型转换错误,这个事JDK1.5环境报的错,1.6和这个还不相同
下面是JDK1.6的异常
java.lang.ClassCastException: $Proxy7 cannot be cast to com.spring.aop.service.impl.PersonServiceBeanImpl
at mytest.SpringTest.myTest(SpringTest.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)


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" />
总之统一比较好,不会出莫名其妙的错误

读书人网 >J2EE开发

热点推荐