Java SpringBoot容器注入對(duì)象詳解
1.注入的方式
方式一:使用Import注解
增加一個(gè)類HelloCompent
package com.lx.component;
public class HelloCompent {
public void say() {
System.out.println("HelloCompent.say hello");
}
}
@SpringBootApplication
@Import(HelloCompent.class)
public class StartProgramNoWeb {
public static void main(String[] args) {
System.out.println("啟動(dòng)");
SpringApplication.run(StartProgramNoWeb.class, args);
}
}
使用@Import就可以將HelloCompent注入到容器中。(HelloCompent類不需要增加@Service ,
@Component等注解)

方式二:使用@Service 或者@Component等注解注入到容器中
在需要注入的類增加注解,修改HelloCompent類
package com.lx.component;
import org.springframework.stereotype.Component;
@Component
public class HelloCompent {
public void say() {
System.out.println("HelloCompent.say hello");
}
}

方式三:使用@Configuration和@Bean組合實(shí)現(xiàn)
使用@Configuration和@Bean組合注入可以將對(duì)象注入到容器中,我主要生效的還是@Bean,如果將@Configuration換成@Component也是可以正常注入的。
增加一個(gè)CustomConfig類
package com.lx.config;
import com.lx.component.HelloCompent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CustomConfig {
@Bean("helloCompentConfig")
public HelloCompent helloCompent() {
return new HelloCompent();
}
}
這里我使用了方式2和3同時(shí)注入了,會(huì)導(dǎo)致重復(fù)注入而發(fā)生異常。所以我在bean是增加一個(gè)名稱,所以打印容器里對(duì)象的名稱也就是設(shè)置的名稱。

springboot自動(dòng)配置注入對(duì)象就是使用的方式3實(shí)現(xiàn)注入對(duì)象到容器中,平時(shí)最常用的就是方式2和方式3,如果同時(shí)使用方式2和方式3注入會(huì)出現(xiàn)注入重復(fù)的對(duì)象。
2.注入是增加條件判斷注解
@ComponentScan:聲明作用域
@ConditionalOnBean:當(dāng)容器里有指定Bean的條件下
@ConditionalOnClass:當(dāng)類路徑下有指定的類的條件下
@ConditionalOnExpression:基于SpEL表達(dá)式為true的時(shí)候作為判斷條件才去實(shí)例化
@ConditionalOnJava:基于JVM版本作為判斷條件
@ConditionalOnJndi:在JNDI存在的條件下查找指定的位置
@ConditionalOnMissingBean:當(dāng)容器里沒有指定Bean的情況下
@ConditionalOnMissingClass:當(dāng)容器里沒有指定類的情況下
@ConditionalOnWebApplication:當(dāng)前項(xiàng)目時(shí)Web項(xiàng)目的條件下
@ConditionalOnNotWebApplication:當(dāng)前項(xiàng)目不是Web項(xiàng)目的條件下
@ConditionalOnProperty:指定的屬性是否有指定的值
@ConditionalOnProperty(prefix = "customconfig",name = "enable",havingValue = "true") 等效于@ConditionalOnProperty(value = "customconfig.enable",havingValue = "true")
@ConditionalOnResource:類路徑是否有指定的值
@ConditionalOnOnSingleCandidate:當(dāng)指定Bean在容器中只有一個(gè),或者有多個(gè)但是指定首選的Bean
這些注解都組合了@Conditional注解,只是使用了不同的條件組合最后為true時(shí)才會(huì)去實(shí)例化需要實(shí)例化的類,否則忽略
3.構(gòu)造方法時(shí)帶參數(shù)注入
有時(shí)候在實(shí)際工作中,我們需要在構(gòu)造方法是增加一些處理邏輯,同事也需要從容器中獲取對(duì)象,但是這時(shí)候我們?cè)跇?gòu)造方式時(shí)想從容器中獲取對(duì)象,實(shí)際上并不能獲取到。因?yàn)檫@個(gè)spring的注解優(yōu)先級(jí)有關(guān)系。當(dāng)構(gòu)造方法使用字段時(shí),spring并沒有將對(duì)象注入成功,所有構(gòu)造方式取值也就是用。
package com.lx.component;
import com.lx.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class HelloTwoCompent {
@Value("${proper.name}")
private String name;
@Autowired
private HelloService helloService;
public HelloTwoCompent() {
System.out.println("hellotwo 無參");
System.out.println("name=" + name + ";helloService=" + helloService);
if (helloService != null) {
helloService.print();
}
}
}

