Spring公共属性的流入

Spring公共属性的注入

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>