|
|
inject instance based on system property
my app has two quot;modesquot;
A) get all my accounts from the DB (select account from table)
B) get accounts from local file
how can I (in the appConfig.xml) say:
Code:
// both classes implement AccountDao
// I need to decide which one to inject based on
// a system property or input arg
if( System.getProperty(quot;account.filequot;) != null ){ lt;bean name=quot;account.dao.implquot; class=quot;foo.bar.dao.impl.AccountDaoFileImplquot;gt; lt;property name=quot;filePathquot; value=quot;${account.file}quot;/gt; lt;/beangt;
}else{ lt;bean name=quot;account.dao.implquot; class=quot;foo.bar.dao.impl.AccountDaoJdbcImplquot;gt; lt;property name=quot;jdbcTemplatequot; ref=quot;jdbc.templatequot;/gt; lt;/beangt;
}
all the examples I find on runtime configuration deal with JDBC props and is geared towards running the app in different environments.
My example deals more with the user not needing to know the names of class imples and making a decision on what class to inject based on the presence of a system property. I don't want users to have to know anything about the classes....and both modes are valid at any time on any environment.
java -jar MyJar.jar -Daccount.file=C:/myfile.txt
java -jar MyJar.jar -Daccount.file=/opt/MyApp/file.txt
java -jar MyJar.jar
I read:
sp...rideconfigurer
but as you can see in my example, each instance of my bean has different dependencies itself
When posting code/xml/stacktraces please use [ code][/code ] tags, that way it remains readable.
I would suggest a FactoryBean which returns the correct instance of the AccountDao to use. You could then also make the 2 AccountDao implementations lazy initialized so that 1 or the other is used. |
|