Java教程

spring的生命周期(宏观角度)---上

本文主要是介绍spring的生命周期(宏观角度)---上,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

对于spring的生命周期,在宏观角度来看,可以分为实例化前、实例化、实例化后、初始化前、初始化、初始化后。

 

实例化前:通过实现BeanPostProcessor接口的postProcessBeforeInitialization方法,就可以在对象实例化之前进行操作

@Component
public class WxyBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if(beanName.equals("user")){
            System.out.println("实例化之前");
        }
        return null;
    }
}
@Component
public class User {
    public User(){
        System.out.println("实例化:调用构造方法");
    }
}

 

 可以从结果中看出来,在实例化之前进行了操作。

 

实例化:

@Component
public class UserService {
    private User user;

    public UserService(){
        System.out.println("无参数实例化");
    }

    public UserService(User user){
        System.out.println("1个参数实例化");
    }
}

调用的是无参数实例化

@Component
public class UserService {
    private User user;

    public UserService(User user){
        System.out.println("1个参数实例化");
    }

    public UserService(User user,User user1){
        System.out.println("2个参数实例化");
    }
}

因为没有指定,所以会报错

@Component
public class UserService {
    private User user;

    public UserService(User user){
        System.out.println("1个参数实例化");
    }
    @Autowired
    public UserService(User user,User user1){
        System.out.println("2个参数实例化");
    }
}

调用1个参数实例化的构造方法,因为进行了指定

@Component
public class UserService {
    private User user;

    @Autowired
    public UserService(User user){
        System.out.println("1个参数实例化");
    }
    @Autowired
    public UserService(User user,User user1){
        System.out.println("2个参数实例化");
    }
}

会报错,因为都进行了指定,系统不知道要选择哪个

@Component
public class UserService {
    private User user;

    @Autowired(required = false)
    public UserService(User user){
        System.out.println("1个参数实例化");
    }
    @Autowired(required = false)
    public UserService(User user,User user1){
        System.out.println("2个参数实例化");
    }
}

会调用2个参数的构造方法,因为required=false的意思表示非必要

 

 

@ComponentScan("wxy")
public class Config {
    @Bean
    public User user(){
        return new User();
    }
    @Bean
    public User user1(){
        return new User();
    }
}
@Component
public class UserService {
    private User user;


    public UserService(User user){
        System.out.println("1个参数实例化");
    }
    @Autowired(required = true)
    public UserService(User user,User user1){
        System.out.println("2个参数实例化");
    }
}

会调用2个参数实例化的构造方法,Autowired是先找类型,找到类型之后数量有多个,则开始找名字相同的。

 

@ComponentScan("wxy")
public class Config {
    @Bean
    public User user(){
        return new User();
    }
    @Bean
    public User user2(){
        return new User();
    }
}
@Component
public class UserService {
    private User user;


    public UserService(User user){
        System.out.println("1个参数实例化");
    }
    @Autowired(required = true)
    public UserService(User user,User user1){
        System.out.println("2个参数实例化");
    }
}

这样会报错,因为容器中有user、user2的bean,然后构造方法中有user1这是没有的。

 

============================================================

实例化后:

@Component
public class BeanPostProcessor1 implements InstantiationAwareBeanPostProcessor {

    //实例化前
    @Override
    public Object postProcessBeforeInstantiation(Class<?> aClass, String beanName) throws BeansException {

        return null;
    }

    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        if(beanName.equals("userService")){
            System.out.println("实例化后-----");
        }
        return true;
    }
}

这个是实现InstantiationAwareBeanPostProcessor 类的方法,postProcessAfterInstantiation是实例化后的方法,注意到它的返回值是boolean,false表示实例化后不需要走spring的属性注入,如果是true表示还需要属性注入。

@Component
public class UserService {


    private User user;


    @Autowired
    public void aaa(User user){
        System.out.println("set注入");
        this.user = user;
    }

}

 

 

    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        if(beanName.equals("userService")){
            System.out.println("实例化后-----");
        }
        return false;
    }

 

这篇关于spring的生命周期(宏观角度)---上的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!