方式1:使用spring xml實(shí)現(xiàn)
新增加一個(gè)用于測(cè)試的類HelloTwoCompent
在xml bean節(jié)點(diǎn)上增加構(gòu)造方法參數(shù)配置即可。然后在springboot啟動(dòng)類上增加@ImportResource(locations= {"classpath:application-bean.xml"})。這里我不喜歡用,暫時(shí)就不寫測(cè)試代碼了。
方式2:使用@Autowired
修改HelloTwoCompent 類在構(gòu)造方法上增加@Autowired
@Autowired
public HelloTwoCompent( @Value("${proper.name}") String name, HelloService helloService) {
System.out.println("hellotwo 兩參");
System.out.println("name=" + name + ";helloService=" + helloService);
if (helloService != null) {
helloService.print();
}
}

方式3使用@Configuration和@Bean組合
增加一個(gè)配置了 HelloConfig
package com.lx.config;
import com.lx.component.HelloTwoCompent;
import com.lx.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class HelloConfig {
@Value("${proper.name}")
private String name;
@Autowired
private HelloService helloService;
@Bean("helloTwoCompentBean")
public HelloTwoCompent helloTwoCompent() {
return new HelloTwoCompent(name,helloService,"config-bean");
}
}
修改一下HelloTwoCompent,增加一個(gè)三個(gè)參數(shù)的構(gòu)造方法,并且構(gòu)造方法上不增加任何的注解。
public HelloTwoCompent(String name, HelloService helloService,String type) {
System.out.println("hellotwo 三參;type="+type);
System.out.println("name=" + name + ";helloService=" + helloService);
if (helloService != null) {
helloService.print();
}
}

4.對(duì)象注入時(shí)的一些總結(jié)
1.靜態(tài)字段不支持@Autowired和@Resource實(shí)現(xiàn)自動(dòng)裝配,因?yàn)樽詣?dòng)裝配依賴于set和get方法,@Autowired和@Resource就是消除set和get方法。
2.自動(dòng)裝配的字段可以為private,因?yàn)樽詣?dòng)裝配依賴于set和get方法。所以和字段的作用域無關(guān)。
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Java實(shí)現(xiàn)級(jí)聯(lián)下拉結(jié)構(gòu)的示例代碼
在開發(fā)過程中,會(huì)遇到很多的實(shí)體需要將查出的數(shù)據(jù)處理為下拉或者級(jí)聯(lián)下拉的結(jié)構(gòu),提供給前端進(jìn)行展示。本文為大家介紹了java封裝下拉和級(jí)聯(lián)下拉的通用工具類,需要的可以參考一下2022-06-06
使用CXF和Jersey框架來進(jìn)行Java的WebService編程
這篇文章主要介紹了使用CXF和Jersey框架來進(jìn)行Java的WebService編程,Web service是一個(gè)平臺(tái)獨(dú)立的低耦合的自包含的基于可編程的web的應(yīng)用程序,需要的朋友可以參考下2015-12-12
SpringCloud Eureka實(shí)現(xiàn)服務(wù)注冊(cè)與發(fā)現(xiàn)
Eureka是一種基于REST(具像狀態(tài)傳輸)的服務(wù),主要用于AWS云中定位服務(wù),以實(shí)現(xiàn)中間層服務(wù)器的負(fù)載平衡和故障轉(zhuǎn)移。本文記錄一個(gè)簡(jiǎn)單的服務(wù)注冊(cè)與發(fā)現(xiàn)實(shí)例。感興趣的小伙伴們可以參考一下2019-01-01
Java線性結(jié)構(gòu)中的雙向鏈表實(shí)現(xiàn)原理
這篇文章將給大家詳細(xì)講解雙向鏈表的內(nèi)容,尤其是會(huì)通過代碼來進(jìn)行鏈表的操作,文中的代碼示例介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2023-07-07
Spring Boot統(tǒng)一異常處理最佳實(shí)踐(拓展篇)
這篇文章主要給大家介紹了關(guān)于Spring Boot統(tǒng)一異常處理最佳實(shí)踐(拓展篇)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
Java中Easypoi實(shí)現(xiàn)excel多sheet表導(dǎo)入導(dǎo)出功能
這篇文章主要介紹了Java中Easypoi實(shí)現(xiàn)excel多sheet表導(dǎo)入導(dǎo)出功能,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Springboot RestTemplate 簡(jiǎn)單使用解析
這篇文章主要介紹了Springboot RestTemplate 簡(jiǎn)單使用解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
MyBatis Generator配置生成接口和XML映射文件的實(shí)現(xiàn)
本文介紹了配置MBG以生成Mapper接口和XML映射文件,過合理使用MBG和自定義生成策略,可以有效解決生成的Example類可能帶來的問題,使代碼更加簡(jiǎn)潔和易于維護(hù)2025-02-02
Java中的ArrayList和contains函數(shù)和擴(kuò)容機(jī)制(源碼詳解)
這篇文章主要介紹了Java中的ArrayList和contains函數(shù)和擴(kuò)容機(jī)制,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-10-10

