无法为代理对象执行方面

Can't get the aspect executed for proxy objects

我正在尝试在代理对象上执行一个方面

package thispkg;

public class MyLogger {
    public void before() {
        System.out.println("=========Before========");
    }
    public void after() {
        System.out.println("=========After=========");
    }
    public void info() {
        System.out.println("=========Info=========");
    }
}

package thispkg;

public interface MyInterface {
    public void speak();
}

package thispkg;

public class MyInterfaceImpl implements MyInterface {
    @Override
    public void speak() {
        System.out.println("MyInterfaceImpl :: Hello world");
    }
}

package thispkg;

public class RandomClass {
    public void suvichar() {
        System.out.println("RandomClass (suvichar)::Karm kiye jaa, fal ki chinta mat kar");
    }
}

package thispkg;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("thispkg.xml");
        MyInterface in = (MyInterface) context.getBean("randomClass", RandomClass.class);
        in.speak();
    }
}

我的XML

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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">

    <context:spring-configured/>
    <bean id="mylogger" class="thispkg.MyLogger"/>
    <bean id="randomClass" class="thispkg.RandomClass"/>
    <bean id="myInterfaceImpl" class="thispkg.MyInterfaceImpl"/>
    <aop:config proxy-target-class="true">
        <aop:aspect id="usageTrackerAspect" ref="mylogger">
            <aop:declare-parents types-matching="thispkg.RandomClass+" implement-interface="thispkg.MyInterface" default-impl="thispkg.MyInterfaceImpl"/>
            <aop:pointcut expression="this(thispkg.MyInterface)" id="randompointcut"/>
            <aop:before pointcut-ref="randompointcut" method="info"/>
        </aop:aspect>
    </aop:config>
</beans>

我已经尝试了 thispkg.MyInterfaceImplthispkg.RandomClass 切入点表达式但仍然无法获取 ========Info======== 打印。仅打印

MyInterfaceImpl :: Hello world

有什么线索吗?

我不确定这是错误还是有意为之。

您将 MyInterface(和 MyInterfaceImpl)添加为 introductions

<aop:declare-parents ... />

这些都是用代理对象中的特定拦截器(称之为引入拦截器)实现的。切面作为单独的拦截器(称为 AOP 拦截器)添加到代理中。一个不能(不)拦截另一个。

如果您执行以下操作

RandomClass bean = context.getBean(RandomClass.class);
bean.suvichar();

您的建议将被调用,因为 AOP 拦截器开始发挥作用。

MyInterface in = (MyInterface) context.getBean("randomClass", RandomClass.class);
in.speak();

然而,它是处理 speak 调用的引入拦截器。您的 AOP 拦截器不涉及,因此不会调用 before 建议。

因此,经过一些尝试、更多阅读和更多重试之后,我发现在使用 delegate-ref 而不是 default-impl 时我可以获得所需的行为,从而允许 spring 管理我的实现。同时更正我的切入点表达式。

<aop:aspect ref="mylogger">
        <aop:declare-parents types-matching="thispkg.RandomClass+" implement-interface="thispkg.MyInterface" delegate-ref="myInterfaceImpl"/>
        <aop:pointcut expression="execution(* thispkg.MyInterface.speak())" id="randompointcut"/>
        <aop:before method="info" pointcut-ref="randompointcut"/>
</aop:aspect>

输出

=========Info=========
MyInterfaceImpl :: Hello world