如果总统发生变化,我必须在presidentName
中将 application-context.xml
的值更改为以下三倍:
<beans:property name="presidentName" value="Barack Obama" />
有没有办法在application-context.xml
中设置一次变量来表示字符串 Barack Obama
。
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.xsd">
<beans:bean id="testBeanA" class="com.TestBean">
<beans:property name="presidentName" value="Barack Obama" />
</beans:bean>
<beans:bean id="testBeanB" class="com.TestBean">
<beans:property name="presidentName" value="Barack Obama" />
</beans:bean>
<beans:bean id="testBeanC" class="com.TestBean">
<beans:property name="presidentName" value="Barack Obama" />
</beans:bean>
</beans:beans>
答案 0 :(得分:12)
正如yawn所指出的,你可以定义一个新的String-class spring bean。
<bean id="testBeanA" class="com.TestBean">
<property name="presidentName" ref="potus" />
</bean>
<bean name="potus" class="java.lang.String">
<constructor-arg value="Barack Obama" />
</bean>
答案 1 :(得分:2)
在spring中定义一个bean,将其指定为abstract(abstract =“true”)并在那里注入presidentName属性。然后,您可以通过指定先前定义为父级的抽象bean来定义3个具体bean。例如。
<beans:bean id="testBeanSpec" class="com.TestBean" abstract="true">
<beans:property name="presidentName" value="Barack Obama" />
</beans:bean
<beans:bean id="testBeanA" class="com.TestBean" parent="testBeanSpec">
</beans:bean
答案 2 :(得分:2)
将president
定义为String-classed bean?