Spring Boot 自動(dòng)裝配的幾種用法示例小結(jié)
說明:自動(dòng)裝配是Spring Boot框架的一大特點(diǎn),簡(jiǎn)單說,是項(xiàng)目啟動(dòng)時(shí),自動(dòng)創(chuàng)建一系列Bean對(duì)象。
本文介紹幾個(gè)借助Spring Boot自動(dòng)裝配的用法。
基礎(chǔ)用法
用法一:基本操作
最基礎(chǔ)的用法是用 @Bean 注解,手動(dòng)將對(duì)象放入到 Spring Boot 的IOC容器中,其他地方就可以使用 @Autowired 注解直接使用該對(duì)象。
如下:
(某個(gè)類對(duì)象,注意類上沒有額外加注解)
public class DemoService {
public String test() {
return "test";
}
}(將對(duì)象放入到 IOC 容器中)
import com.hezy.service.DemoService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AutoBeanConfiguration {
@Bean
public DemoService testBean() {
return new DemoService();
}
}(直接注入使用)
import com.hezy.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/demo")
public class DemoController {
@Autowired
private DemoService demoService ;
@GetMapping
public String sayHello() {
return demoService.test();
}
}啟動(dòng)項(xiàng)目,調(diào)用,正常返回結(jié)果

另外,使用 @Bean 注解將對(duì)象放入 IOC 容器,需要配合類上的 @Configuration 注解,所以通常來說,我們都會(huì)創(chuàng)建一個(gè)配置類,將需要實(shí)例化的 Bean 對(duì)象都放入到這個(gè)配置類中,最后再使用服務(wù)注冊(cè)的方式,將這個(gè)配置的全限定類名放到 META-INF 文件下的 org.springframework.boot.autoconfigure.AutoConfiguration.imports 文件里,這樣其他引入該模塊的其他模塊,就能直接注入該模塊的 Bean 對(duì)象。
用法二:獲取指定Service
在一些場(chǎng)景,比如將數(shù)據(jù)生成文件(excel、word、pdf),我們需要根據(jù)配置來調(diào)用指定的 Service 實(shí)現(xiàn)類(ExcelServiceImpl、WordServiceImpl、PdfServiceImpl),我們通常的做法是給這些 Service 設(shè)置一個(gè)名稱,然后使用 applicationContext 的 ap i獲取指定實(shí)現(xiàn)類調(diào)用。
如下:
(創(chuàng)建一個(gè)寫服務(wù)接口,其他具體實(shí)現(xiàn)類實(shí)現(xiàn)該接口)
/**
* 寫服務(wù)接口
*/
public interface WriteService {
String write(String content);
}(excel實(shí)現(xiàn)類)
import org.springframework.stereotype.Service;
/**
* excel實(shí)現(xiàn)類
*/
@Service("excel")
public class ExcelServiceIImpl implements WriteService {
@Override
public String write(String content) {
return "excel:" + content;
}
}(word實(shí)現(xiàn)類)
import org.springframework.stereotype.Service;
/**
* word實(shí)現(xiàn)類
*/
@Service("word")
public class WordServiceImpl implements WriteService {
@Override
public String write(String content) {
return "word:" + content;
}
}(pdf實(shí)現(xiàn)類)
import org.springframework.stereotype.Service;
/**
* pdf實(shí)現(xiàn)類
*/
@Service("pdf")
public class PdfServiceImpl implements WriteService {
@Override
public String write(String content) {
return "pdf:" + content;
}
}(使用,使用 applicationContext 的 getBean方法獲取指定實(shí)現(xiàn)類調(diào)用其方法執(zhí)行)
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
@Service
public class TestService {
@Autowired
private ApplicationContext applicationContext;
public String test(String type, String content) {
return applicationContext.getBean(type, WriteService.class).write(content);
}
}(具體使用哪一個(gè)實(shí)現(xiàn)類,來自前端傳遞的參數(shù))
@Autowired
private TestService testService;
@GetMapping("/write")
public String write(String type, String content) {
return testService.test(type, content);
}啟動(dòng)項(xiàng)目,傳遞執(zhí)行寫操作的類型及內(nèi)容,如下。這樣就很方便了,是策略模式的一種體現(xiàn)。

