这是我的弹簧配置文件:
<bean id="controller" class="com.sample.controller.Controller">
<property name="message" value="Controller1"/>
</bean>
<bean id="controller2" class="com.sample.controller.Controller2">
<property name="message" value="#{controller.message}"/>
</bean>
代码:
ApplicationContext context =
new ClassPathXmlApplicationContext("beans.xml");
Controller obj = (Controller) context.getBean("controller");
System.out.println(obj.getMessage());
obj.message = "Controller1 changed!";
Controller2 obj2 = (Controller2) context.getBean("controller2");
System.out.println(obj2.getMessage());
我希望输出为:
Controller1
Controller1 changed!
但它是
Controller1
Controller1
除了将Controller注入Controller1之外,是否有更简单的方法来获取更新值?
谢谢。
答案 0 :(得分:0)
将属性message
定义为自己的bean
<bean id="message" class="java.lang.String">
<constructor-arg value="Controller1"/>
</bean>
并更改原始bean配置
<bean id="controller" class="com.sample.controller.Controller">
<property name="message" ref="message"/>
</bean>
<bean id="controller2" class="com.sample.controller.Controller2">
<property name="message" ref="message"/>
</bean>
现在用
更改java代码中的消息 String obj = (String) context.getBean("message");