Spring IoC 容器
Spring ApplicationContext 容器
Application Context 是 spring 中较高级的容器。和 BeanFactory 类似,它可以加载配置文件中定义的 bean,将所有的 bean 集中在一起,当有请求的时候分配 bean。 另外,它增加了企业所需要的功能,比如,从属性文件中解析文本信息和将事件传递给所指定的监听器。这个容器在 org.springframework.context.ApplicationContext interface 接口中定义。
FileSystemXmlApplicationContext:该容器从 XML 文件中加载已被定义的 bean。在这里,你需要提供给构造器 XML 文件的完整路径。
ClassPathXmlApplicationContext:该容器从 XML 文件中加载已被定义的 bean。在这里,你不需要提供 XML 文件的完整路径,只需正确配置 CLASSPATH 环境变量即可,因为,容器会从 CLASSPATH 中搜索 bean 配置文件。
ApplicationContext context = new FileSystemXmlApplicationContext("springconfig.xml");
ApplicationContext context1 = new FileSystemXmlApplicationContext("E:/spring/src/springconfig.xml");
hellospring Hello = (hellospring)context.getBean("hellospring");
Hello.sayHello();
Spring Bean 定义
bean 的对象是构成应用程序的支柱也是由 Spring IoC 容器管理的。bean 是一个被实例化,组装,并通过 Spring IoC 容器所管理的对象。
属性 | 描述 |
---|---|
id | bean唯一标识符 |
class | 指定用来创建 bean 的 bean 类 |
init-method | 初始化回调函数 |
destroy-method | 析构回调函数 |
lazy-init | 延迟初始化的 bean 告诉 IoC 容器在它第一次被请求时,而不是在启动时去创建一个 bean 实例 |
<bean id="hellospring" class="hellospring" init-method=".." destroy-method=".." lazy-init="true">
<property name="name" value="zhw"></property>
</bean>
Spring Bean 作用域
当在 Spring 中定义一个 bean 时,你必须声明该 bean 的作用域的选项。例如,为了强制 Spring 在每次需要时都产生一个新的 bean 实例,你应该声明 bean 的作用域的属性为 prototype。同理,如果你想让 Spring 在每次需要时都返回同一个bean实例,你应该声明 bean 的作用域的属性为 singleton。
Spring 框架支持以下五个作用域,分别为singleton、prototype、request、session和global session
作用域 | 描述 |
---|---|
singleton | 在spring IoC容器仅存在一个Bean实例,Bean以单例方式存在,默认值 |
prototype | 每次从容器中调用Bean时,都返回一个新的实例,即每次调用getBean()时,相当于执行newXxxBean() |
request | 每次HTTP请求都会创建一个新的Bean,该作用域仅适用于WebApplicationContext环境 |
session | 同一个HTTP Session共享一个Bean,不同Session使用不同的Bean,仅适用于WebApplicationContext环境 |
global-session | 一般用于Portlet应用环境,该运用域仅适用于WebApplicationContext环境 |
Spring Bean 生命周期
当一个 bean 被实例化时,它可能需要执行一些初始化使它转换成可用状态。同样,当 bean 不再需要,并且从容器中移除时,可能需要做一些清除工作。
1.代码中实现InitializingBean接口,实现afterPropertiesSet()
2.代码中实现DisposableBean接口,实现destroy()
3.代码中实现初始化方法,例如init(),unInit(),xml文件中配置init-method属性,destroy-method属性
springconfig.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 id="hellospring" class="hellospring" scope="singleton" init-method=".." destroy-method="..">
<property name="name" value="zhw"></property>
</bean>
</beans>
hellospring.java
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class hellospring implements DisposableBean,InitializingBean {
private String name;
@Override
public void afterPropertiesSet() throws Exception {
}
@Override
public void destroy() throws Exception {
}
public void setName(String name) {
this.name = name;
}
public void sayHello(){
System.out.println("hello spring " + name);
}
}
Spring Bean 后置处理器
Bean 后置处理器允许在调用初始化方法前后对 Bean 进行额外的处理。BeanPostProcessor 接口定义回调方法,你可以实现该方法来提供自己的实例化逻辑,依赖解析逻辑等。ApplicationContext 会自动检测由 BeanPostProcessor 接口的实现定义的 bean,注册这些 bean 为后置处理器,然后通过在容器中创建 bean,在适当的时候调用它。
hellospring.java
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class hellospring{
private String name;
public void setName(String name) {
this.name = name;
}
public void getName(){
System.out.println("your message " + name);
}
public void init(){
System.out.println("spring init");
}
public void delete(){
System.out.println("spring destory");
}
}
inithellospring.java
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class inithellospring implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("before init "+beanName);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("after init "+beanName);
return bean;
}
}
springconfig.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 id="hellospring" class="hellospring" init-method="init" destroy-method="delete">
<property name="name" value="zhw"></property>
</bean>
<bean class="inithellospring"></bean>
</beans>
测试结果
Spring Bean 定义继承
bean 定义可以包含很多的配置信息,包括构造函数的参数,属性值,容器的具体信息例如初始化方法,静态工厂方法名。子 bean 的定义继承父定义的配置数据。子定义可以根据需要重写一些值,或者添加其他值。Spring Bean 定义的继承与 Java 类的继承无关,但是继承的概念是一样的。你可以定义一个父 bean 的定义作为模板和其他子 bean 就可以从父 bean 中继承所需的配置。当你使用基于 XML 的配置元数据时,通过使用父属性,指定父 bean 作为该属性的值来表明子 bean 的定义。
springconfig.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 id="benaTmp" class="benaTmp" abstract="true">
<property name="msg0" value="msg0"></property>
<property name="msg1" value="msg1"></property>
<property name="msg2" value="msg2"></property>
</bean>
<bean id="bean" class="bean" parent="benaTmp">
<property name="msg0" value="bean0"></property>
<property name="msg2" value="bean2"></property>
</bean>
</beans>
beanTmp.java
public class benaTmp {
private String msg0;
private String msg1;
private String msg2;
public void setMsg0(String msg0) {
this.msg0 = msg0;
}
public void setMsg1(String msg1) {
this.msg1 = msg1;
}
public void setMsg2(String msg2) {
this.msg2 = msg2;
}
}
bean.java
public class bean {
private String msg0;
private String msg1;
private String msg2;
public void setMsg0(String msg0) {
this.msg0 = msg0;
}
public void setMsg1(String msg1) {
this.msg1 = msg1;
}
public void setMsg2(String msg2) {
this.msg2 = msg2;
}
public String getMsg0() {
System.out.println(msg0);
return msg0;
}
public String getMsg1() {
System.out.println(msg1);
return msg1;
}
public String getMsg2() {
System.out.println(msg2);
return msg2;
}
}