Spring之Bean的生命周期过程中调用的方法

打印 上一主题 下一主题

主题 1021|帖子 1021|积分 3063

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
1。这个部分除了6,9都在这了

  1. package com.example.springbootdemo3.lifebeean;
  2. import org.springframework.beans.BeansException;
  3. import org.springframework.beans.factory.*;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.ApplicationContextAware;
  7. import org.springframework.stereotype.Component;
  8. import javax.annotation.PostConstruct;
  9. import javax.annotation.PreDestroy;
  10. /**
  11. * description
  12. *
  13. * @author PC 2025/02/27 20:26
  14. */
  15. @Component
  16. public class MyBeanLife implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean {
  17.     private String name;
  18.     // 构造函数,当Bean被实例化时调用
  19.     public MyBeanLife() {
  20.         System.out.println("1.调用了Bean的构造函数");
  21.     }
  22.     @Value("name")
  23.     public void setName(String name) {
  24.         this.name = name;
  25.         System.out.println("2.调用了Bean的set方法(属性的依赖注入)");
  26.     }
  27.     @Override
  28.     public void setBeanName(String s) {
  29.         System.out.println("3.调用了BeanNameAware的setBeanName方法,beanName:" + s);
  30.     }
  31.     @Override
  32.     public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
  33.         System.out.println("4.调用了BeanFactoryAware的setBeanFactory方法,beanFactory:" + beanFactory.toString());
  34.     }
  35.     @Override
  36.     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  37.         System.out.println("5.调用了ApplicationContextAware的setApplicationContext方法,applicationContext:" + applicationContext.toString());
  38.     }
  39.     @PostConstruct
  40.     public void postConstruct() {
  41.         System.out.println("7.调用了@PostConstruct注解的方法");
  42.     }
  43.     @Override
  44.     public void afterPropertiesSet() throws Exception {
  45.         System.out.println("8.调用了InitializingBean的afterPropertiesSet方法");
  46.     }
  47.     @PreDestroy
  48.     public void preDestroy() {
  49.         System.out.println("10.调用了@PreDestroy注解的方法");
  50.     }
  51.     @Override
  52.     public void destroy() throws Exception {
  53.         System.out.println("11.调用了DisposableBean的destroy方法");
  54.     }
  55. }
复制代码
2. 6和9部分

BeanPostProcessor的两个方法是所有Bean都会调用,除了实现这个接口的Bean,所以为了方便加了判定
  1. package com.example.springbootdemo3.lifebeean;
  2. import org.springframework.beans.BeansException;
  3. import org.springframework.beans.factory.config.BeanPostProcessor;
  4. import org.springframework.stereotype.Component;
  5. /**
  6. * description
  7. *
  8. * @author PC 2025/02/27 21:00
  9. */
  10. @Component
  11. public class MyBeanLife2 implements BeanPostProcessor {
  12.     @Override
  13.     public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
  14.         if (!beanName.equals("myBeanLife")) {
  15.             return bean;
  16.         }
  17.         System.out.println("6.调用了BeanPostProcessor的postProcessBeforeInitialization方法,beanName:" + beanName);
  18.         return bean;
  19.     }
  20.     @Override
  21.     public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
  22.         if (!beanName.equals("myBeanLife")) {
  23.             return bean;
  24.         }
  25.         System.out.println("9.调用了BeanPostProcessor的postProcessAfterInitialization方法,beanName:" + beanName);
  26.         return bean;
  27.     }
  28. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

伤心客

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表