首页 > 代码库 > Spring学习--引用其他Bean , 内部Bean

Spring学习--引用其他Bean , 内部Bean

引用其他Bean:

  1. 组成应用程序的 Bean 经常需要相互协作以完成应用程序的功能 , 要使 Bean 能够相互访问, 就必须在 Bean 配置文件中指定对 Bean 的引用。
  2. 在 Bean 的配置文件中 , 可以通过 <ref> 元素或 ref 属性为 Bean 的属性或者构造器参数指定对 Bean 的引用。
  3. 也可以在属性或者构造器里包含 Bean 的声明 , 这样的 Bean 称为内部 Bean。
 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         <property name="car" ref="car3"/>
11     </bean>
12 
13     <bean id="car3" class="com.itdjx.spring.dependency.injection.Car">
14         <constructor-arg value="BaoMa" index="0"/>
15         <constructor-arg value="HuaChen" index="1"/>
16         <constructor-arg value="230000" index="2" type="double"/>
17     </bean>
18 
19     <bean id="car4" class="com.itdjx.spring.dependency.injection.Car">
20         <constructor-arg value="BaoMa" index="0"/>
21         <constructor-arg value="HuaChen" index="1"/>
22         <constructor-arg value="230" type="int"/>
23     </bean>
24 
25 </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     private Car car;
18 
19     public String getName() {
20         return name;
21     }
22 
23     public void setName(String name) {
24         this.name = name;
25     }
26 
27     public String getSex() {
28         return sex;
29     }
30 
31     public void setSex(String sex) {
32         this.sex = sex;
33     }
34 
35     public int getAge() {
36         return age;
37     }
38 
39     public void setAge(int age) {
40         this.age = age;
41     }
42 
43 
44     public Car getCar() {
45         return car;
46     }
47 
48     public void setCar(Car car) {
49         this.car = car;
50     }
51 
52     @Override
53     public String toString() {
54         return "Person{" +
55                 "name=‘" + name + ‘\‘‘ +
56                 ", sex=‘" + sex + ‘\‘‘ +
57                 ", age=" + age +
58                 ", car=" + car +
59                 ‘}‘;
60     }
61 }
 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     }
20 }

 

控制台输出:

Person{name=‘张三‘, sex=‘男‘, age=23, car=Car{brand=‘BaoMa‘, address=‘HuaChen‘, price=230000.0, maxSpeed=0}}

 

内部Bean:

 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="person2" class="com.itdjx.spring.dependency.injection.Person">
 7         <property name="name" value="李玉"/>
 8         <property name="age" value="23"/>
 9         <property name="sex" value="女"/>
10         <property name="car" >
11             <bean class="com.itdjx.spring.dependency.injection.Car">
12                 <constructor-arg value="Ferrari" index="0"/>
13                 <constructor-arg value="Italy" index="1"/>
14                 <constructor-arg value="22500000" type="double"/>
15             </bean>
16         </property>
17     </bean>
18 
19 </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       
17         Person person = (Person) app.getBean("person2");
18         System.out.println(person);
19      
20     }
21 }

 

控制台输出:

Person{name=‘李玉‘, sex=‘女‘, age=23, car=Car{brand=‘Ferrari‘, address=‘Italy‘, price=2.25E7, maxSpeed=0}}

Spring学习--引用其他Bean , 内部Bean