Spring-BeanPostProcessor PostConstruct init InitializingBean 执行顺序

这篇具有很好参考价值的文章主要介绍了Spring-BeanPostProcessor PostConstruct init InitializingBean 执行顺序。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

执行顺序探究

新建一个对象用于测试

@Component
public class Student implements InitializingBean {
	private String name;
	private int age;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	/**
	 * InitializingBean 提供的在属性设置之后执行的方法
	 *
	 * @throws Exception
	 */
	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("====== Student afterPropertiesSet");
	}

	/**
	 * 初始化方法用于配置 bean inti
	 */
	public void init() {
		System.out.println("====== afterPropertiesSet init");
	}

	/**
	 * PostConstruct
	 */
	@PostConstruct
	public void post() {
		System.out.println("====== Student PostConstruct");
	}

}

新建一个TestBeanPostProcessor 实现 BeanPostProcessor

@Component
public class TestBeanPostProcessor implements BeanPostProcessor {
	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) {
		System.out.println("======== Student postProcessAfterInitialization");
		return bean;
	}
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("=============Student postProcessBeforeInitialization");
		return bean;
	}
}

通过注解的 方式进行测试

@Test
public void test3() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.liyong.learn");
Student bean = (Student) context.getBean("student");
System.out.println(String.format("ben is name{%s} age is {%s}", bean.getName(), bean.getAge()));
}

得到的结果如下:
Spring-BeanPostProcessor PostConstruct init InitializingBean 执行顺序,spring,java,spring,postconstruct,postprocessor
总结PostProcessBeforeInitialization -> PostConstruct -> InitializingBean -> PostProcessAfterInitialization
通过Bean.xml的方式

<bean id="student" class="com.liyong.learn.Student" init-method="init">
	<property name="name" value="english"></property>
	<property name="age" value="18"></property>
</bean>
<bean id="testBeanPostProcessor" class="com.liyong.learn.TestBeanPostProcessor"></bean>
@Test
public void test () {
// 指定XML路径
String path = "beans.xml";
// 创建DefaultListableBeanFactory工厂,这也就是Spring的基本容器
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
// 创建BeanDefinition阅读器
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
// 进行BeanDefinition注册流程
reader.loadBeanDefinitions(path);
beanFactory.addBeanPostProcessor((BeanPostProcessor) beanFactory.getBean("testBeanPostProcessor"));
// Bean实例创建流程
Student student = (Student) beanFactory.getBean("student");
System.out.println(String.format("bean attrs name {%s} age {%s}", student.getName(), student.getAge()));
}

执行结果如下:
Spring-BeanPostProcessor PostConstruct init InitializingBean 执行顺序,spring,java,spring,postconstruct,postprocessor
总结PostProcessBeforeInitialization -> InitializingBean -> initMethod -> PostProcessAfterInitialization 文章来源地址https://www.toymoban.com/news/detail-806513.html

到了这里,关于Spring-BeanPostProcessor PostConstruct init InitializingBean 执行顺序的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 深入了解 Spring BeanPostProcessor 的应用

    Spring框架的强大之处在于其提供了丰富的扩展点,其中之一是BeanPostProcessor接口。这个接口允许我们在Spring容器实例化bean之后、在调用初始化方法之前和之后对bean进行一些自定义的处理。在这篇文章中,我们将深入研究BeanPostProcessor的使用场景,并通过一个详细的例子演示如

    2024年01月25日
    浏览(27)
  • Spring FrameWork从入门到NB - BeanPostProcessor

    Spring框架之所以NB,BeanPostProcessor功不可没。BeanPostProcessor通过生命周期回调实现对Bean的增强比如,其实Spring的AOP功能主要就是通过BeanPostProcessor实现的,以及,Spring重要的自动装配功能,也是通过BeanPostProcessor实现的。 BeanPostProcessor生效原理 BeanPostProcessor指的是Spring容器中所

    2024年02月12日
    浏览(28)
  • spring管理的bean在静态方法使用技巧——@PostConstruct

            如果我们使用正常的 @Component 注解将一个类交由spring管理,然后在使用的时候用@Resource注入对象,在没有碰到静态方法的时候,是不会出现问题,但是如果出现了静态方法,这样的使用方法就会出现 空指针的异常 ,也就是你@Resource注入的对象是空         一

    2024年02月10日
    浏览(37)
  • Spring后置处理器BeanFactoryPostProcessor与BeanPostProcessor源码解析

    Spring有两种类型的后置处理器,分别是 BeanFactoryPostProcessor 和 BeanPostProcessor ,这里再贴出我画的 Spring 启动过程,可以看看这两种后置处理器在 Spring 启动过程中位置。 BeanFactoryPostProcessor 的 postProcessBeanFactory 方法在 Spring 容器启动时被调用,可以对整个容器中的 BeanDefinition (

    2024年02月13日
    浏览(31)
  • Mr. Cappuccino的第61杯咖啡——Spring之BeanPostProcessor

    BeanPostProcessor:Bean对象的后置处理器,负责对已创建好的bean对象进行加工处理; BeanPostProcessor中的两个核心方法: postProcessBeforeInitialization:在每个bean对象的初始化方法执行之前执行该方法,如InitializingBean的afterPropertiesSet方法; postProcessAfterInitialization:在每个bean对象的初始

    2024年02月12日
    浏览(26)
  • Spring高手之路6——Bean生命周期的扩展点:BeanPostProcessor

    在前一篇讲解生命周期的时候就可以讲解后置处理器了,但是内容比较多,还是分开来讲解。    BeanPostProcessor 的设计目标主要是提供一种扩展机制,让开发者可以在 Spring Bean 的初始化阶段进行自定义操作。这种设计理念主要体现了 Spring 的一种重要原则,即“开放封闭原

    2024年02月10日
    浏览(37)
  • Spring Boot 容器扩展BeanFactoryPostProcessor、BeanPostProcessor、 BeanDefinitionRegistryPostProcessor用法和解析

    之所以在企业级应用中Spring 到Spring Boot 一直立于不败之地,保持火热,其中很关键的一个点是,它的扩展简直太强大了,上一篇,我们聊了一下 Spring Boot整个生命周期中的扩展点,,比如 SpringApplicationRunListener 可以监听应用的各个阶段。本文我们聊聊,Spring 容器上的扩展。

    2024年02月11日
    浏览(27)
  • 每天使用Spring 框架,那你知道 lazy-init 懒加载原理吗?

    懒加载是Spring框架中的一个重要特性,它允许我们将bean的实例化推迟到第一次使用时。懒加载的主要用途是提高应用程序的启动性能,减少不必要的资源消耗。 在大型的应用程序中,有些bean可能只在特定的条件下才会被使用到。如果在应用程序启动时就实例化所有的bean,会

    2024年02月05日
    浏览(33)
  • [Spring5.3.2] Servlet[springmvc]的Servlet.init()引发异常, 解析类文件失败

    问题表现: 图中提到的问题: 例外情况 javax.servlet.ServletException: Servlet[springmvc]的Servlet.init()引发异常 根本原因 org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [D:apache-tomcat-9.0.69webappsch2_2WEB-INFclassescontrollerIndexController.class]; nested excep

    2024年02月02日
    浏览(22)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包