如何为ProxyFactoryBean设置许多目标?

编程入门 行业动态 更新时间:2024-10-26 18:26:39
本文介绍了如何为ProxyFactoryBean设置许多目标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用Spring 4 AOP,现在,我将ProxyFactoryBean配置如下:

I am working with Spring 4 AOP and right now, i have my ProxyFactoryBean configured like this:

@Bean @Primary public ProxyFactoryBean proxyFactoryBean() { ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean(); proxyFactoryBean.setTarget(new ClientService()); proxyFactoryBean.addAdvice(new LoggingAdvice()); proxyFactoryBean.addAdvice(new DebugInterceptor()); return proxyFactoryBean; }

这有效,但是目标只是ClientService对象.

This works, but the target is just the ClientService object.

是否可以设置许多目标,而不仅仅是一个?如果可能的话,我想将这些建议设置在整个程序包中.否则,设置具体目标,但不仅要设置一个.你怎么能做到这一点 ?预先感谢

Is it possible to set many targets and not just one ? I want to set those advices to an entire package, if it is possible. Otherwise, set specifics targets, but again, not just one. How could you do that ? Thanks in advance

推荐答案

使用Spring的自动代理功能. las,切入点api在基于Java的配置中使用有点麻烦;我通常将 AbstractAutoProxyCreator子类化这样我就可以用Java代码表示切入点.

Proxying all beans in an application context that match certain criteria is easiest done with Spring's AutoProxy-Facility. Alas, the pointcut api is somewhat cumbersome to use in java based config; I usually subclass the AbstractAutoProxyCreator so I can express the pointcut in java code.

例如,我会做类似的事情:

For instance, I'd do something like:

@Bean AbstractAutoProxyCreator autoProxyCreator() { return new AbstractAutoProxyCreator() { @Override protected Object[] getAdvicesAndAdvisorsForBean(Class<?> beanClass, String beanName, TargetSource customTargetSource) { if (BusinessService.class.isAssignableFrom(beanClass)) { return new Object[] {loggingAdvice()}; } else { return DO_NOT_PROXY; } } }; } @Bean LoggingAdvice loggingAdvice() { return new LoggingAdvice(); } @Bean public PersonService personService() { return new PersonService(); }

此代码未经测试,因为我手头没有带有Spring(或Maven)的IDE,但是要旨应该可以.

This code is untested, as I don't have an IDE with Spring (or Maven) at hand, but the gist should work.

更多推荐

如何为ProxyFactoryBean设置许多目标?

本文发布于:2023-11-05 19:32:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561661.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:何为   目标   ProxyFactoryBean

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!