Spring学习--依赖注入的方式

Spring 依赖注入

  1. 属性注入(最常使用)
  2. 构造函数注入
  3. 工厂方法注入(很少使用,不推荐)

属性注入

通过 setter 方法注入 Bean 的属性值或依赖的对象 , 使用<property> 元素 , 使用 name 属性指定 Bean 的属性名称 , value 属性或 <value> 子节点指定属性值。

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 5 
 6     <bean id="person" class="com.itdjx.spring.dependency.injection.Person">
 7         <property name="name" value="张三" />
 8         <property name="age" value="23" />
 9         <property name="sex" value="男" />
10     </bean>
11 
12 </beans>
 1 package com.itdjx.spring.dependency.injection;
 2 
 3 /**
 4  * 属性注入
 5  *
 6  * @author Wáng Chéng Dá
 7  * @create 2017-02-28 15:14
 8  */
 9 public class Person {
10 
11     private String name;
12 
13     private String sex;
14 
15     private int age;
16 
17     public String getName() {
18         return name;
19     }
20 
21     public void setName(String name) {
22         this.name = name;
23     }
24 
25     public String getSex() {
26         return sex;
27     }
28 
29     public void setSex(String sex) {
30         this.sex = sex;
31     }
32 
33     public int getAge() {
34         return age;
35     }
36 
37     public void setAge(int age) {
38         this.age = age;
39     }
40 
41     @Override
42     public String toString() {
43         return "Person{" +
44                 "name='" + name + '\'' +
45                 ", sex='" + sex + '\'' +
46                 ", age=" + age +
47                 '}';
48     }
49 }
 1 package com.itdjx.spring.dependency.injection;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 /**
 7  * 依赖注入main
 8  *
 9  * @author Wáng Chéng Dá
10  * @create 2017-02-28 15:16
11  */
12 public class MainIOC {
13 
14     public static void main(String[] args) {
15         ApplicationContext app = new ClassPathXmlApplicationContext("applicationConfig.xml");
16         Person person = (Person) app.getBean("person");
17         System.out.println(person);
18     }
19 }

 

控制台输出:

Person{name='张三', sex='男', age=23}

 

构造函数注入

通过构造函数注入 Bean 的属性值或依赖的对象 , 它保证了 Bean 实例在实例化后就可以使用。构造函数注入在 <constructor-arg> 元素里声明属性 , 没有 name 属性。

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 5 
 6     <bean id="person" class="com.itdjx.spring.dependency.injection.Person">
 7         <property name="name" value="张三" />
 8         <property name="age" value="23" />
 9         <property name="sex" value="男" />
10     </bean>
11 
12     <bean id="car" class="com.itdjx.spring.dependency.injection.Car">
13         <constructor-arg value="BaoMa"/>
14         <constructor-arg value="HuaChen"/>
15         <constructor-arg value="230000"/>
16     </bean>
17 
18 </beans>
 1 package com.itdjx.spring.dependency.injection;
 2 
 3 /**
 4  * 构造器注入
 5  *
 6  * @author Wáng Chéng Dá
 7  * @create 2017-02-28 15:32
 8  */
 9 public class Car {
10 
11     private String brand;
12 
13     private String address;
14 
15     private double price;
16 
17     private int maxSpeed;
18 
19 
20     public Car(String brand, String address, double price) {
21         this.brand = brand;
22         this.address = address;
23         this.price = price;
24     }
25 
26     public Car(String brand, String address, int maxSpeed) {
27         this.brand = brand;
28         this.address = address;
29         this.maxSpeed = maxSpeed;
30     }
31 
32     public Car(String brand, double price, int maxSpeed) {
33         this.brand = brand;
34         this.price = price;
35         this.maxSpeed = maxSpeed;
36     }
37 
38     public Car() {
39     }
40 
41     @Override
42     public String toString() {
43         return "Car{" +
44                 "brand='" + brand + '\'' +
45                 ", address='" + address + '\'' +
46                 ", price=" + price +
47                 ", maxSpeed=" + maxSpeed +
48                 '}';
49     }
50 }
 1 package com.itdjx.spring.dependency.injection;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 /**
 7  * 依赖注入main
 8  *
 9  * @author Wáng Chéng Dá
10  * @create 2017-02-28 15:16
11  */
12 public class MainIOC {
13 
14     public static void main(String[] args) {
15         ApplicationContext app = new ClassPathXmlApplicationContext("applicationConfig.xml");
16         Person person = (Person) app.getBean("person");
17         System.out.println(person);
18 
19         Car car = (Car) app.getBean("car");
20         System.out.println(car);
21     }
22 }

 

