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

上記を参照spokerしてくださいPerformer。これは、DeclareParentsが示していることです。(値の1つ)の実装については、の実装で親としてSpokerImpl宣言します。PerformerGuitar

于 2012-08-20T18:24:02.627 に答える