簡體   English   中英

Spring AOP代理使用

[英]Spring AOP proxy use

我嘗試使用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>

我認為您的解決方法有誤,我嘗試了以下方法,它的工作原理是:

@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所表示的,對於SpokerImpl (值一個)的實現,將Performer聲明為Guitar的實現的父項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM