代码如下:
拦截类package com.spring.aop.service;import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;@Aspect
public 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");
}
}现在出现这个问题,如果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这个包去掉一样存在这个问题
不太懂了

解决方案 »

  1.   

    你要用annotation的形式就全用annotation,要不就全用XML,混用有时会有问题的
      

  2.   

    PersonServiceBean personServiceBeanImpl = (PersonServiceBeanImpl)ctx.getBean("personServiceBeanImpl");
    恩是的,这样要报错。
    所以改为:
    PersonServiceBean personServiceBeanImpl = (PersonServiceBean)ctx.getBean("personServiceBeanImpl");
            personServiceBeanImpl.save("test");
      

  3.   

    IOC容器我仅仅是注册,难道连注册都不用?
      

  4.   

    检查AOP语法 看语法是否支持
      

  5.   

    楼主不知道用你要管理的bean上面加上@Component,xml文件只需<context:annotation-config />
    <context:component-scan base-package="com.bjsxt" />
    总之统一比较好,不会出莫名其妙的错误
      

  6.   

    。老大,你说的是注入的注解实现。
    我说的是AOP的注解实现,以前项目没怎么接触过,试试罢了。
    谢谢abstruct兄,都弄明白了。送分啦。。
      

  7.   

    还有你说的统一,如果项目太大的话,IOC容器会显得过于臃肿,注解实现也是为IOC减肥的一个策略罢了。