Spring公共属性的注入
公共的意思是多个bean拥有同名的字段名,把这些相同的字段提取出来放到配置文件中。这样可以减少配置文件的大小。
而不是说字段是public的。
applicationContext.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:amq="http://activemq.apache.org/schema/core" xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:context="http://www.springframework.org/schema/context"
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://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd">
<!-- 公共属性的注入。公共的意思是多个bean有相同的属性名,而不是说属性是public的 -->
<!-- 公共的属性需要bean标签声明为抽象的abstract="true"。因为它没不属于具体的类,因此没有class这个属性-->
<bean id="publicbean" abstract="true">
<property name="id" value="0001"/>
<property name="name" value="zhangsan"/>
</bean>
<!-- 当bean的属性有parent时,说明此bean有公用的属性注入,parent的值就为公用属性的bean的id值 -->
<bean name="bean1" class="com.cos.bean110317.Bean1" parent="publicbean">
<property name="password" value="123"/>
</bean>
<bean name="bean2" class="com.cos.bean110317.Bean2" parent="publicbean"/>
</beans>
本文介绍了Spring框架中如何使用公共属性注入来简化配置文件。通过将多个bean的共有属性提取到一个抽象bean中,实现了一次定义多次复用的目的。
214

被折叠的 条评论
为什么被折叠?



