java - Spring AOP : Interceptor not working

标签 java spring aop spring-aop aspect

注释:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface DeadlockRetry {


}

拦截器:

public class DeadlockRetryMethodInterceptor implements MethodInterceptor
{

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable
    {
        System.err.println("I've been Intercepted!");
        return invocation.proceed();
    }
}

被拦截的类:

public class Intercepted {


    @DeadlockRetry
    public void interceptMe()
    {
        System.err.println("Above here should be a message I've been Intercepted!");
    }
}

主要起点:

public class Main {
    public static void main(String[] args)
    {
        //Function that loads the spring-context.xml
        SpringContext.init();

        Intercepted intercepted = new Intercepted();
        intercepted.interceptMe();
    }
}

spring-context.xml:

<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"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
     http://www.springframework.org/schema/beans
     classpath:/org/springframework/beans/factory/xml/spring-beans-3.2.xsd
     http://www.springframework.org/schema/tx
     classpath:/org/springframework/transaction/config/spring-tx-3.2.xsd
     http://www.springframework.org/schema/aop
     classpath:/org/springframework/aop/config/spring-aop-3.2.xsd
     http://www.springframework.org/schema/context
     classpath:/org/springframework/context/config/spring-context-3.2.xsd">


    <aop:aspectj-autoproxy />



    <context:annotation-config/>

    <bean id="deadlockRetryAdvice" class="com.metaregistrar.hibernate.DeadlockRetryMethodInterceptor"/>



    <bean id="deadlockPointcutAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
        <property name="advice" ref="deadlockRetryAdvice"/>
        <property name="pointcut" ref="deadlockRetryPointcut"/>
    </bean>


    <bean name="deadlockRetryPointcut" class="org.springframework.aop.support.annotation.AnnotationMatchingPointcut">
        <constructor-arg index="0" value="com.metaregistrar.hibernate.DeadlockRetry"/>
        <constructor-arg index="1" value="com.metaregistrar.hibernate.DeadlockRetry"/>
    </bean>



</beans>

Stderr 结果:

上面应该是一条我被拦截的消息!

预期的 Stderr 结果:

我被拦截了! 上面应该是一条我被拦截的消息!

我做错了什么?我已经被这个问题困扰一整天了,而且它变得非常烦人......

最佳答案

Spring AOP 仅适用于 Spring bean。您应该在 spring 配置文件中将拦截的对象定义为 bean,从上下文中获取它并调用它的方法。

这是不正确的部分。

Intercepted intercepted = new Intercepted();
intercepted.interceptMe();

将其添加到 xml 文件中

然后从spring ctx中检索实例

ApplicationContext ctx = // create context using the config file

intercepted = ctx.getBean("intercepted",Intercepted.class);

intercepted.interceptMe();

关于java - Spring AOP : Interceptor not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18021071/

相关文章:

JAVA : file I/O

java - 更改 sonar.java.binaries 属性中的行为

spring - 如何在AOP中实现策略模式

java - 使用 Java 10 的 jaotc 编译单个类

java - 为什么 BluetoothLEScanner 不调用它的 ScanCallback

java - 如何从 WSDL 创建包含多个服务的 WebService?

spring - 使用docker和gradle在spring-client上连接被拒绝

java - ProtoBuf 消息通过 Mqtt 订阅者问题

java - Spring数据休息Bean验证国际化

java - 错误::0 找不到引用的切入点注释