您的位置:首页 > 编程语言 > Java开发

Spring继承Bean的配置

2015-08-15 18:10 561 查看
1.使用parent配置继承bean

//address2继承了address1,继承了其city属性的值,继承了其类类型的配置

//address2并不是没有继承address1的location属性,而是被address2自己的location属性值覆盖了

<bean id="address1" class="autowire.Address" p:city="wuhan1" p:location="hongshanqu"></bean>

<bean id="address2" p:location="wuchangqu" parent="address1"></bean>

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

Address address1 = (Address) ac.getBean("address1");

Address address2 = (Address) ac.getBean("address2");

System.out.println(address1);

System.out.println(address2);

输出:

Address [city=wuhan1, location=hongshanqu]

Address [city=wuhan1, location=wuchangqu]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: