第一步需要在pom.xml文件指定需要導(dǎo)入的坐標(biāo)
要是沒有自動(dòng)提示需要檢查maven有沒有
實(shí)現(xiàn)代碼
/*springboot第三方自動(dòng)配置實(shí)現(xiàn)方法 * 什么是自動(dòng)配置 自動(dòng)配置就是springboot啟動(dòng)自動(dòng)加載的類不需要在手動(dòng)的控制反轉(zhuǎn)自動(dòng)的加入bean中 * * */ /*第一種方案包掃描 不推薦因?yàn)榉爆嵶约阂矝]有試成功過加在啟動(dòng)類上 @ComponentScan({"com.example","com.example.comtihmabc2"}*/ /* 第二種方案@Import注解導(dǎo)入加在啟動(dòng)類上 @Import({HeaderParser.class}) 導(dǎo)入普通類交給I0C容器管理 @Import({HeaderConfig.class})//導(dǎo)入配置類,交給I0C容器管理 示例代碼: @Configuration public class HeaderConfig { @Bean public HeaderParser headerParser(){ return new HeaderParser();} @Bean public HeaderGenerator headerGenerator(){ return new HeaderGenerator(); } } @Import({MyImportSelector.class})//導(dǎo)入Importselector接口實(shí)現(xiàn)類 第三方所實(shí)現(xiàn)的接口ImportSelector 里面清楚的寫明白了需要導(dǎo)入的類位置 示例代碼: public class MyImportSelector implements ImportSelector { public String[] selectImports(AnnotationMetadata importingClassMetadata) { return new String[]{"com.example.HeaderConfig"}; } */ /*第三種方案自定義注解@EnableHeaderConfig加在啟動(dòng)類上 第三方自定義注解示例代碼: @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Import(MyImportSelector.class)//MyImportSelector.class表示所要導(dǎo)入的類可以是配置類 public @interface EnableHeaderConfig { } * */ @SpringBootApplication public class ComTihmaBc2Application { public static void main(String[] args) { SpringApplication.run(ComTihmaBc2Application.class, args); } }
執(zhí)行代碼示例文章來源:http://www.zghlxwxcb.cn/news/detail-829547.html
@SpringBootTest class ComTihmaBc2ApplicationTests { // //1.獲取I0c容器對(duì)象 // @Autowired // ApplicationContext applicationContext; @Autowired //手動(dòng)依賴注入 ApplicationContext applicationContext ; // @Test // void contextLoads() { // applicationContext.getBean(HeaderParser.class).parse(); // } @Test void contextLoads1() { applicationContext.getBean(HeaderGenerator.class).generate(); } } 自動(dòng)裝配條件判斷
/*springboot自動(dòng)配置條件判斷注解滿足條件才會(huì)執(zhí)行 通常聲名在類上或者方法上 * springboot底層自動(dòng)配置就是用此方法 * */ @Configuration public class HeaderConfig { //注解1 /*判斷環(huán)境中是否有對(duì)應(yīng)字節(jié)碼文件,才注冊(cè)bean到I0C容器??赏ㄟ^類型value或者名字name來進(jìn)行判斷 示例1 : @ConditionalOnClass(name ="io.jsonwebtoken.Jwts") 名字name 示例2 : @ConditionalOnClass(value = HeaderParser.class ) 通過類型value */ //注解2 /* @ConditionalOnMissingBean 當(dāng)不存在當(dāng)前類型的bean時(shí), 才聲明該bean才加入ioc容器 ---也可以指定類型(value屬性)或 名稱(name屬性) 沒有就是當(dāng)前 比如 @ConditionalOnMissingBean public HeaderParser headerParser(){ return new HeaderParser();} */ /* @ConditionalOnProperty(name = "name",havingValue = "itheima") 配置文件中存在對(duì)應(yīng)的屬性和值,才注冊(cè)bean到I0C容器 比如application.properties 配置文件中 # 應(yīng)用服務(wù) WEB 訪問端口 server.port=8080 name=itheima 有就可以執(zhí)行 */ @Bean @ConditionalOnProperty(name = "name",havingValue = "itheima") public HeaderParser headerParser(){ return new HeaderParser();} @Bean public HeaderGenerator headerGenerator(){ return new HeaderGenerator(); } }
文章來源地址http://www.zghlxwxcb.cn/news/detail-829547.html
到了這里,關(guān)于spring boot自動(dòng)裝配及自動(dòng)裝配條件判斷的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!