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>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值