java - Spring AOP代理使用

标签 java xml spring aop

我尝试使用AOP代理在Spring框架中引入新的方法。但在控制台上我看到类转换异常

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 中的实现的父级。

关于java - Spring AOP代理使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12042788/

相关文章:

java - Android - 导入时测试 "Well Formed"有效 XML

java - 表单内的 Thymeleaf href 转到不同页面

java - 我可以从 Eclipse View 中删除重复的运行配置吗?

.net - XML 单元测试 : XmlUnit alternatives for . NET?

java - log4j2 - 在执行期间更改输出文件

c# - 如果子节点满足特定条件,则检索 XML 父节点属性并将两者分配给变量

java - 来自 Spring Boot Controller 的 URL 重定向

java - Spring mvc中如何将URL作为参数传递

java - Eclipse 类未找到异常

java - 如果一个算法调用另一个算法来执行其功能,它的总体时间复杂度是否会受到影响?