in Spring.config, posso definire un oggetto di tipo stringa?
-
03-07-2019 - |
Domanda
Posso fare qualcosa del genere:
<object id="mydb" type="string">
<value>"blah"</value> <-- note that <value> tag does not really exist
</object>
Per poterlo utilizzare in seguito in questo modo:
<object id="Someobject" type="Sometype">
<property name="ConnectionString" ref="mydb"/>
</object>
EDIT: questa era la soluzione SpringFramework.NET che stavo cercando. Sembra che anche PropertyPlaceholderConfigurer esista lì. Grazie a tutti.
Soluzione
Usa il PropertyPlaceholdConfigurer :
<bean id="PropertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="location" value="classpath*:/application.properties"/>
</bean>
<bean id="Someobject" class="somepackage.Sometype">
<property name="connectionString" value="${mydb}"/>
</bean>
L'impostazione di SYSTEM_PROPERTIES_MODE_OVERRIDE
consente di ignorare la proprietà tramite la riga di comando.
Altri suggerimenti
Usa segnaposto, ad esempio $ {magic} e definisci la chiave / valore in un file delle proprietà insieme a un PostProcessor. Google per segnaposto post processore primavera ...
Non vedo alcun vantaggio per la tua strada. È ancora tutto solo configurazione.
A volte le persone esternalizzano le stringhe di connessione al database in un file .properties e le ottengono in quel modo. Penso che abbia più senso della tua proposta.