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类

    
<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">
    
    <bean id="circle " class="com.bean.Circle" />
    
    <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);

     }  

当类中的属性有引用类型


<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">
      
      <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>zhangsanvalue>
                <value>zhaoziqiangvalue>
                <value>xiazenghaovalue>
                <value>hejiangtaovalue>
                <value>wucaibingvalue>
                <value>chenjianjianvalue>
                <value>hushenglivalue>
            array>
        property>

        <property name="list">
            <list>
                <value>wangjishenvalue>
                <value>hexiangyivalue>
                <value>wanghailinvalue>
                <value>lizhiyangvalue>
            list>
        property>

        <property name="set">
            <set>
                <value>zhangruixuanvalue>
                <value>yangyubingvalue>
                <value>changzhongbovalue>
                <value>zhoujunfengvalue>
            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">mysqldriverprop>
                <prop key="url">mysqlurlprop>
                <prop key="username">rootprop>
                <prop key="password">rootprop>
            props>
        property>
    bean>

有参构造

"1.0" encoding="UTF-8"?>
"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">

    

    

    

    

    "emp" class="com.sp05.Employee">
        value="zhang"/>
        value="10000" />
        value="boy" />
        ref="dept" />
    

    "dept" class="com.sp05.Dept">
        "did" value="101" />
        "dname" value="java ee" />
    

你可能感兴趣的:(spring,对象)