Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Spring中使用MethodInterceptor_Java_Spring_Interceptor_Spring Aop_Method Interception - Fatal编程技术网

Java 在Spring中使用MethodInterceptor

Java 在Spring中使用MethodInterceptor,java,spring,interceptor,spring-aop,method-interception,Java,Spring,Interceptor,Spring Aop,Method Interception,我使用以下配置截取方法并在从方法返回后应用建议,但是,以下配置不起作用。你能告诉我我遗漏了什么吗 @Service("txnEventSubscriber") EventSubscriberImpl ... @Resource(name="txnEventSubscriber") private EventSubscriberImpl subscriber; @Bean public Advice myAdvice() { return new Af

我使用以下配置截取方法并在从方法返回后应用建议,但是,以下配置不起作用。你能告诉我我遗漏了什么吗

@Service("txnEventSubscriber")
EventSubscriberImpl
...

@Resource(name="txnEventSubscriber")
    private EventSubscriberImpl subscriber;


    @Bean
    public Advice myAdvice() {
        return new AfterReturningAdvice() {
            @Override
            public void afterReturning(Object returnValue, Method method, Object[] args, Object target)
            {
                System.out.println("inside advice");
            }
        };
    }

    @Bean
    public ProxyFactoryBean myProxyFactoryBean() {
        return new ProxyFactoryBean() {
            private static final long serialVersionUID = 6296720408391985671L;

            @PostConstruct
            public void afterPropertiesSet() throws ClassNotFoundException {
                setTarget(subscriber);
                setInterceptorNames(new String[] {"myAdvice"});
            }
        };
    }
我有一个EventSubscriber,当调用和返回方法时,我需要截获方法调用并执行一些操作。。。在这种情况下,请打印“内部建议”


我没有看到任何异常,只是没有调用方法建议。

首先,我看到您的类名为
EventSubscriberImpl
,并且您正在注入相同类型的类。也就是说,您不是在编程接口。在这种情况下,您需要
setProxyTargetClass(true)ProxyFactoryBean
bean编写代码>并将CGLIB放入项目的类路径中

其次,你需要这样的东西

@Resource(name="myProxyFactoryBean")
private EventSubscriberImpl subscriber;
每当您想使用
EventSubscriberImpl
的代理版本时。也就是说,您需要通过代理bean的名称显式地获取代理bean

第三,我会使用类似这样的方法,以避免通过其代理名称获取bean:

@Resource(name="txnEventSubscriber")
private EventSubscriberImpl subscriber;

@Bean
public Advice myAdvice() {
    return new AfterReturningAdvice() {
        public void afterReturning(Object returnValue, Method method, Object[] args, Object target)
        {
            System.out.println("inside advice");
        }
    };
}

@Bean
public Advisor myAdvisor() {
    AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
    pointcut.setExpression("execution(public * com.foo.bar.EventSubscriberImpl.*(..))");
    return new DefaultPointcutAdvisor(pointcut, myAdvice());
}