码迷,mamicode.com
首页 > 编程语言 > 详细

[Spring入门点滴]利用构造函数和setter注入

时间:2016-04-26 17:32:08      阅读:299      评论:0      收藏:0      [点我收藏+]

标签:

构造器方式注入:

  主要是了解构造器方式注入时,参数顺序的区分(index),以及类型的区分(type),以及两者的混合使用;另外就是,当传入的内容有特殊符号时,借助于value标签和CDATA的处理。以保障程序的正常运行!

/**
 * Created by wangzh on 2016/4/26.
 * Description:通过构造器注入实体;
 * 两个构造器区别就是类型不同;
 * 在配置spring时,可以利用index或者type进行区分;
 * 而对于包含特殊符号的值,可以结合<![CDATA[<beijing>^_^]]> 来进行转义;
 */
public class Car {

    private String brand;
    private String city;
    private double price;
    private int maxSpeed;

    public Car(String brand, String city, double price) {
        super();
        this.brand = brand;
        this.city = city;
        this.price = price;
    }


    public Car(String brand, String city, int maxSpeed) {
        super();
        this.brand = brand;
        this.city = city;
        this.maxSpeed = maxSpeed;
    }


    @Override
    public String toString() {
        return "Car{" +
                "brand=‘" + brand + ‘\‘‘ +
                ", city=‘" + city + ‘\‘‘ +
                ", price=" + price +
                ", maxSpeed=" + maxSpeed +
                ‘}‘;
    }
}

spring-context.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">

    <bean id="helloWorld" class="org.study.java.lessions.spring.HelloWorld" destroy-method="destroyMethod"
          init-method="initMethod">
        <property name="name" value="spring"></property>
    </bean>

    <!--依赖bean注入-->
    <bean id="menBean" class="org.study.java.lessions.spring.Men">
        <property name="person" ref="personBean"></property>
    </bean>
    <!--属性字段赋值-->
    <bean id="personBean" class="org.study.java.lessions.spring.Person">
        <property name="name" value="hager"/>
        <property name="age" value="29"/>
        <property name="sex" value="男"/>
    </bean>

    <!--通过构造器进行注入,同时指定了参数顺序。but,解决不了重载的问题-->
    <!--<bean id="car" class="org.study.java.lessions.spring.Car">-->
    <!--<constructor-arg value="BMW" index="0"/>-->
    <!--<constructor-arg value="beijing" index="1"/>-->
    <!--<constructor-arg value="30.00" index="2"/>-->
    <!--</bean>-->

    <!--解决重载问题:利用type类型区分-->
    <bean id="car" class="org.study.java.lessions.spring.Car">
        <constructor-arg value="BMW" index="0"/>
        <constructor-arg value="beijing" index="1"/>
        <constructor-arg value="30.00" index="2" type="double"/>
    </bean>

    <!--解决包含特殊符号的问题:结合CDATA处理-->
    <bean id="car2" class="org.study.java.lessions.spring.Car">
        <constructor-arg index="0" type="java.lang.String">
            <value><![CDATA[<B`M``W```>^_^]]></value>
        </constructor-arg>
        <constructor-arg value="beijing" index="1"/>
        <constructor-arg value="30.00" index="2"/>
    </bean>
</beans>

测试用例:

/**
 * Created by wangzh on 2016/4/26.
 * Description:
 */
public class ConstructorTest {

    @Test
    public void construct_Test() {
        ApplicationContext atx = new ClassPathXmlApplicationContext(new String[]{"spring-context.xml"});
        //方法一: 通过byName进行获取
        Car car = (Car) atx.getBean("car");
        System.out.println(car.toString());

        //方法二: 利用byType方式注入,但是如果有多个相同类型的,那么就会失败。因为无法辨别用哪个
        // 因为下面我还声明了一个id = car2 但是 type依然是car的bean.
        Car car2 = atx.getBean(Car.class);
        System.out.println(car2.toString());

        Assert.assertNotNull("true");
    }

    @Test
    public void construct2_Test() {
        ApplicationContext atx = new ClassPathXmlApplicationContext(new String[]{"spring-context.xml"});
        Car car = (Car) atx.getBean("car2");
        System.out.println(car.toString());

        Assert.assertNotNull("true");
    }
}

输出结果:

# 利用byType方式获取的话,会提示:不是唯一声明,找到了两个bean.
org.springframework.beans.factory.NoUniqueBeanDefinitionException: 
No qualifying bean of type [org.study.java.lessions.spring.Car] is defined: expected single matching bean but found
2: car,car2 # 正常结果如下:利用CDATA包装后的结果 i‘m initMethod. Car{brand=‘<B`M``W```>^_^‘, city=‘beijing‘, price=30.0, maxSpeed=0}

 

setter方式注入:

[Spring入门点滴]利用构造函数和setter注入

标签:

原文地址:http://www.cnblogs.com/hager/p/5435820.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!