關于
SpringBoot
的自動裝配功能,相信是每一個
Java
程式員天天都會用到的一個功能,但是它究竟是如何實作的呢?今天阿粉來帶大家看一下。
自動裝配案例
首先我們通過一個案例來看一下自動裝配的效果,建立一個
SpringBoot
的項目,在
pom
檔案中加入下面的依賴。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
其中
web
的依賴表示我們這是一個
web
項目,
redis
的依賴就是我們這邊是要驗證的功能依賴。随後在
application.properties
配置檔案中增加
redis
的相關配置如下
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=123456
再編寫一個
Controller
和
Service
類,相關代碼如下。
package com.example.demo.controller;
import com.example.demo.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping(value = "/hello")
public String hello(@RequestParam("name"){
return helloService.sayHello(name);
}
}
service
代碼如下
package com.example.demo.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
@Service
public class HelloService {
@Autowired
RedisTemplate<String, String> redisTemplate;
public String sayHello(String name){
String result = doSomething(name);
redisTemplate.opsForValue().set("name", result);
result = redisTemplate.opsForValue().get("name");
return "hello: " + result;
}
private String doSomething(String name){
return name + " 歡迎關注 Java 極客技術";
}
}
啟動項目,然後我們通過通路 http://127.0.0.1:8080/hello?name=ziyou,可以看到正常通路。接下來我們再通過
Redis
的用戶端,去觀察一下資料是否正确的寫入到
Redis
中,效果跟我們想象的一緻。

自動裝配分析
看到這裡很多小夥伴就會說,這個寫法我天天都在使用,用起來是真的爽。雖然用起來是很爽,但是大家有沒有想過一個問題,那就是在我們的
HelloService
中通過
@Autowired
注入了一個
RedisTemplate
類,但是我們的代碼中并沒有寫過這個類,也沒有使用類似于
@RestControlle
r,
@Service
這樣的注解将
RedisTemplate
注入到
Spring IoC
容器中,那為什麼我們就可以通過
@Autowired
注解從
IoC
容器中擷取到
RedisTemplate
首先我們看下項目的啟動類,
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(value = "com.example.demo.*")
public class DemoApplication {
public static void main(String[] args){
SpringApplication.run(DemoApplication.class, args);
}
}
在啟動類上面有一個
@SpringBootApplication
注解,我們點進去可以看到如下内容
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
// 省略
}
在這個注解中,其中有一個
@EnableAutoConfiguration
注解,正是因為有了這樣一個注解,我們才得以實作自動裝配的功能。繼續往下面看。
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
Class<?>[] exclude() default {};
String[] excludeName() default {};
}
可以看到
@EnableAutoConfiguration
注解中有一個
@Import({AutoConfigurationImportSelector.class})
,導入了一個
AutoConfigurationImportSelector
類,該類間接實作了
ImportSelector
接口,實作了一個
String[] selectImports(AnnotationMetadata importingClassMetadata);
方法,這個方法的傳回值是一個字元串數組,對應的是一系列主要注入到
Spring IoC
容器中的類名。當在
@Import
中導入一個
ImportSelector
的實作類之後,會把該實作類中傳回的
Class
名稱都裝載到
IoC
容器中。
一旦被裝載到
IoC
容器中過後,我們在後續就可以通過
@Autowired
來進行使用了。接下來我們看下
selectImports
方法裡面的實作,當中引用了
getCandidateConfigurations
方法 ,其中的
ImportCandidates.load
方法我們可以看到是通過加載
String location = String.format("META-INF/spring/%s.imports", annotation.getName());
對應路徑下的
org.springframework.boot.autoconfigure.AutoConfiguration.imports
檔案,其中就包含了很多自動裝配的配置類。
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes){
List<String> configurations = new ArrayList(SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader()));
ImportCandidates.load(AutoConfiguration.class, this.getBeanClassLoader()).forEach(configurations::add);
Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories nor in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. If you are using a custom packaging, make sure that file is correct.");
return configurations;
}
我們可以看到這個檔案中有一個
RedisAutoConfiguration
配置類,在這個配置中就有我們需要的
RedisTemplate
類的
Bean
,同時也可以看到,在類上面有一個
@ConditionalOnClass({RedisOperations.class})
注解,表示隻要在類路徑上有
RedisOperations.class
這個類的時候才會進行執行個體化。這也就是為什麼隻要我們添加了依賴,就可以自動裝配的原因。
通過
org.springframework.boot.autoconfigure.AutoConfiguration.imports
這個檔案,我們可以看到有很多官方幫我們實作好了配置類,這些功能隻要我們在
pom
檔案中添加對應的
starter
依賴,然後做一些簡單的配置就可以直接使用。
其中本質上自動裝配的原理很簡單,本質上都需要實作一個配置類,隻不過這個配置類是官方幫我們建立好了,再加了一些條件類注解,讓對應的執行個體化隻發生類類路徑存在某些類的時候才會觸發。這個配置類跟我們平常自己通過
JavaConfig
形式編寫的配置類沒有本質的差別。