So once you've split your application context into three pieces (the main piece and each alternative piece), then what?
Spring presently doesn't have any sort of conditionals for application contexts. It does have an <import> statement. You can use property placeholder syntax to conditionalize the import, thusly:
<import resource="${whichOne}ChoiceContext.xml"/>
And that works, as long as the whichOne property is a system property. But what if you're using PropertyPlaceholderConfigurer to get placeholder material from a property file? That won't work on <import> tags, because imports take place before bean creation, which is where the configurer gets loaded.
The workaround for that, it turns out, is to rephrase your <import> like this:
<bean id="myChoiceBeanFactory" class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg type="java.lang.String" value="${whichOne}ChoiceContext.xml"/>
</bean>
<bean factory-bean="myChoiceBeanFactory" factory-method="getBean">
<constructor-arg value="myChoiceBean"/>
</bean>
That is, create a ClassPathXmlApplicatioContext object out of the correct context file, then call getBean("myChoiceBean") on it, and cache the resulting bean in this context as "myChoiceBean".
No comments:
Post a Comment