spring xml配置初始化执行方法_Spring注解设置Bean的初始化、销毁方法的方式,你知道几种?...

438410c7fe6b175a9a60def366b2c35d.png

bean的生命周期:创建---初始化---销毁。

Spring中声明的Bean的初始化和销毁方法有3种方式:

1. @Bean的注解的initMethod、DestroyMethod属性

2. bean实现InitializingBean、DisposableBean接口

3. @PostConstruct、@PreDestroy注解

4. BeanPostProcessor(这种仅仅增强了Bean的初始化方法)

@Bean的注解的initMethod、DestroyMethod属性

--cat类

public class Cat {

    public Cat() {
        System.out.println("cat...constructor...");
    }

    public void init() {
        System.out.println("cat...init...");
    }

    public void destroy() {
        System.out.println("cat...destory...");
    }
}

--配置类

@Configuration
public class SpringConfig {

    @Bean(name = "cat", initMethod = "init", destroyMethod = "destroy")
    public Cat getCat() {
        return new Cat();
    }
}

--测试代码

@Test
public void testLifeCycle() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
    System.out.println("IOC容器创建了...");
    context.close();
}

--测试结果

9dcd737aa5595cf73b6073a8f78f5b83.png

bean实现InitializingBean、DisposableBean接口

实现InitialzingBean接口的afterPropertiesSet方法,即为bean的初始化方法。

实现DisposableBean接口的destroy方法,即为bean的销毁方法。

--dog类

public class Dog implements InitializingBean, DisposableBean {

    public Dog() {
        System.out.println("dog...constructor...");
    }

    // Dog类的初始化方法
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("dog...afterPropertiesSet...");
    }

    // Dog类的销毁方法
    @Override
    public void destroy() throws Exception {
        System.out.println("dog...destroy...");
    }
}

--配置类

@Configuration
public class SpringConfig {

    @Bean(name = "dog")
    public Dog getDog() {
        return new Dog();
    }
}

--测试代码

@Test
public void testLifeCycle() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
    System.out.println("IOC容器创建了...");
    context.close();
}

--测试结果

f902a4a316398ee7f003397de3a07229.png

@PostConstruct、@PreDestroy注解

这两个注解只能标注在方法上,并且@PostConstruct注解标注的方法为bean的初始化方法,@PreDestroy标注的方法为bean的销毁方法。

--tiger类

public class Tiger {

    public Tiger() {
        System.out.println("tiger...constructor");
    }

    @PostConstruct
    public void init() {
        System.out.println("tiger...init...");
    }

    @PreDestroy
    public void destroy() {
        System.out.println("tiger...destroy...");
    }
}

--配置类

@Configuration
public class SpringConfig {

    @Bean(name = "tiger")
    public Tiger getTiger() {
        return new Tiger();
    }
}

--测试代码

@Test
public void testLifeCycle() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
    System.out.println("IOC容器创建了...");
    context.close();
}

--测试结果

ad5003bfcbd3d145a8d99eb0b8e47424.png

BeanPostProcessor

BeanPostProcessor是一个接口,这个接口中的postProcessBeforeInitialization、postProcessAfterInitialization方法分别在Bean的初始化方法执行的前后执行,本质上不算是声明Bean的初始化和销毁方法的一种,而是仅仅对bean的初始化方法的一个功能增强,并且在Spring中使用非常广泛,例如DI注解Autowired,当IOC容器调用无参构造创建对象之后,就会进行依赖注入,而且依赖注入在Bean的初始化方法执行之前,所以可以适用BeanPostProcessor来完成这一功能,Spring底层也是这么完成的。

--MyBeanPostProcessor

public class MyBeanPostProcessor implements BeanPostProcessor {

    /**
     * 在初始化之前执行
     * @param bean 初始化的bean
     * @param beanName bean的名字
     * @return 初始化的bean或者包装过的bean
     * @throws BeansException
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println(beanName + " ==> " + "postProcessBeforeInitialization...");
        return bean;
    }

    /**
     * 在初始化之后执行
     * @param bean 初始化的bean
     * @param beanName bean的名字
     * @return 初始化的bean或者包装过的bean
     * @throws BeansException
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println(beanName + " ==> " + "postProcessAfterInitialization...");
        return bean;
    }
}

--配置类

@Configuration
public class SpringConfig {

    @Bean(name = "tiger")
    public Tiger getTiger() {
        return new Tiger();
    }

    @Bean(name = "myBeanPostProcessor")
    public MyBeanPostProcessor getMyBeanPostProcessor() {
        return new MyBeanPostProcessor();
    }
}

--测试代码

@Test
public void testLifeCycle() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
    System.out.println("IOC容器创建了...");
    context.close();
}

--测试结果

32536784037712fdd6b9dccbdd9a5598.png
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值