1

我尝试使用AOP代理在 Spring 框架中引入新方法。但在控制台上我看到了class cast exeption

I WorkingException in thread "main" java.lang.ClassCastException: com.proxy.$Proxy11 cannot be cast to com.proxy.Spoker
    at com.proxy.MyAspect.extendsPosibility(MyAspect.java:27)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:603)
    at org.springframework.aop.aspectj.AspectJMethodBeforeAdvice.before(AspectJMethodBeforeAdvice.java:39)
    at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:49)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    at com.proxy.$Proxy11.perform(Unknown Source)
    at com.proxy.TestAOP.main(TestAOP.java:15)

我有课

public class TestAOP {

    /**
     * @param args
     */
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
        Performer per = (Performer) context.getBean("guitar");
        per.perform();

    }

}


@Component
@Qualifier("guitar")
public class Guitar implements Performer{

    @Override
    public void perform() {
        System.out.println("I PLAY");
    }

}

方面

@Aspect
@Component
public class MyAspect {

    @DeclareParents(defaultImpl = Guitar.class, value = "*.SpookerImp")
    @Autowired
    @Qualifier("guitar")
    private Performer guitar;

    @Before("performAll()")
    public void extendsPosibility() {
        System.out.println("I Working");
        ((Spoker)guitar).spook();
    }
    @Pointcut("execution(* *.*())")
    public void performAll() {
    }
}

@Component("SpokerImp")
class SpookerImp implements Spoker {

    @Override
    public void spook() {
        System.out.println("I Spoke");
    }

}

界面

interface Performer{
    void perform();

}

interface Spoker {

    void spook();
}

Beans.xml配置文件。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    <context:annotation-config></context:annotation-config>
    <context:component-scan base-package="com.proxy"></context:component-scan>

</beans>
4

1 回答 1

1

我认为您弄错了,我尝试了以下方法并且有效:

@Aspect
@Component
public class MyAspect {

    @DeclareParents(defaultImpl = Guitar.class, value = "com.proxy.SpookerImp")
    private Performer guitar;

}

和这个测试:

Spoker spoker = (Spoker)context.getBean("SpokerImp");
spoker.spook();
((Performer)spoker).perform();

见上文,我正在转换spokerPerformer. 这就是 DeclareParents 所指示的,对于SpokerImpl(值中的那个)的实现,声明Performer为具有 in 中的实现的父级Guitar

于 2012-08-20T18:24:02.627 回答