Java springaop代理的使用

Java springaop代理的使用,java,xml,spring,aop,Java,Xml,Spring,Aop,我尝试使用AOPproxy在spring框架中引入新方法来类。但是在控制台上,我看到了类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

我尝试使用
AOP
proxy在spring框架中引入新方法来类。但是在控制台上,我看到了
类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>

我想你搞错了,我尝试了以下方法,效果不错:

@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();

如上所述,我正在将
辐条
投射给
表演者
。这就是DeclareParents所指出的,对于
SpokerImpl
(值中的一个)的实现,声明
Performer
Guitar
中实现的父级,我想你搞错了,我尝试了以下方法,效果很好:

@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();
如上所述,我正在将
辐条
投射给
表演者
。这就是DeclareParents所指出的,对于
SpokerImpl
(值中的一个)的实现,声明
Performer
Guitar
中实现的父级