在Spring中使用MethodInterceptor [英] Using MethodInterceptor in Spring

查看:226
本文介绍了在Spring中使用MethodInterceptor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下配置来拦截方法并从方法返回后应用建议,但是,以下配置不起作用.你能建议我所缺少的吗?

I have the following configuration to intercept a method and apply advice after returning from method, But, the following configuration does not work. Could you suggest on what I am missing?

@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,当调用它并返回方法时,我需要截获该方法调用并执行某些操作……在这种情况下,请打印内部建议".

I have the EventSubscriber which when invoked and when method is returned, I need to intercept the method call and do something... in this case, print "inside advice".

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

I am not seeing any exceptions, just method advice is not getting called.

推荐答案

首先,我看到您的类名称为EventSubscriberImpl,并且正在注入相同类型的类.意思是,您不是在对接口进行编程.在这种情况下,您可能希望setProxyTargetClass(true);为您的ProxyFactoryBean bean,并将CGLIB放入项目的类路径中.

First of all, I see you have the class name as EventSubscriberImpl and you are injecting the same type of class. Meaning, you are not programming to interfaces. In this case, you would want to setProxyTargetClass(true); for your ProxyFactoryBean bean and put CGLIB in your project's classpath.

第二,您需要类似这样的东西

Secondly, you would need something like this

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

每当您要使用EventSubscriberImpl的代理版本时.意思是,您需要通过其代理的bean名称来显式地获取该代理的bean.

whenever you want to use the proxied version of your EventSubscriberImpl. Meaning, you need to explicitly get that proxied bean by its proxied bean name.

第三,我将使用类似的方法,以避免使用其代理名称获取该豆:

Thirdly, I'd use something like this, to avoid getting the bean by its proxied name:

@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());
}

这篇关于在Spring中使用MethodInterceptor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