Wire * ALL * @Service level将我的XML配置中的Spring bean转换为特定的类

时间:2015-01-29 10:32:30

标签: java spring

我有一个类 - 称之为MsgRouting.java - 在这个类中,我目前访问所有服务级别组件。在我的应用程序中,我只使用@Autowire,我使用Spring组件扫描。虽然,我意识到,我可以通过基类型自动装配所有类 - 我在其他地方这样做,但是目前,并非所有我的服务级别bean都从相同的基类型继承;我不想在我的所有服务中添加基本类型。

我是否可以在XML Spring配置中指定我希望将所有服务级别bean直接连接到MsgRouting.java类?这些都是@Service注释。

即。我不想这样做:

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

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

相当于这样:

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

然后在MsgRouting里面,只是

 service1.doStuff();

谢谢!

1 个答案:

答案 0 :(得分:0)

如果您不需要公共基类,但至少需要一个通用接口。 否则你将无法打电话给你&#34; doStuff&#34;无论如何。 在这种情况下,您只需自动连接该接口的列表

@Autowired
List<ServiceInterface> services;

虽然您的服务类看起来像:

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

修改 你当然也可以注入

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

然后只会获得附加了相应限定符的所有内容。但在我的意见中,这将是糟糕的设计。