1. 两种类型的 IOC 容器实现
Spring BeanFactory 容器
BeanFactory,以 Factory 结尾,表示它是一个工厂类(接口 ),用于管理 Bean 的一个工厂。在 Spring 中,BeanFactory 是 IOC 容器的核心接口 BeanFactory 是 Spring 框架的基础设施,面向 Spring 本身。 它的职责包括:实例化、定位、配置应用程序中的对象及建立这些对象间的依赖。 Spring ApplicationContext 容器
Application Context 是 BeanFactory 的子接口,也被成为 Spring 上下文。 ApplicationContext 面向使用 Spring 框架的开发者,几乎所有的应用场合都直接使用 ApplicationContext 而非底层的 BeanFactory 。 ApplicationContext 的主要实现类:
ClassPathXmlApplicationContext :从类路径下加载配置文件(用的最多 )FileSystemXmlApplicationContext: 从文件系统中加载配置文件 WebXmlApplicationContext:该容器会在一个 web 应用程序的范围内加载在 XML 文件中已被定义的 bean
2. 配置 Bean
1. javaBean 类的设计
Person 类
package www. xq. spring. beans;
public class Person {
private String name;
private int age;
private Car car;
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
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;
}
public String toString ( ) {
return "Person [name=" + name + ", age=" + age + ", car=" + car + "]" ;
}
public Person ( ) {
super ( ) ;
}
public Person ( String name, int age, Car car) {
super ( ) ;
this . name = name;
this . age = age;
this . car = car;
}
}
Car类
public class Car {
private String brand;
private String crop;
private double price;
private int maxSpeed;
public Car ( ) {
super ( ) ;
}
public Car ( String brand, String crop, double price, int maxSpeed) {
super ( ) ;
this . brand = brand;
this . crop = crop;
this . price = price;
this . maxSpeed = maxSpeed;
}
public String getBrand ( ) {
return brand;
}
public void setBrand ( String brand) {
this . brand = brand;
}
public String getCrop ( ) {
return crop;
}
public void setCrop ( String crop) {
this . crop = crop;
}
public double getPrice ( ) {
return price;
}
public int getMaxSpeed ( ) {
return maxSpeed;
}
public void setPrice ( double price) {
this . price = price;
}
public void setMaxSpeed ( int maxSpeed) {
this . maxSpeed = maxSpeed;
}
public String toString ( ) {
return "Car [brand=" + brand + ", crop=" + crop + ", price=" + price + ", maxSpeed=" + maxSpeed + "]" ;
}
}
2. spring 配置文件配置 bean
配置形式:基于 XML 文件的方式 Bean 的配置方式:通过全类名(反射) bean 的属性
class:bean 的全类名,通过反射的方式在 IOC 容器中创建 Bean.所以要求 Bean 中必须有无参数的构造器 id:标识容器中的 bean.id 唯一.
< bean id = " person" class = " www.xq.spring.beans.Person" >
< property name = " name" value = " Spring" > </ property>
< property name = " age" value = " 4" > </ property>
</ bean>
3. 测试
public static void main ( String[ ] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext ( "applicationContext.xml" ) ;
Person person = ( Person) ctx. getBean ( "person" ) ;
System. out. println ( person) ;
}
4. 运行结果
Person [name=Spring, age=4, car=null]
3. 依赖注入的方式
1. 属性注入(最常用)
< bean id = " car" class = " www.xq.spring.beans.Car" >
< property name = " brand" value = " Baoma" > </ property>
< property name = " crop" value = " WuHan" > </ property>
< property name = " price" value = " 500000" > </ property>
< property name = " maxSpeed" value = " 240" > </ property>
</ bean>
2. 构造器注入
< bean id = " car2" class = " www.xq.spring.beans.Car" >
< constructor-arg value = " Audi" index = " 0" > </ constructor-arg>
< constructor-arg value = " ShangHai" index = " 1" > </ constructor-arg>
< constructor-arg value = " 500000" index = " 2" > </ constructor-arg>
< constructor-arg value = " 220" index = " 3" > </ constructor-arg>
</ bean>
< bean id = " car3" class = " www.xq.spring.beans.Car" >
< constructor-arg value = " DaZhong" type = " java.lang.String" > </ constructor-arg>
< constructor-arg value = " Gunagzhou" type = " java.lang.String" > </ constructor-arg>
< constructor-arg value = " 200000" type = " double" > </ constructor-arg>
< constructor-arg value = " 200" type = " int" > </ constructor-arg>
</ bean>
3.测试
public static void main ( String[ ] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext ( "applicationContext.xml" ) ;
Car car = ( Car) ctx. getBean ( "car" ) ;
System. out. println ( car) ;
car = ( Car) ctx. getBean ( "car2" ) ;
System. out. println ( car) ;
car = ( Car) ctx. getBean ( "car3" ) ;
System. out. println ( car) ;
}
4. 运行结果
Car [brand=Baoma, crop=WuHan, price=500000.0, maxSpeed=240]
Car [brand=Audi, crop=ShangHai, price=500000.0, maxSpeed=220]
Car [brand=DaZhong, crop=Gunagzhou, price=200000.0, maxSpeed=200]
5. 总结
属性注入
属性注入即通过 setter 方法注入Bean 的属性值或依赖的对象 属性注入使用 元素, 使用 name 属性指定 Bean 的属性名称,value 属性或 子节点指定属性值 属性注入是实际应用中最常用的注入方式 构造方法注入
通过构造方法注入Bean 的属性值或依赖的对象,它保证了 Bean 实例在实例化后就可以使用。 构造器注入在 元素里声明属性