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

时间:2023-03-08 15:37:46

引用其他Bean

  1. 组成应用程序的 Bean 经常需要相互协作以完成应用程序的功能 , 要使 Bean 能够相互访问, 就必须在 Bean 配置文件中指定对 Bean 的引用。
  2. 在 Bean 的配置文件中 , 可以通过 <ref> 元素或 ref 属性为 Bean 的属性或者构造器参数指定对 Bean 的引用。
  3. 也可以在属性或者构造器里包含 Bean 的声明 , 这样的 Bean 称为内部 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="person" class="com.itdjx.spring.dependency.injection.Person">
<property name="name" value="张三"/>
<property name="age" value="23"/>
<property name="sex" value="男"/>
<property name="car" ref="car3"/>
</bean> <bean id="car3" class="com.itdjx.spring.dependency.injection.Car">
<constructor-arg value="BaoMa" index="0"/>
<constructor-arg value="HuaChen" index="1"/>
<constructor-arg value="230000" index="2" type="double"/>
</bean> <bean id="car4" class="com.itdjx.spring.dependency.injection.Car">
<constructor-arg value="BaoMa" index="0"/>
<constructor-arg value="HuaChen" index="1"/>
<constructor-arg value="230" type="int"/>
</bean> </beans>
 package com.itdjx.spring.dependency.injection;

 /**
* 属性注入
*
* @author Wáng Chéng Dá
* @create 2017-02-28 15:14
*/
public class Person { private String name; private String sex; private int age; private Car car; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public Car getCar() {
return car;
} public void setCar(Car car) {
this.car = car;
} @Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", car=" + car +
'}';
}
}
 package com.itdjx.spring.dependency.injection;

 /**
* 构造器注入
*
* @author Wáng Chéng Dá
* @create 2017-02-28 15:32
*/
public class Car { private String brand; private String address; private double price; private int maxSpeed; public Car(String brand, String address, double price) {
this.brand = brand;
this.address = address;
this.price = price;
} public Car(String brand, String address, int maxSpeed) {
this.brand = brand;
this.address = address;
this.maxSpeed = maxSpeed;
} public Car(String brand, double price, int maxSpeed) {
this.brand = brand;
this.price = price;
this.maxSpeed = maxSpeed;
} public Car() {
} @Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", address='" + address + '\'' +
", price=" + price +
", maxSpeed=" + maxSpeed +
'}';
}
}
 package com.itdjx.spring.dependency.injection;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* 依赖注入main
*
* @author Wáng Chéng Dá
* @create 2017-02-28 15:16
*/
public class MainIOC { public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationConfig.xml");
Person person = (Person) app.getBean("person");
System.out.println(person); }
}

控制台输出:

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

内部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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="person2" class="com.itdjx.spring.dependency.injection.Person">
<property name="name" value="李玉"/>
<property name="age" value="23"/>
<property name="sex" value="女"/>
<property name="car" >
<bean class="com.itdjx.spring.dependency.injection.Car">
<constructor-arg value="Ferrari" index="0"/>
<constructor-arg value="Italy" index="1"/>
<constructor-arg value="22500000" type="double"/>
</bean>
</property>
</bean> </beans>
 package com.itdjx.spring.dependency.injection;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* 依赖注入main
*
* @author Wáng Chéng Dá
* @create 2017-02-28 15:16
*/
public class MainIOC { public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationConfig.xml"); Person person = (Person) app.getBean("person2");
System.out.println(person); }
}

控制台输出:

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