一、Spring的使用
1.导入jar包
2.编写实体类
Person.java
public class Person{ private String name; public void say(){
System.out.println("Hello," + name);
} public String getName(){
return name;
} public void setName(String name){
this.name = name;
} }
3.编写配置文件
<?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 -->
<bean id="person" class="com.kiwi.domain.Person">
<property name="name" value="Tom"/>
</bean> </beans>
4.编写测试类
Test.java
@Test
public void testHello(){
//1.创建Spring的IOC容器对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //2.从IOC容器获取bean实例
Person person = (Person)context.getBean("person"); //3.调用方法
person.say();
}
结果输出:
Hello,Tom
二、IOC容器
1.IOC概述
控制反转(Inverse of Control): 其思想是反转资源获取的方向。传统的资源查找方式是要求组件向容器发起请求查找资源。作为回应,容器适时的返回资源。而应用了IOC之后,则是容器主动地将资源推送给它所管理的组件,组件所要做的仅仅是选择一种合适的方式来接受资源。
2.BeanFactory和ApplicationContext
Spring通过一个配置文件描述了Bean及Bean之间的依赖关系,利用Java语言的反射功能实例化Bean并建立了Bean之间的依赖关系。Spring的IOC容器在完成这些底层工作的基础上,还提供了Bean实例缓存,生命周期管理、Bean实例代理、事件发布、资源装载等高级服务。
BeanFactory是Spring框架的基础设施,面向Spring本身。而ApplicationContext面向Spring框架的开发者,几乎所有的应用场合都直接使用ApplicationContext而非底层的BeanFactory。
ApplicationContext有两个主要的实现类:
(1)ClassPathXmlApplicationContext: 从类路径下加载配置文件。
(2)FileSystemXmlApplicationContext: 从文件系统加载配置文件。
ConfigurableApplicationContext实现了ApplicationContext,主要增加列两个方法: refresh()和close(),让ApplicationContext具有启动、刷新和关闭上下文的能力。
ApplicationContext在初始化上下文的时候就实例化了所有单例的Bean。
WebApplicationContext是专门为Web而准备的,它允许从相对于Web根目录的路径完成初始化工作。
三、依赖注入的3种方式
1.属性注入
属性注入即通过setXxx()方法注入Bean的属性值或依赖对象,由于属性注入方式具有可选择性和灵活性高的优点,因此属性注入是实际中最常采用的注入方式。
属性注入要求Bean提供一个默认的构造函数,并为需要注入的属性提供对应的Setter方法。Spring先调用Bean的默认构造函数实例化Bean对象,然后通过反射的方式调用Setter方法注入属性值。
Car.java
public class Car{ private String brand;
private String color;
private int price;
private int maxSpeed; @Override
public String toString(){
return "Car [brand=" + brand + ", color=" + color + ", price=" + price + ", maxSpeed=" + maxSpeed + "]";
} //省略 get()、set()方法....
}
applicationContext.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
class: bean的全类名,通过反射在IOC容器中创建Bean,所以要求Bean中必须有无参的构造器
id:bean的名称,是唯一的
-->
<bean id="car" class="com.kiwi.domain.Car">
<property name="brand" value="BMW"/>
<property name="color" value="Black"/>
<property name="price" value="800000"/>
<property name="maxSpeed" value="200"/>
</bean> </beans>
Test.java
@Test
public void testCar(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Car car = (Car)context.getBean("car");
System.out.println(car);
}
结果:
Car [brand=BMW, color=Black, price=800000, maxSpeed=200]
2.构造函数注入
构造函数注入是除属性之外的另一种常用的注入方式,它保证一些必要的属性在Bean实例化时就得的设置,并且确保了Bean实例在实例化之后就可以使用。
(1)按照索引匹配入参
<bean id="car" class="com.kiwi.domain.Car">
<constructor-arg index="0" value="BMW"/>
<constructor-arg index="1" value="Red"/>
<constructor-arg index="2" value="800000"/>
</bean>
(2)按照类型匹配入参
<bean id="car" class="com.kiwi.domain.Car">
<constructor-arg type="java.lang.String" value="BMW"/>
<constructor-arg type="java.lang.String" value="Red"/>
<constructor-arg type="int" value="200"/>
</bean>
3.工厂方法注入
工厂方法需要额外的类和代码,这些功能和业务是没有关系的,所以不推荐使用工厂方法的注入方式。