高級(jí)用法
用法一:獲取某接口的所有實(shí)現(xiàn)類
假設(shè)一個(gè)場(chǎng)景,我們需要獲取某接口的所有實(shí)現(xiàn)類,依次執(zhí)行,返回最終結(jié)果。這時(shí)我們可以通過下面這種方式,直接獲取所有實(shí)現(xiàn)類的集合:
@Autowired
private List<WriteService> writeServices;
public String test2() {
return writeServices.size() + "";
}啟動(dòng)項(xiàng)目,調(diào)用接口,打斷點(diǎn)查看集合,可見該接口的所有實(shí)現(xiàn)類

這種用法場(chǎng)景挺多的,作者在實(shí)際開發(fā)中也遇到過。可以參看下面這兩篇博客介紹:
用法二:獲取具體實(shí)現(xiàn)類的Map映射
在前面基礎(chǔ)用法中,我們提到獲取指定 Service 可以使用 applicationContext 的 api,其實(shí)更簡(jiǎn)單的可以直接使用下面這種方式:
@Autowired
private Map<String, WriteService> writeServiceMap;
public String test3() {
return writeServiceMap.size() + "";
}啟動(dòng)項(xiàng)目,斷點(diǎn)打在這里,可以看到 Map 里面存的 key 是 Bean 名稱,value 是具體實(shí)現(xiàn)類

所以基礎(chǔ)用法的代碼,可以修改如下:
@Autowired
private Map<String, WriteService> writeServiceMap;
public String test(String type, String content) {
return writeServiceMap.get(type).write(content);
}這種寫法是不是更簡(jiǎn)潔、優(yōu)雅

總結(jié)
本文介紹了Spring Boot 自動(dòng)裝配的幾種用法。
到此這篇關(guān)于Spring Boot 自動(dòng)裝配的幾種用法示例小結(jié)的文章就介紹到這了,更多相關(guān)springboot自動(dòng)裝配內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot實(shí)現(xiàn)PDF添加水印的三種方法
本文主要介紹了SpringBoot實(shí)現(xiàn)PDF添加水印的三種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
解決spring.thymeleaf.cache=false不起作用的問題
這篇文章主要介紹了解決spring.thymeleaf.cache=false不起作用的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
java 網(wǎng)絡(luò)編程之TCP通信和簡(jiǎn)單的文件上傳功能實(shí)例
下面小編就為大家分享一篇java 網(wǎng)絡(luò)編程之TCP通信和簡(jiǎn)單的文件上傳功能實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01
SpringMVC結(jié)合ajaxfileupload.js實(shí)現(xiàn)文件無刷新上傳
這篇文章主要介紹了SpringMVC結(jié)合ajaxfileupload.js實(shí)現(xiàn)文件無刷新上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
SpringMVC使用自定義驗(yàn)證器進(jìn)行數(shù)據(jù)驗(yàn)證的方法
SpringMVC?提供了強(qiáng)大的數(shù)據(jù)驗(yàn)證機(jī)制,可以方便地驗(yàn)證表單提交的數(shù)據(jù),除了自帶的驗(yàn)證器之外,SpringMVC?還支持自定義驗(yàn)證器,允許開發(fā)者根據(jù)業(yè)務(wù)需求自定義驗(yàn)證規(guī)則,本文將介紹如何在?SpringMVC?中使用自定義驗(yàn)證器2023-07-07
SpringBoot使用責(zé)任鏈模式優(yōu)化業(yè)務(wù)邏輯中的if-else代碼
在開發(fā)過程中,我們經(jīng)常會(huì)遇到需要根據(jù)不同的條件執(zhí)行不同的邏輯的情況,我們可以考慮使用責(zé)任鏈模式來優(yōu)化代碼結(jié)構(gòu),使得代碼更加清晰、可擴(kuò)展和易于維護(hù)2023-06-06
Java語(yǔ)言實(shí)現(xiàn)最大堆代碼示例
這篇文章主要介紹了Java語(yǔ)言實(shí)現(xiàn)最大堆代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。2017-12-12

