解決springboot自定義配置Boolean屬性不能生效的問(wèn)題
springboot自定義配置Boolean屬性不能生效
屬性名不能是is開(kāi)頭,例如isLog屬性,你在配置文件中不管怎么給這個(gè)屬性設(shè)值都不會(huì)生效,只需改成log即可。
我使用的版本
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.6.RELEASE</version> <relativePath/> </parent>
springboot自動(dòng)配置原理
springboot自動(dòng)配置
1、概述
自動(dòng)配置的功能是其簡(jiǎn)化運(yùn)用的關(guān)鍵技術(shù),思想就是約定大于配置,意思就是一個(gè)工程約定必須要有事務(wù)功能,要有aop功能,要有mvc功能等,所以springboot在創(chuàng)建工程的時(shí)候自動(dòng)把這些功能所需的類(lèi)實(shí)例化并加入到spring容器了,這個(gè)就是約定大于配置,約定了必須要有這些功能。
2、springboot中的SPI機(jī)制
java原生的SPI,是一種服務(wù)發(fā)現(xiàn)機(jī)制( Service Provider Interface)。
它通過(guò)在ClassPath路徑下的META-INF/services文件夾查找文件,自動(dòng)加載文件里所定義的類(lèi)。
這一機(jī)制為很多框架擴(kuò)展提供了可能,比如在Dubbo、JDBC中都使用到了SPI。
- 2.1、JDK中的SPI機(jī)制
public interface Log { boolean support(String type); void info(); }
在resources/META-INF/services目錄創(chuàng)建文件,文件名稱(chēng)必須跟接口的完整限定名相同。
這個(gè)接口文件中配置了該接口的所有實(shí)現(xiàn)類(lèi)的完整限定名。
然后jdk api 加載配置文件
//jdk api 加載配置文件配置實(shí)例 ServiceLoader<Log> all = ServiceLoader.load(Log.class);
- 2.2、springboot中的SPI機(jī)制
具體流程和上面差不多,在工程的resources下面創(chuàng)建META-INF文件夾,在文件夾下創(chuàng)建spring.factories文件,在文件配置內(nèi)容如下:
com.ss.yc.spi.Log=\ com.ss.yc.spi.Log4j,\ com.ss.yc.spi.Logback,\ com.ss.yc.spi.Slf4j
配置的key就是接口完整限定名,value就是接口的各個(gè)實(shí)現(xiàn)類(lèi),用","號(hào)隔開(kāi)。
loadFactoryNames方法獲取實(shí)現(xiàn)了接口的所有類(lèi)的名稱(chēng)
@Test public void test() { List<String> strings = SpringFactoriesLoader .loadFactoryNames(Log.class, ClassUtils.getDefaultClassLoader()); for (String string : strings) { System.out.println(string); } }
loadFactories方法獲取實(shí)現(xiàn)了接口的所有類(lèi)的實(shí)例
@Test public void test1() { List<String> strings = SpringFactoriesLoader .loadFactories(Log.class, ClassUtils.getDefaultClassLoader()); for (String string : strings) { System.out.println(string); } }
- 2.3、我們以SpringFactoriesLoader.loadFactoryNames(Log.class, ClassUtils.getDefaultClassLoader());方法調(diào)用為例分析其源碼
可以看到springboot spi是加載了整個(gè)工程的jar包和自己工程定義的spring.factories文件的。
其核心代碼
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
public static Properties loadProperties(Resource resource) throws IOException { Properties props = new Properties(); //核心代碼,把文件包裝成properties對(duì)象 fillProperties(props, resource); return props; }
springboot中的SPI其實(shí)就是加載整個(gè)工程里面的spring.factories文件,然后把文件里面的內(nèi)容建立一個(gè)key和value的映射關(guān)系,只是這個(gè)映射關(guān)系是一個(gè)類(lèi)型和list的映射關(guān)系。
3、@EnableAutoConfiguration
@EnableAutoConfiguration注解是springboot自動(dòng)配置的核心注解,就是因?yàn)橛羞@個(gè)注解存在就會(huì)把例如事務(wù),緩存,aop,mvc等功能自動(dòng)導(dǎo)入到springboot工程中,Spring框架提供的各種名字為@Enable開(kāi)頭的Annotation定義,比如@EnableScheduling、@EnableMBeanExport等,@EnableAutoConfiguration的理念和做事方式其實(shí)一脈相承,簡(jiǎn)單概括一下就是,借助@Import的支持,收集和注冊(cè)特定場(chǎng)景相關(guān)的bean定義。
@SuppressWarnings("deprecation") @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @AutoConfigurationPackage @Import(EnableAutoConfigurationImportSelector.class) public @interface EnableAutoConfiguration { ... }
@Import了一個(gè)類(lèi),這個(gè)AutoConfigurationImportSelector自動(dòng)配置類(lèi)
- 3.1、DeferredImportSelector
DeferredImportSelector該接口是ImportSelector接口的一個(gè)子接口,那么它是如何使用的呢?
// public class DeferredImportSelectorDemo implements DeferredImportSelector{ @Override public String[] selectImports(AnnotationMetadata importingClassMetadata) { System.out.println("=====DeferredImportSelectorDemo.selectImports"); return newString[]{DeferredBean.class.getName()}; } //要返回一個(gè)實(shí)現(xiàn)了Group接口的類(lèi) @Override public Class<?extendsGroup> getImportGroup(){ return DeferredImportSelectorGroupDemo.class; } private static class DeferredImportSelectorGroupDemo implements Group{ List<Entry> list=new ArrayList<>(); //收集需要實(shí)例化的類(lèi) @Override public void process(AnnotationMetadata metadata,DeferredImportSelector selector){ System.out.println("=====DeferredImportSelectorGroupDemo.process"); String[] strings=selector.selectImports(metadata); for(String string:strings){ list.add(newEntry(metadata,string)); } } //把收集到的類(lèi)返回給spring容器 @Override public Iterable<Entry> selectImports(){ System.out.println("=====DeferredImportSelectorGroupDemo.selectImports"); return list; } } } //要實(shí)例的bean public class DeferredBean{}
該類(lèi)必須是@Import導(dǎo)入進(jìn)來(lái)
@Component //Import雖然是實(shí)例化一個(gè)類(lèi),Import進(jìn)來(lái)的類(lèi)可以實(shí)現(xiàn)一些接口@Import({DeferredImportSelectorDemo.class}) public class ImportBean{}
這樣就實(shí)現(xiàn)了一個(gè)類(lèi)的實(shí)例化。
- 3.2、EnableAutoConfigurationImportSelector
而AutoCon?gurationImportSelector類(lèi),這個(gè)類(lèi)就是@EnableAutoCon?guration注解中@Import進(jìn)來(lái)的類(lèi),可以看到該類(lèi)正是實(shí)現(xiàn)了DeferredImportSelector接口的。
該類(lèi)其實(shí)就是收集spring.factories文件中以@EnableAutoCon?guration類(lèi)型為key的所有的類(lèi),然后把這些類(lèi)交給spring去實(shí)例化,而這些類(lèi)就是我們說(shuō)的aop、事務(wù)、緩存、mvc等功能的支持類(lèi),這就是自動(dòng)配置的加載原理。
4、@Configuration
就是JavaConfig形式的Spring Ioc容器的配置類(lèi)使用的那個(gè)@Configuration,SpringBoot社區(qū)推薦使用基于JavaConfig的配置形式,所以,這里的啟動(dòng)類(lèi)標(biāo)注了@Configuration之后,本身其實(shí)也是一個(gè)IoC容器的配置類(lèi)。
其中@Bean的方法,其返回值將作為一個(gè)bean定義注冊(cè)到Spring的IoC容器,方法名將默認(rèn)成該bean定義的id。
5、@ComponentScan
其實(shí)就是自動(dòng)掃描并加載符合條件的組件(比如@Component和@Repository等)或者bean定義,最終將這些bean定義加載到IoC容器中。
可以通過(guò)basePackages等屬性來(lái)細(xì)粒度的定制@ComponentScan自動(dòng)掃描的范圍,如果不指定,則默認(rèn)Spring框架實(shí)現(xiàn)會(huì)從聲明@ComponentScan所在類(lèi)的package進(jìn)行掃描
自定義SpringBoot Starter
1.引入項(xiàng)目的配置依賴(lài)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>2.1.4.RELEASE</version> </dependency>
2.創(chuàng)建xxxService類(lèi)
完成相關(guān)的操作邏輯
DemoService.java @Data public class DemoService{ private String str1; private String str2; }
3.定義xxxProperties類(lèi)
屬性配置類(lèi),完成屬性配置相關(guān)的操作,比如設(shè)置屬性前綴,用于在application.properties中配置
//指定項(xiàng)目在屬性文件中配置的前綴為str,即可以在屬性文件中通過(guò) str.str1=springboot,就可以改變屬性類(lèi)字段 str1 的值了 @SuppressWarnings("ConfigurationProperties") @ConfigurationProperties(prefix = "str") public class DemoProperties { public static final String DEFAULT_STR1 = "I know, you need me"; public static final String DEFAULT_STR2 = "but I also need you"; private String str1 = DEFAULT_STR1; private String str2 = DEFAULT_STR2; }
4.定義xxxAutoConfiguration類(lèi)
自動(dòng)配置類(lèi),用于完成Bean創(chuàng)建等工作
// 定義 java 配置類(lèi) @Configuration //引入DemoService @ConditionalOnClass({DemoService.class}) // 將 application.properties 的相關(guān)的屬性字段與該類(lèi)一一對(duì)應(yīng),并生成 Bean @EnableConfigurationProperties(DemoProperties.class) public class DemoAutoConfiguration { // 注入屬性類(lèi) @Autowired private DemoProperties demoProperties; @Bean // 當(dāng)容器沒(méi)有這個(gè) Bean 的時(shí)候才創(chuàng)建這個(gè) Bean @ConditionalOnMissingBean(DemoService.class) public DemoService helloworldService() { DemoService demoService= new DemoService(); demoService.setStr1(demoProperties.getStr1()); demoService.setStr2(demoProperties.getStr2()); return demoService; } }
5.在resources下創(chuàng)建目錄META-INF
在 META-INF 目錄下創(chuàng)建 spring.factories
在SpringBoot啟動(dòng)時(shí)會(huì)根據(jù)此文件來(lái)加載項(xiàng)目的自動(dòng)化配置類(lèi)
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.demo.springboot.config.DemoAutoConfiguration
6.其他項(xiàng)目中使用自定義的Starter
<!--引入自定義Starter--> <dependency> <groupId>com.lhf.springboot</groupId> <artifactId>spring-boot-starter-demo</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
7.編寫(xiě)屬性配置文件
#配置自定義的屬性信息 str.str1=為什么我的眼里常含淚水 str.str2=那是因?yàn)槲覍?duì)你愛(ài)的深沉
8.寫(xiě)注解使用
@RestController public class StringController { @Autowired private DemoService demoService; //引入自定義Starter中的DemoService @RequestMapping("/") public String addString(){ return demoService.getStr1()+ demoService.getStr2(); } }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
JAVA 截取字符串的三種方法 subString,StringUtils,split實(shí)例詳解
這篇文章給大家介紹JAVA 截取字符串的三種方法 subString,StringUtils,split,每種方法結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-12-12MyEclipse8.6首次運(yùn)行maven項(xiàng)目圖標(biāo)上沒(méi)有小M的標(biāo)識(shí)怎么解決
myeclipse8.6導(dǎo)入maven項(xiàng)目后識(shí)別為普通java項(xiàng)目,即項(xiàng)目圖標(biāo)上沒(méi)有小M的標(biāo)識(shí)。這時(shí)是無(wú)法直接運(yùn)行的,怎么解決這一問(wèn)題呢?下面小編給大家?guī)?lái)了解決方案,需要的朋友參考下吧2016-11-11SpringBoot使用Redis對(duì)用戶IP進(jìn)行接口限流的示例詳解
使用接口限流的主要目的在于提高系統(tǒng)的穩(wěn)定性,防止接口被惡意打擊,這篇文章主要介紹了SpringBoot使用Redis對(duì)用戶IP進(jìn)行接口限流的示例代碼,需要的朋友可以參考下2023-07-07Docker 解決openjdk容器里無(wú)法使用JDK的jmap等命令問(wèn)題
這篇文章主要介紹了Docker 解決openjdk容器里無(wú)法使用JDK的jmap等命令問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12教你快速搭建sona服務(wù)及idea使用sona的方法
Sonar 是一個(gè)用于代碼質(zhì)量管理的開(kāi)放平臺(tái)。通過(guò)插件機(jī)制,Sonar 可以集成不同的測(cè)試工具,代碼分析工具,以及持續(xù)集成工具,本文給大家分享搭建sona服務(wù)及idea使用sona的方法,感興趣的朋友一起看看吧2021-06-06