控制台输出:

Person{name='张三', sex='男', age=23}
Car{brand='BaoMa', address='HuaChen', price=0.0, maxSpeed=230000}

通过控制台输出我们看到 IOC 容器初始化调用的构造函数出现的匹配问题,匹配不到咱们想要调用的构造函数。这时候我们就得继续给 <constructor-arg> 中添加新的属性。分为:按索引匹配入参(index),按类型匹配入参(type) , 可混合使用,也可同时使用。

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 5 
 6     <bean id="person" class="com.itdjx.spring.dependency.injection.Person">
 7         <property name="name" value="张三" />
 8         <property name="age" value="23" />
 9         <property name="sex" value="男" />
10     </bean>
11 
12     <bean id="car" class="com.itdjx.spring.dependency.injection.Car">
13         <constructor-arg value="BaoMa"/>
14         <constructor-arg value="HuaChen"/>
15         <constructor-arg value="230000"/>
16     </bean>
17 
18     <bean id="car2" class="com.itdjx.spring.dependency.injection.Car">
19         <constructor-arg value="BaoMa" index="0"/>
20         <constructor-arg value="HuaChen" index="1"/>
21         <constructor-arg value="230000" index="2"/>
22     </bean>
23 
24     <bean id="car3" class="com.itdjx.spring.dependency.injection.Car">
25         <constructor-arg value="BaoMa" index="0"/>
26         <constructor-arg value="HuaChen" index="1"/>
27         <constructor-arg value="230000" index="2" type="double"/>
28     </bean>
29 
30     <bean id="car4" class="com.itdjx.spring.dependency.injection.Car">
31         <constructor-arg value="BaoMa" index="0"/>
32         <constructor-arg value="HuaChen" index="1"/>
33         <constructor-arg value="230" type="int"/>
34     </bean>
35 
36 </beans>
 1 package com.itdjx.spring.dependency.injection;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 /**
 7  * 依赖注入main
 8  *
 9  * @author Wáng Chéng Dá
10  * @create 2017-02-28 15:16
11  */
12 public class MainIOC {
13 
14     public static void main(String[] args) {
15         ApplicationContext app = new ClassPathXmlApplicationContext("applicationConfig.xml");
16         Person person = (Person) app.getBean("person");
17         System.out.println(person);
18         System.out.println("----------------------");
19 
20         Car car = (Car) app.getBean("car");
21         System.out.println(car);
22         System.out.println("----------------------");
23 
24         car = (Car) app.getBean("car2");
25         System.out.println(car);
26         System.out.println("----------------------");
27 
28         car = (Car) app.getBean("car3");
29         System.out.println(car);
30         System.out.println("----------------------");
31 
32         car = (Car) app.getBean("car4");
33         System.out.println(car);
34         System.out.println("----------------------");
35     }
36 }

 

控制台输出:

Person{name='张三', sex='男', age=23}
----------------------
Car{brand='BaoMa', address='HuaChen', price=0.0, maxSpeed=230000}
----------------------
Car{brand='BaoMa', address='HuaChen', price=0.0, maxSpeed=230000}
----------------------
Car{brand='BaoMa', address='HuaChen', price=230000.0, maxSpeed=0}
----------------------
Car{brand='BaoMa', address='HuaChen', price=0.0, maxSpeed=230}
----------------------

posted @ 2017-02-28 15:57  Chinda  阅读(406)  评论(0编辑  收藏  举报