2

In my spring context I am creating a service bean and a proxy for this service bean (explicitly). Both implement the same interface.

Can I ensure that autowiring cannot inject the target bean?

I would like to be able to use the target service with the @Resource or @Qualifier annotations, but when autowiring it should always be the proxy.

Any ideas?

4

2 回答 2

2

Use the Primary annotation. It will indicate which bean should be use preferably when autowiring.

Hope this helps :)

于 2013-02-19T10:30:51.637 回答
0

You can put @Primary annotation in your proxy service like bellow:

@Primary
@Repository
public class ProxyOfSomeService implements SomeService

And after that when you use, @Autowired annotation on SomeService field, the ProxyOfSomeService will be injected by deafault.

But when you need the real service you can have it like bellow:

@Autowired
@Resource(name="someRealService")
private SomeService someService;

I think this serves your need, thanks!

于 2013-02-24T18:06:42.947 回答