bean的生命周期
- Spring IOC容器可以管理bean的生命周期,Spring允许在bean生命周期内特定的时间点执行指定的任务。
- Spring IOC容器对bean的生命周期进行管理的过程:
① 通过构造器或工厂方法创建bean实例
② 为bean的属性设置值和对其他bean的引用
③ 调用bean的初始化方法
④ bean可以使用了
⑤ 当容器关闭时,调用bean的销毁方法 - 在配置bean时,通过init-method和destroy-method 属性为bean指定初始化和销毁方法
- bean的后置处理器
① bean后置处理器允许在调用初始化方法前后对bean进行额外的处理
② bean后置处理器对IOC容器里的所有bean实例逐一处理,而非单一实例。
其典型应用是:检查bean属性的正确性或根据特定的标准更改bean的属性。
③ bean后置处理器需要实现接口:
org.springframework.beans.factory.config.BeanPostProcessor。在初始化方法被调用前后,Spring将把每个bean实例分别传递给上述接口的以下两个方法:
●postProcessBeforeInitialization(Object, String)
●postProcessAfterInitialization(Object, String) - 添加bean后置处理器后bean的生命周期
①通过构造器或工厂方法创建bean实例
②为bean的属性设置值和对其他bean的引用
③将bean实例传递给bean后置处理器的postProcessBeforeInitialization()方法
④调用bean的初始化方法
⑤将bean实例传递给bean后置处理器的postProcessAfterInitialization()方法
⑥bean可以使用了
⑦当容器关闭时调用bean的销毁方法
使用外部的属性文件
- 创建properties属性文件
- 引入context名称空间
- 指定properties属性文件的位置
- 从properties属性文件中引入属性值
AfterHandler.java
package com.spring2;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class AfterHandler implements BeanPostProcessor{
@Override
public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {
student st=(student)arg0;
if(st.getId().equals("1"))
st.setName("小明");
else
st.setName("小红");
return st;
}
@Override
public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException {
return arg0;
}
}
student.java
package com.spring2;
public class student {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "student [id=" + id + ", name=" + name + "]";
}
public student(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
public student() {
super();
}
}
com.spring2.test.java
package com.spring2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("life.xml");
student s1=ac.getBean("s1", student.class);
System.out.println(s1);
}
}
com.spring2.db.test.java
package com.spring2.db;
import java.sql.SQLException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.alibaba.druid.pool.DruidDataSource;
public class test {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("db.xml");
DruidDataSource bean=ac.getBean("dataSource",DruidDataSource.class);
try {
System.out.println(bean.getConnection());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/sms
jdbc.name=root
jdbc.psaaword=123456
db.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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- 引用外部文件使用 -->
<!-- 直接使用 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="user"/>
<property name="password" value="chenqing"/>
<property name="Url" value="jdbc:mysql://localhost:3306/sms"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
</bean>
<!-- 加载资源文件1 -->
<bean class="org.springframework,beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="db.properties"></property>
</bean>
<!-- 加载资源文件2 -->
<context:property-placeholder location="db.properties"/>
<bean id="dataSource2" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="${jdbc.name}"/>
<property name="password" value="${jdbc.password}"/>
<property name="Url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
</bean>
</beans>
life.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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<bean id="s1" class="com.spring2.student">
<property name="id" value ="1"></property>
</bean>
<bean class="com.spring2.AfterHandler"></bean>
</beans>