0
点赞
收藏
分享

微信扫一扫

bean的生命周期

穿裙子的程序员 2022-03-15 阅读 73

生命周期是从生成到销毁的过程

1.bean的生命周期 通过构造函数(不管是有参还是无参)来实例化bean

2.为bean的属性设置值 和对其他的bean引用(调用set方法)

3.调用bean的初始化的方法(需要进行配置) xml文件中加上init -method

<bean class="com.testdemo.Lession" id="lession"  init-method="initmethod">
  <property name="name" value="1000"></property>
</bean>

lession类中

  public  void initmethod()
    {
        System.out.println("执行初始化的方法");
    }

4.bean 可以使用(获取到了对象)

5.当容器关闭的时候,调用bean的销毁的办法(需要配置xml文件 和初始化一样)

lession类

package com.testdemo;

public class Lession {
    public  String name ;
     Lession()
     {
         System.out.println("调用了无参数构造方法,初始化对象");
     }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        System.out.println("调用了set方法");
        this.name = name;
    }
    public  void initmethod()
    {
        System.out.println("执行初始化的方法");
    }
    public  void distprymethod()
    {
        System.out.println("销毁了该实例");
    }

}

test

package com.testdemo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {
    public static void main( String [] args)
    {
       ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext("demo1.xml");
        Lession lession= context.getBean("lession",Lession.class);
        System.out.println("创建了实例对象");
        context.close();
    }
}

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:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<bean class="com.testdemo.Lession" id="lession"  init-method="initmethod" destroy-method="distprymethod">
  <property name="name" value="1000"></property>
</bean>
</beans>

完整的bean的声明周期:

应该加上两个后置处理器 即第3步和第5步

1.bean的生命周期 通过构造函数(不管是有参还是无参)来实例化bean

2.为bean的属性设置值 和对其他的bean引用(调用set方法)

3.在初始化之前执行的方法 postProcessBeforeInitialization

4.调用bean的初始化的方法(需要进行配置) xml文件中加上init -method

5.在初始化之后执行的方法 postProcessAfterInitialization

6.获取创建bean实例对象

7.执行销毁方法

xml文件中配置后置处理器之后, 会给xml文件中所有的bean类都配置后置处理器

<?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:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<bean class="com.testdemo.Lession" id="lession"  init-method="initmethod" destroy-method="distprymethod">
  <constructor-arg index="0" value="有参构造的参数"></constructor-arg>
  <property name="id" value="10"></property>
</bean>
  <bean id="books" class="com.testdemo.Books"></bean>
  <bean id="mybeanpost" class="com.testdemo.Mybeanpost"></bean>
</beans>

新建一个mybeanpost类,继承自BeanPostProcessor接口

package com.testdemo;

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

public class Mybeanpost implements BeanPostProcessor {

        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            System.out.println("初始化之前执行的方法");
            return bean;
        }

        @Override
       public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            System.out.println("初始化之后执行的方法");
            return bean;
        }
}

总结

写到这里也结束了,在文章最后放上一个小小的福利,以下为小编自己在学习过程中整理出的一个关于 java开发 的学习思路及方向。从事互联网开发,最主要的是要学好技术,而学习技术是一条慢长而艰苦的道路,不能靠一时激情,也不是熬几天几夜就能学好的,必须养成平时努力学习的习惯,更加需要准确的学习方向达到有效的学习效果。

由于内容较多就只放上一个大概的大纲,需要更及详细的学习思维导图的 点击我的Gitee获取。
还有 高级java全套视频教程 java进阶架构师 视频+资料+代码+面试题!

全方面的java进阶实践技术资料,并且还有技术大牛一起讨论交流解决问题。

举报

相关推荐

0 条评论