开发者

Spring: What is the best way to configure at startup time which service beans should be initialized?

Ok, this might sound like a weird question, but here's the catch: We have a Spring-based application here that has different "setup" modes. In one mode it needs to use additional service beans (that connect to开发者_Python百科 external resources), in the other mode it does not.

What I'm now looking for is some kind of way to prevent those beans from being initialized that are not needed in one setup mode and the only thing I came across was to use the standard applicationContext.xml way of doing things (i.e. no @Service annotation) and somehow using @Autowired(required=false) in those beans depending on the (optional) components.

Is this a valid way to go further (and even possible)? I really don't like to loose too much from the easyness of the annotation-based wiring and want to write as less XML configuration as possible...


If you can use Spring 3.1 there is a new feature called Profiles that you can use to do what you want. Check out this blog post which explains how to use Profiles with annotation-based code.


One way is to isolate the different classes for your different "modes" in their own packages. Then you can use the <context:component-scan> element in your application context xml to control which packages are scanned and which are not. Or use inclusion/exclusion filters (see http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-scanning-filters). That way, you can define exclusion-filters based on your own custom annotations, for instance.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