开发者

Spring: how to hide bean from dependency injection?

The situation is that I have two different resource bundles, a general one and a more specific one. They don't share any message keys.

General one:

<bean id="messageSourceGlobal" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="messages/messagesGlobal" />
</bean>

I include the general one in my specific one (different file obviously):

<bean id="messageSource" class="org.springframework.co开发者_开发知识库ntext.support.ResourceBundleMessageSource">
    <property name="basename" value="messages/messages" />
    <property name="parentMessageSource" ref="messageSourceGlobal" />
</bean>

The Java-code then autowires it:

@Autowired
private MessageSource messages;

This will cause an exception when starting the web-app as two MessageSources are found. Obviously I can use a @Qualifier to make it clear to Spring what I want. However, the general resource bundle won't be used alone. Therefore, I thought that in this case it would make sense to hide the general resource bundle from dependency injection. One benefit would be that others won't run into the "duplicates".

Is this possible? How would I do this?


Sure you can add autowire-candidate="false" on the definition of the bean you want to hide.


In many cases it could be better to promote one bean with primary="true" or @Primary instead of demoting all other candidates with autowire-candidate="false".

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