目录

Spring - Bean初始化有哪些方式?

李羽秋
李羽秋 2023年03月22日  ·  阅读 1,233

Spring - Bean初始化有哪些方式?

1. @PostConstruct

接口

public interface UserFactory {
    
}

实现类

public class DefaultUserFactory implements UserFactory {
    // 1.基于 @PostConstruct
    @PostConstruct
    public void init(){
        System.out.println("@PostConstruct: UserFactory 初始化中。。。");
    }
}

入口

@Configuration
public class BeanInitializationDemo {

    public static void main(String[] args) {
        //创建BeanFactory容器
        AnnotationConfigApplicationContext  applicationContext =  new AnnotationConfigApplicationContext();

        // 注册 Configuraiob Class
        applicationContext.register(BeanInitializationDemo.class);

        //启动Spring 应用上下文
        applicationContext.refresh();

        //依赖查找 UserFactory
        UserFactory userFactory = applicationContext.getBean(UserFactory.class);

        //关闭spring应用上下文
        applicationContext.close();
    }

    public UserFactory userFactory() {
        return new DefaultUserFactory();
    }

2. 实现InitialzingBean 接口的 afterPropertiesSet()方法

接口

public interface UserFactory {
    
}

实现类

public class DefaultUserFactory implements UserFactory, InitializingBean {

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("InitializingBean#afterPropertiesSet(): UserFactory 初始化中。。。。");
    }
}

入口

@Configuration
public class BeanInitializationDemo {

    public static void main(String[] args) {
        //创建BeanFactory容器
        AnnotationConfigApplicationContext  applicationContext =  new AnnotationConfigApplicationContext();

        // 注册 Configuraiob Class
        applicationContext.register(BeanInitializationDemo.class);

        //启动Spring 应用上下文
        applicationContext.refresh();

        //依赖查找 UserFactory
        UserFactory userFactory = applicationContext.getBean(UserFactory.class);

        //关闭spring应用上下文
        applicationContext.close();
    }

    public UserFactory userFactory() {
        return new DefaultUserFactory();
    }

3. 自定义初始化方法

接口

public interface UserFactory {
     public default User createUser(){
        return User.createUser();
    }

    void initUserFactory();
}

实现类

public class DefaultUserFactory implements UserFactory, InitializingBean {
 

    public  void initUserFactory() {
        System.out.println("自定义初始化 UserFactory 初始化中。。。。 ");
    }
    
}

入口

@Configuration
public class BeanInitializationDemo {

    public static void main(String[] args) {
        //创建BeanFactory容器
        AnnotationConfigApplicationContext  applicationContext =  new AnnotationConfigApplicationContext();

        // 注册 Configuraiob Class
        applicationContext.register(BeanInitializationDemo.class);

        //启动Spring 应用上下文
        applicationContext.refresh();

        //依赖查找 UserFactory
        UserFactory userFactory = applicationContext.getBean(UserFactory.class);

        //关闭spring应用上下文
        applicationContext.close();
    }

    @Bean(initMethod = "initUserFactory")
    public UserFactory userFactory() {
        return new DefaultUserFactory();
    }

}

4. 三种方法执行顺序

如果以上三种方式均在同一Bean中定义,那么这些方法的执行顺序是怎么样的。

public class DefaultUserFactory implements UserFactory, InitializingBean {
    // 1.基于 @PostConstruct
    @PostConstruct
    public void init(){
        System.out.println("@PostConstruct: UserFactory 初始化中。。。");
    }

    public  void initUserFactory() {
        System.out.println("自定义初始化 UserFactory 初始化中。。。。 ");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("InitializingBean#afterPropertiesSet(): UserFactory 初始化中。。。。");
    }
}

执行结果

image-20220722102525294

我可以看出@postConstruct在前,其次是实现initializingBean接口的方式,最后才是自定义的方式

分类: Spring
标签: