美文网首页
Bean的七个生命周期

Bean的七个生命周期

作者: YAOPRINCESS | 来源:发表于2020-10-07 13:28 被阅读0次

    结果

    image.png

    测试

    @Test
        public void testLife() {
            //低级容器BeanFactory 高级容器ApplicationContext
    //        BeanFactory
            //加载spring配置文件
            ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
            //获取配置创建的类 user.class是要返回的类型
            User user = applicationContext.getBean("user", User.class);
            System.out.println("4.获取对象");
            System.out.println(user);
            user.add();
            //执行close才会调用销毁方法
            applicationContext.close();
    
        }
    

    User

    package com.kang.pojo;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    /**
     * @author klr
     * @create 2020-10-05-14:47
     */
    @Data
    //@NoArgsConstructor
    @AllArgsConstructor
    public class User {
        private int id;
        private String name;
    
    
        public void setId(int id) {
            this.id = id;
            System.out.println("2.使用set方法给属性赋值");
        }
    
        public User() {
            System.out.println("1.执行无参构造创建实例");
        }
    
        public void add() {
            System.out.println("添加一个User");
        }
    
        public void init() {
            System.out.println("3.执行初始化方法");
        }
    
        public void destroy() {
            System.out.println("5.执行销毁方法");
        }
    }
    

    BeanPostProcessor

    package com.kang.beanpostprocessor;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.BeanPostProcessor;
    
    /**
     * @author klr
     * @create 2020-10-07-13:09
     */
    public class MyBeanPostProcessor implements BeanPostProcessor {
    
        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            System.out.println("初始化前");
            return null;
        }
    
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            System.out.println("初始化后");
            return null;
        }
    }
    

    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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
    <!--    配置User对象的创建-->
    <!--    p命名空间底层是set方法注入,不是有参构造注入,用有参构造器不行,只能用无参构造器-->
        <bean id="user" class="com.kang.pojo.User"  p:name="王五" p:id="4" init-method="init" destroy-method="destroy">
        </bean>
    
    <!--    后置处理器-->
        <bean id="myBeanProcessor" class="com.kang.beanpostprocessor.MyBeanPostProcessor"></bean>
    
    </beans>
    

    相关文章

      网友评论

          本文标题:Bean的七个生命周期

          本文链接:https://www.haomeiwen.com/subject/grefpktx.html