Bean 初始化的方式与顺序

2021-01-26

Bean 在实例化完成后,可以配置初始化,方式如下:

  • @PostConstruct 注解作用于方法
  • 实现 InitializingBean 接口,复写 afterPropertiesSet 方法
  • 自定义 initMethod:xml <bean init-method="init">属性、@Bean(initMethod = "init")、BeanDefinitionBuilder#setInitMethodName

 

示例代码:

spring 配置文件

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="user1" class="constxiong.User" init-method="init">
        <property name="source" value="xml"/>
    </bean>

    <!-- 开启注解能力 -->
    <context:component-scan base-package="constxiong"/>

</beans>

 

User 类

package constxiong;

import org.springframework.beans.factory.InitializingBean;

import javax.annotation.PostConstruct;

public class User implements InitializingBean {

	private String source;

	private Integer id;
	
	private String name;
	
	public User() {
	}

	@PostConstruct
	public void postConstruct() {
		this.id = 1;
		this.name = "cx1";
		System.out.println(this);
		System.out.println(source + ": User#postConstruct executed\n");
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		this.id = 2;
		this.name = "cx2";
		System.out.println(this);
		System.out.println(source + ": User#afterPropertiesSet executed\n");
	}

	public void init() {
		this.id = 3;
		this.name = "cx3";
		System.out.println(this);
		System.out.println(source + ": User#init executed\n");
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

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

	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	@Override
	public String toString() {
		return "User{" +
				"source='" + source + '\'' +
				", id=" + id +
				", name='" + name + '\'' +
				'}';
	}
}

 

测试代码

package constxiong;


import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 测试 Bean 的初始化
 * @author ConstXiong
 */
@Configuration
public class Test {
	
	public static void main(String[] args) {
		// xml init
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring-bean-initialization.xml");
		User user1 = context.getBean("user1", User.class);
		System.out.println(user1);

		// @Bean
		User user2 = context.getBean("user2", User.class);
		System.out.println(user2);

		//BeanDefinition
		((DefaultListableBeanFactory)context.getBeanFactory()).registerBeanDefinition("user3",
				BeanDefinitionBuilder.rootBeanDefinition(User.class).setInitMethodName("init").addPropertyValue("source", "BeanDefinition").getBeanDefinition());
		User user3 = context.getBean("user3", User.class);
		System.out.println(user3);
	}

	@Bean(initMethod = "init", name = "user2")
	public User getUser() {
		User user = new User();
		user.setSource("@Bean");
		return user;
	}

}

 

打印结果

ConstXiong 备案号:苏ICP备16009629号-3