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)容器里沒(méi)有指定Bean的情況下
@ConditionalOnMissingClass:當(dāng)容器里沒(méi)有指定類的情況下
@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并沒(méi)有將對(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 無(wú)參"); 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方法。所以和字段的作用域無(wú)關(guān)。
總結(jié)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Spring?Boot?快速使用?HikariCP?連接池配置詳解
Spring Boot 2.x 將其作為默認(rèn)的連接池組件,項(xiàng)目中添加 spring-boot-starter-jdbc 或 spring-boot-starter-data-jpa 模塊后,HikariCP 依賴會(huì)被自動(dòng)引入,這篇文章主要介紹了Spring?Boot使用HikariCP連接池配置詳解,需要的朋友可以參考下2023-06-06Spring6?的JdbcTemplate的JDBC模板類的使用介紹(最新推薦)
JdbcTemplate?是Spring?提供的一個(gè)JDBC模板類,是對(duì)JDBC的封裝,簡(jiǎn)化JDBC代碼,當(dāng)然,你也可以不用,可以讓Spring集成其它的ORM框架,這篇文章主要介紹了Spring6?的JdbcTemplate的JDBC模板類的詳細(xì)使用說(shuō)明,需要的朋友可以參考下2024-05-05SpringBoot項(xiàng)目打包成jar后獲取classpath下文件失敗的解決
這篇文章主要介紹了SpringBoot項(xiàng)目打包成jar后獲取classpath下文件失敗的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07Java 進(jìn)程執(zhí)行外部程序造成阻塞的一種原因
前一陣子在研究文檔展示時(shí)使用了java進(jìn)程直接調(diào)用外部程序,其中遇到一個(gè)問(wèn)題花了好長(zhǎng)時(shí)間才解決,這個(gè)問(wèn)題就是外部程序直接執(zhí)行沒(méi)什么問(wèn)題,但是當(dāng)使用Java進(jìn)程執(zhí)行時(shí)外部程序就阻塞在那兒不動(dòng)了。而且這個(gè)外部程序在處理某些文件時(shí)使用Java進(jìn)程執(zhí)行是沒(méi)問(wèn)題的2014-03-03Java Redis Template批量查詢指定鍵值對(duì)的實(shí)現(xiàn)
本文主要介紹了Java Redis Template批量查詢指定鍵值對(duì)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07SpringMVC 實(shí)現(xiàn)用戶登錄實(shí)例代碼
這篇文章主要介紹了SpringMVC 實(shí)現(xiàn)用戶登錄實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02