简体   繁体   中英

Wire *ALL* @Service level Spring beans in my XML config into a specific class

I have a class - call it MsgRouting.java - in this class currently I access all Service level components. In my application I only use @Autowire, and I use Spring component scanning. Whilst, I realise, I can autowire all classes by base type- I do this elsewhere, but currently, not all my Service level beans inherit from the same base type; and I don't want to have to add a base type to all my Services.

Is there a way I can specify inside my XML Spring config that I want to have all Service level beans wired directly into the MsgRouting.java class? These are all @Service annotated.

Ie I don't want to have to do this:

 @Autowire Service1 service1
 @Autowire Serviec2 service2 
 @Autowire Service3 ... ad infinitum

 service1.doStuff();
 service2.doOtherStuff();
 service3.calculateSomething();

rather something like this:

 <bean id="RouteIncomingNetMSG" class="uk.co.foo.MsgRouting">
        <gimme>
</bean>

then inside MsgRouting, just

 service1.doStuff();

Thanks!

If you do not need to have a common base class but at least a common interface. As otherwise you would not be able to call you "doStuff" anyway. In this case you can just auto wire a list of that interface

@Autowired
List<ServiceInterface> services;

While your service classes look like:

@Service
public class Service1 implements ServiceInterface {...}

Edit: You could of course also just inject

@Autowired
@Qualifier("MyServices")
List<Object> services;

and then would just get everything that has a corresponding qualifier attached. But in my oppinion that would be bad design.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM