1. 程式人生 > >Spring用bean.xml注入物件

Spring用bean.xml注入物件

Spring的物件注入
寫一個類

public class Circle {

    @Override
    public String toString() {
        return "Circle [radius=" + radius + "]";
    }

    private double radius;

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }


    public
Circle() { super(); } }

再建立一個bean.xml類

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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.xsd"
>
<!-- //此行在呼叫載入時會建立一個Circle 一個bean節點對應一個物件為單例,下次再呼叫時依然還是以前建立的物件。執行的為為無參的構造方法 --> <bean id="circle " class="com.bean.Circle" /> <!-- 也可以給物件屬性設值 在類中一定要寫set方法--> <bean id="circle2" class="com.bean.Circle"> <property name="radius" value="2.0"></property
>
</bean> <!-- 可以使用上個物件的值 不過要先載入上個物件 --> <bean id="circle3" class="com.bean.Circle"> <property name="radius" value="#{circle2.radius+1}"></property> </bean> </beans>

在程式碼中如何實現

   @Test
     public void CircleTest(){

            Circle circle=  (Circle) new ClassPathXmlApplicationContext("bean.xml").getBean("circle");
            circle.setRadius(12);
               System.out.println(circle);

            Circle circle2=  (Circle) new ClassPathXmlApplicationContext("bean.xml").getBean("circle2");
               System.out.println(circle2);

            Circle circle3=  (Circle) new ClassPathXmlApplicationContext("bean.xml").getBean("circle3");
               System.out.println(circle3);

     }  

當類中的屬性有引用型別

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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.xsd">
      <!-- 當屬性是引用型別時 用值用 ref -->
      <bean id="xiaoming" class="com.bean.Student">
       <property name="id" value="1"></property>
       <property name="name" value="xiaoming"></property>
       <property name="score" value="78"></property> 
       <property name="birthday" ref="birthday"></property>
      </bean>

      <bean id="birthday" class="com.bean.BirthDay"> 
       <property name="year" value="1994"></property>
       <property name="month" value="03"></property>
       <property name="day" value="15"></property>
      </bean>

      </beans>

特殊屬性如List Set 等

<bean id="myColl" class="com.phone1000.je1702.sp03.MyCollection">
        <property name="array">
            <array>
                <value>zhangsan</value>
                <value>zhaoziqiang</value>
                <value>xiazenghao</value>
                <value>hejiangtao</value>
                <value>wucaibing</value>
                <value>chenjianjian</value>
                <value>hushengli</value>
            </array>
        </property>

        <property name="list">
            <list>
                <value>wangjishen</value>
                <value>hexiangyi</value>
                <value>wanghailin</value>
                <value>lizhiyang</value>
            </list>
        </property>

        <property name="set">
            <set>
                <value>zhangruixuan</value>
                <value>yangyubing</value>
                <value>changzhongbo</value>
                <value>zhoujunfeng</value>
            </set>
        </property>

        <property name="map">
            <map>
                <entry key="banzhang" value="zhangjiandong"></entry>
                <entry key="xuewei" value="changzhongbo"></entry>
                <entry key="laowei" value="hushengli"></entry>
                <entry key="kongwei" value="xujinxin"></entry>
            </map>
        </property>

        <property name="props">
            <props>
                <prop key="driver">mysqldriver</prop>
                <prop key="url">mysqlurl</prop>
                <prop key="username">root</prop>
                <prop key="password">root</prop>
            </props>
        </property>
    </bean>

有參構造

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context  
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- <bean id="emp" class="com.sp05.Employee">
        <constructor-arg name="sex" value="boy" />
        <constructor-arg name="dept" ref="dept" />
        <constructor-arg name="name" value="zhang"/>
        <constructor-arg name="salary" value="10000" />
    </bean> -->

    <!-- <bean id="emp" class="com.sp05.Employee">
        <constructor-arg name="name" value="zhang"/>
        <constructor-arg name="salary" value="10000" />
        <constructor-arg name="sex" value="boy" />
        <constructor-arg name="dept" ref="dept" />
        <property name="dept" ref="dept"></property>
    </bean> -->

    <!-- <bean id="emp" class="com.sp05.Employee">
        <constructor-arg index="0" value="zhang"/>
        <constructor-arg index="1"  value="10000" />
        <constructor-arg index="2" value="boy" />
        <constructor-arg index="3" ref="dept" />
    </bean> -->

    <!-- <bean id="emp" class="com.sp05.Employee">
        <constructor-arg type="java.lang.String" value="zhang"/>
        <constructor-arg type="double"  value="10000" />
        <constructor-arg type="java.lang.String" value="boy" />
        <constructor-arg type="com.sp05.Dept" ref="dept" />
    </bean> -->

    <bean id="emp" class="com.sp05.Employee">
        <constructor-arg value="zhang"/>
        <constructor-arg value="10000" />
        <constructor-arg value="boy" />
        <constructor-arg ref="dept" />
    </bean>

    <bean id="dept" class="com.sp05.Dept">
        <property name="did" value="101" />
        <property name="dname" value="java ee" />
    </bean>
</beans>