You want PropertyPlaceholderConfigurer . This section of the manual demonstrates this better than I could locally.
In your example, you need to either change the value of the property to class1 or class2 (the name of the desired bean in the context of spring).
Alternatively, your configuration could be:
<bean id="mainView" class="mainView"> <property name="angebotsClient" ref="angebotsClient" /> <property name="class"> <bean class="${classToUse}"> <constructor-arg ref="mainView"/> </bean> </property> </bean>
with a configuration file containing: classToUse = fully.qualified.name.of.some.Class
Using a bean or class names would not be acceptable in a user-editable configuration file, and you really need to use "Y" and "N" as the values ββfor the configuration parameter. In this case, you just need to do it in Java, spring is not meant to be completed.
mainView can directly access the application context:
if (this.withSmartCards) { this.class_ = context.getBean("class1"); } else { this.class_ = context.getBean("class2"); }
A cleaner solution would be to encapsulate user configuration processing in its own class, which would do it higher in order to reduce the number of classes that should be ApplicationContextAware and, if necessary, introduce them into its other classes.
Using BeanFactoryPostProcessor , you can register a class property definition programmatically. Using FactoryBean , you can dynamically create a bean. Both are somewhat advanced ways to use Spring.
Aside: I'm not sure if your config example is legal, given the circular dependency between mainView and class1 / class2.
millimoose
source share