bean的生命周期
具体的生命周期过程
1bean对象创建(调用无参构造器) 2给bean对象设置属性 3bean对象初始化之前操作(由bean的后置处理器负责) 4bean对象初始化(需在配置bean时指定初始化方法) 5bean对象初始化之后操作(由bean的后置处理器负责) 6bean对象就绪可以使用 7bean对象销毁(需在配置bean时指定销毁方法) 8IOC容器关闭 User 类
package com.atguigu.spring.pojo;
public class User {
private Integer id;
private String username;
private String password;
private Integer age;
public User() {
System.out.println("生命周期1实例化");
}
public User(Integer id, String username, String password, Integer age) {
this.id = id;
this.username = username;
this.password = password;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
System.out.println("生命周期2依赖注入");
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", age=" + age +
'}';
}
public void initMethod(){
System.out.println("生命周期3初始化");
}
public void destoryMethod(){
System.out.println("生命周期3销毁");
}
}
测试类
package com.test;
import com.atguigu.spring.pojo.User;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class LifecycleTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("Spring-lifecycle.xml");
User user = classPathXmlApplicationContext.getBean("user", User.class);
System.out.println(user);
classPathXmlApplicationContext.close();
}
}
spring-lifecycle.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="user" class="com.atguigu.spring.pojo.User" init-method="initMethod" destroy-method="destoryMethod">
<property name="id" value="1"></property>
<property name="age" value="23"></property>
<property name="password" value="1234"></property>
<property name="username" value="liuxaing"></property>
</bean>
</beans>
生命周期的测试结果
bean的后置处理器
bean的后置处理器会在生命周期的初始化前后添加额外的操作,需要实现BeanPostProcessor接口,且配置到IOC容器中,需要注意的是,bean后置处理器不是单独针对某一个bean生效,而是针对IOC容器中所有bean都会执行。
package com.atguigu.spring.process;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class mybeanprocess implements BeanPostProcessor{
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("初始化之前bean的后期处理器postProcessBeforeInitialization");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("初始化之后bean的后期处理器postProcessAfterInitialization");
return bean;
}
}
生命周期配置了后置处理器的测试结果
FactoryBean
FactoryBean是Spring提供的一种整合第三方框架的常用机制。和普通的bean不同,配置一个FactoryBean类型的bean,在获取bean的时候得到的并不是class属性中配置的这个类的对象,而是getObject()方法的返回值。通过这种机制,Spring可以帮我们把复杂组件创建的详细过程和繁琐细节都 屏蔽起来,只把最简洁的使用界面展示给我们.
创建类UserFactoryBean
把这个工厂类交由springIOC管理,返回的是这个工厂要创建的类。 实现的FactoryBean接口,所需要的泛型是工厂类管理对象的类型。 重写三个方法 getObject()返回工厂创建的类,而不是工厂类本身 getObjectType(),返回工厂创建的类的类型 isSingleton(),决定了这个工厂创建的类是单例还是多例 true是单例,false是多例
package com.atguigu.spring.factory;
import com.atguigu.spring.pojo.User;
import org.springframework.beans.factory.FactoryBean;
public class userfactorybean implements FactoryBean<User> {
@Override
public User getObject() throws Exception {
return new User();
}
@Override
public Class<?> getObjectType() {
return User.class;
}
@Override
public boolean isSingleton() {
return false;
}
}
spring-factory.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="com.atguigu.spring.factory.userfactorybean"></bean>
</beans>
测试类
package com.test;
import com.atguigu.spring.pojo.User;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class FactoryBeanTest {
@Test
public void testFactory(){
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("Spring-factory.xml");
User user = ioc.getBean(User.class);
User user1 = ioc.getBean(User.class);
System.out.println(user==user1);
}
}
IOC管理工厂类
|