springboot中依赖对象实例化问题

一个问题,暴露出不少问题。然后解决这些问题。

一、问题描述

有个SpringBoot项目,启动的时候,读取某对象的属性报错,因为该对象为null。

这个对象使用了@Autowired注解,看上去,是对象实例化的顺序问题。即A里面使用了B,B由容器负责实例化,但A使用B的时候,B却还没来得及注入。

二、问题解决

1、A等B实例化之后再使用B
使用@PostConstruct注解,比如:

@Service
public class AImpl implements A {
    @Autowired
    B config;
    
	@PostConstruct
	void init(){
		if(config.isDebug){
            System.err.println("开启调试模式。。。");
		}
	}
}

注解@PostConstruct 的作用是,所有依赖的对象都实例化以后,自动执行本方法。上面例子中,当对象config实例化以后,系统将自动执行init()方法。注意加上 @PostConstruct注解 的方法,是系统自动执行,无须显式调用。并且这个方法不能带有参数。

对比一下改写之间的代码:

@Service
public class AImpl implements A {
    @Autowired
    B config;
    
    public AImpl(){
    	init();
    }

	void init(){
		if(config.isDebug){//报错,因为config == null
            System.err.println("开启调试模式。。。");
		}
	}
}

2、普通类使用静态方法使用B
普通类是不能直接使用 @Autowired注解 的。如果一个类想让它的属性使用@Autowired注解,那么它本身也应该由容器实例化,即类本身也要加上@Component 、@configration、@service、@Controller等注解。普通类的属性加上@Autowired,能编译,能运行,但永远都是null。

那如果普通类想使用这个由容器注入的对象,咋办呢?我在上面提到的项目中,采用静态方法获取该对象的方式,核心还是这个@PostConstruct注解。具体如下:

@Component
public class B {
    private static B config = null;
    
    private boolean debug;
    public boolean isDebug() {
        return debug;
    }
    public void setDebug(boolean debug) {
        this.debug = debug;
    }
    
	@PostConstruct
    void init() {
        config = this;
    }

    public static B getInstance() {
        if (config == null) {//纯粹防御一下,不写这句应该也可以
            config = new B();
        }
        return config;
    }
}

public class C {
	public void run(){
		if(B.getInstance().isDebug()){
			System.err.println("俺也开启调试模式。。。");
		}
	}
}

三、相关知识点

1、IoC容器
Spring的核心就是所谓的IoC容器。这个IoC容器主要负责实例化对象,或者说,是提供Bean的地方。IoC,控制反转。何谓控制反转?就是对象创建的控制权转移了。原本构造对象实例,都是我们在代码中,显式地new。而在Spring中,这些工作都由容器来完成,控制权转给了容器。容器根据类(Bean)的定义,结合Bean的实现类,构造出Bean实例。像我们代码中,

    @Autowired
    B config;

B的实例config,就由容器负责实现。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
IoC容器的意义
1)方便编码,提升开发效率

2)可以应对复杂的项目开发
类与类之间的依赖,如果数量少还好,很多的话,依赖关系非常复杂,不容易理清
在这里插入图片描述
3)修改比较容易,提高系统可修改性
IoC本质上是面向接口编程,在我们代码中,声明的是接口对象,由容器根据实现类进行注入,因此声明与实现是解耦的。万一需要切换实现类,那么修改的地方就少了许多。

4)节省资源
Spring中的bean默认都是单例的。当然也会有每次都创建一个新实例的情况,本人暂时还不是很清楚其中的细节。先记录下来:
在这里插入图片描述
容器容器,可以看做黑箱,装的是Bean实例,系统启动之初就自动生成,直接使用即可,犹如探囊取物,煞是方便。

2、@Component , @Repository , @ Controller , @Service , @Configration
都是Bean的注解,不同类型。

从广义上Spring注解可以分为两类:

1)一类是注册Bean,像@Component , @Repository , @ Controller , @Service , @Configration

这些注解就是用于注册Bean,由IoC容器在系统启动之初注入并任君取用。

2)一类是使用Bean,比如@Autowired , @Resource。

3、@Bean
上面说到,@Component , @Repository , @ Controller , @Service , @Configration都是Bean的注解,但SpringBoot也有一个@Bean注解。

@Component , @Repository , @ Controller , @Service , @Configration是类注解,用在类头;@Bean是方法注解,用在类的方法里(当然啦,类头也要添加@Component、@Service之类的注解才能起作用)。Spring的@Bean注解告诉方法,产生一个Bean对象,然后这个Bean对象交给Spring管理。标有@Bean注解的方法不能直接调用,而是采用依赖注入的方式实例化对象进行使用。

//
public class MyHello {
    public void init() {
        System.out.println("初始化...");
    }
    public void destroy() {
        System.out.println("销毁...");
    }
    public String get() {
        return "获取信息...";
    }
}
//
@Configuration
public class MyConfiguration {
	// init和destroy都是这个Bean的方法。
    @Bean(initMethod = "init", destroyMethod = "destroy")
    public MyHello hi() {
        return new MyHello();
    }
}
//
@RestController
public class HelloController {
	@Autowired
	private MyHello hello;
 
    @GetMapping("/test")
    public String test() {
    	logger.info(hello.get());
        return "OK";
    }
}

如果@Bean注解的方法存在参数,则系统会从Spring容器中根据类型注入(若有多个类型的的话则根据方法名按名称注入,没有找到就会报错)。如:

@Bean
public WebSocketClient webSocketClient(ShipConfig shipConfig) {
    this.shipConfig = shipConfig;
    WebSocketClient webSocketClient = new WebSocketClient(new URI(url), new Draft_6455()) {
    	。。。
    }
    return webSocketClient;
}

@Component
@ConfigurationProperties(prefix = "ship")
public class ShipConfig {
	。。。
}

我的一点疑问。Spring Boot容器会如何实例化这些类?是自动的吗,即使没有使用它们?答案是肯定的。
在这里插入图片描述

为什么要有这么个注解呢?原来,@Component , @Repository , @ Controller , @Service , @Configration针对的是本项目中的类,如果引用第三方类,又想让它交由IoC管理怎么办?这时就可以用@Bean。

参考资料:
Spring Boot教程(7) – 直观地理解Spring容器
Spring的IOC原理以及思维导图
大白话讲解Spring的@bean注解
Spring中bean的作用域与生命周期
如何正确控制springboot中bean的加载顺序总结
SpringBoot - @Bean注解详解

posted on 2022-03-24 17:40  左直拳  阅读(0)  评论(0)    收藏  举报  来源

导航