Spring之简单例子展示Bean的生命周期

①具体的生命周期过程

  • bean对象创建(调用无参构造器)
  • 给bean对象设置属性,依赖注入
  • bean对象初始化之前操作(由bean的后置处理器负责)
  • bean对象初始化(需在配置bean时指定初始化方法)
  • bean对象初始化之后操作(由bean的后置处理器负责)
  • bean对象就绪可以使用
  • bean对象销毁(需在配置bean时指定销毁方法)
  • IOC容器关闭

②修改类User

package com.jxz;

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;
    }

    public void initMethod(){
    
    
        System.out.println("生命周期:3、初始化");
    }
    public void destroyMethod(){
    
    
        System.out.println("生命周期:5、销毁");
    }
    @Override
    public String toString() {
    
    
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", age=" + age +
                '}';
    }
}

注意其中的initMethod()和destroyMethod(),可以通过配置bean指定为初始化和销毁的方法

③配置bean

<!-- 使用init-method属性指定初始化方法 -->
<!-- 使用destroy-method属性指定销毁方法 -->
<bean class="com.jxz.User" scope="singleton" init-method="initMethod" destroy-method="destroyMethod">
    <property name="id" value="1001"></property>
    <property name="username" value="admin"></property>
    <property name="password" value="123456"></property>
    <property name="age" value="23"></property>
</bean>
作用域对生命周期的影响

singleton : 获取IOC容器时执行123, 后续获取直接从4开始,容器关闭调用5

prototype:获取IOC容器不执行123,从容器中获取bean则1, 2, 3, 4 执行,容器关闭不会执行5 , 此作用域对象销毁方法需要用户自己处理。(踩了大坑)

执行 scope=“singleton”

package com.jxz;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class LifeCycleTest {
     
     
 @Test
 public void testLifeCycle(){
     
     
     ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("spring-lifecycle.xml");
//        User user = ioc.getBean(User.class);
//        System.out.println(user);
//        System.out.println("生命周期:4、通过IOC容器获取bean并使用");
//        ioc.close();
 }
}

输出:

生命周期:1、使用无参构造器创建对象
生命周期:2、依赖注入
生命周期:3、初始化

还是上面的代码,scope=“prototype”

输出:

无输出

④测试

package com.jxz;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class LifeCycleTest {
    
    
    @Test
    public void testLifeCycle(){
    
    
        ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("spring-lifecycle.xml");
        User user = ioc.getBean(User.class);
        System.out.println(user);
        System.out.println("生命周期:4、通过IOC容器获取bean并使用");
        ioc.close();
    }
}

输出:

生命周期:1、使用无参构造器创建对象
生命周期:2、依赖注入
生命周期:3、初始化
User{
    
    id=1001, username='admin', password='123456', age=23}
生命周期:4、通过IOC容器获取bean并使用
生命周期:5、销毁

⑤bean的后置处理器

bean的后置处理器会在生命周期的初始化前后添加额外的操作,需要实现BeanPostProcessor接口,且配置到IOC容器中,需要注意的是,容器只能配置一个后置处理器,bean后置处理器不是单独针对某一个bean生效,而是针对IOC容器中所有bean都会执行

创建bean的后置处理器:

package com.jxz.process;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class MyBeanProcessor implements BeanPostProcessor {
    
    
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    
    
        System.out.println("初始化前调用-------------"+ beanName + " = " + bean);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    
    
        System.out.println("初始化后调用-------------"+ beanName + " = " + bean);
        return bean;
    }
}

在IOC容器中配置后置处理器:

<bean class="com.jxz.process.MyBeanProcessor"></bean>

输出:

生命周期:1、使用无参构造器创建对象
生命周期:2、依赖注入
初始化前调用-------------com.jxz.User#0 = User{
    
    id=1001, username='admin', password='123456', age=23}
生命周期:3、初始化
初始化后调用-------------com.jxz.User#0 = User{
    
    id=1001, username='admin', password='123456', age=23}
User{
    
    id=1001, username='admin', password='123456', age=23}
生命周期:4、通过IOC容器获取bean并使用
生命周期:5、销毁

猜你喜欢

转载自blog.csdn.net/qq_44036439/article/details/129036485