首页 > 其他分享 >Spring9 - bean生命周期

Spring9 - bean生命周期

时间:2023-02-06 15:12:14浏览次数:38  
标签:username 生命周期 String id bean Spring9 password public

②修改类User

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.atguigu.spring6.bean.User" scope="prototype" 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>

④测试

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

⑤bean的后置处理器

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

创建bean的后置处理器:

package com.atguigu.spring6.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的后置处理器要放入IOC容器才能生效 -->
<bean id="myBeanProcessor" class="com.atguigu.spring6.process.MyBeanProcessor"/>

标签:username,生命周期,String,id,bean,Spring9,password,public
From: https://www.cnblogs.com/Ashen-/p/17095493.html

相关文章

  • Spring10 - FactoryBean
    FactoryBean①简介FactoryBean是Spring提供的一种整合第三方框架的常用机制。和普通的bean不同,配置一个FactoryBean类型的bean,在获取bean的时候得到的并不是class属性中......
  • BeanUtils源码解析
    ......
  • Kubernetes的 pod 重启策略、Pod状态、生命周期
    Pod的重启策略Pod的重启策略指的是当Pod中的容器终止退出后,重启容器的策略。需要注意的是,因为Docker容器的轻量级,重启容器的做法实际上是直接重建容器,所以容器中的数据将会......
  • Eolink神技之二:API全生命周期管理
    系列文章:Eolink神技之一:基于数据库智能生成API文档Eolink全API全生命周期管理解决的问题整个项目中的API管理是一个非常麻烦的事情,从代码接口文档管理到接口用例管理以......
  • 一对多与多对一的bean类
    N对一,对应对象。如:Student类对应Class类(一个学生对应一个班级)publicclassStudent{privateIntegersId;privateStringsName;privateIntegerag......
  • Java多线程02——线程的生命周期和状态调度
    1线程的生命周期在线程的生命周期中,要经过新建​​new​​、就绪​​runnable​​、运行​​running​​、阻塞​​blocked​​和死亡​​dead​​5种状态。当线程启动后,......
  • Maven9 - 生命周期
    生命周期作用为了让构建过程自动化完成,Maven设定了三个生命周期,生命周期中的每一个环节对应构建过程中的一个操作。三个生命周期生命周期名称作用各个环节C......
  • react生命周期
    总结-旧生命周期初始化阶段:由ReactDOM.render()触发---初次渲染constructor()componentWillMount()render()componentDidMount()===>常用一般在这个钩子中做一......
  • Spring6 DI 依赖注入--Bean属性赋值
    Spring6基于XML实现Bean管理(属性赋值)IOC和DI有什么区别:DI是IOC中的具体实现,DI表示依赖注入或注入属性,注入属性要在创建对象的基础之上完成依赖注入方法bean属性赋值方......
  • 686~687 Servlet执行原理 AND Servlet生命周期方法
    Servlet执行原理1.当服务器接受到客户端的请求后,会解析请求URL路径,获取访问的Servlet的资源路径2.查找web.xml文件,是否有对应的<url-pattern>标签体内容3.......