springBoot系列常用注解(小結(jié))
@PropertySource
作用是:對自定義的properties文件加載
使用:@PropertySource(value={"classpath:people.properties"})或者@PropertySource(value="classpath:people.properties")
properties文件,獲取到值亂碼問題
亂碼解決:
file ->settings -->file encoding--> 勾選Transparent native-to-ascill conversion
@ImportResource
作用:可以讓spring的配置文件生效
使用:在啟用類上加ImportResource注解,如@ImportResource(value = "classpath:person.xml")或者@ImportResource(locations ={"classpath:person.xml"})
@Conditional
作用:必須是@Conditional指定的條件成立,才給容器中添加組件或者是讓自動配置類生效。
以 HttpEncodingAutoConfiguration 自動配置類舉例
@Configuration(proxyBeanMethods = false) @EnableConfigurationProperties(ServerProperties.class) @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) @ConditionalOnClass(CharacterEncodingFilter.class) @ConditionalOnProperty(prefix = "server.servlet.encoding", value = "enabled", matchIfMissing = true) public class HttpEncodingAutoConfiguration { private final Encoding properties; public HttpEncodingAutoConfiguration(ServerProperties properties) { this.properties = properties.getServlet().getEncoding(); } @Bean @ConditionalOnMissingBean public CharacterEncodingFilter characterEncodingFilter() { CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter(); filter.setEncoding(this.properties.getCharset().name()); filter.setForceRequestEncoding(this.properties.shouldForce(Encoding.Type.REQUEST)); filter.setForceResponseEncoding(this.properties.shouldForce(Encoding.Type.RESPONSE)); return filter; }
@ConditionalOnMissingBean:作用是如果容器中不存在CharacterEncodingFilter 這個(gè)bean則實(shí)例化創(chuàng)建一個(gè),存在就不走下面的代碼
@ConditionalOnClass(CharacterEncodingFilter.class):作用是如果容器中存在CharacterEncodingFilter 這個(gè)bean(其中一個(gè)條件)則才能夠?qū)嵗邆€(gè)自動配置類HttpEncodingAutoConfiguration
@ConditionalOnProperty(prefix = "server.servlet.encoding", value = "enabled", matchIfMissing = true)作用是如果配置文件中配置了server.servlet.encoding=enabled則才能夠?qū)嵗哌@個(gè)個(gè)自動配置類HttpEncodingAutoConfiguration
springBoot中所有的自動配置類位置:
org\springframework\boot\spring-boot-autoconfigure\2.4.5\spring-boot-autoconfigure-2.4.5.jar!\META-INF\spring.factories文件中
如何判斷spring.factories這個(gè)文件中那些自動配置類是生效的?
在yml或者applicaton.properties中配置,會在控制臺打印自動配置類生效報(bào)告:
########打印自動配置類生效的報(bào)告########## debug =true
其中:Negative match:表示未生效;Positive match:表示生效的
到此這篇關(guān)于springBoot系列常用注解(小結(jié))的文章就介紹到這了,更多相關(guān)springBoot常用注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springmvc與mybatis集成配置實(shí)例詳解
這篇文章主要介紹了springmvc與mybatis集成配置實(shí)例詳解的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09SpringBoot應(yīng)用部署到Tomcat中無法啟動的解決方法
這篇文章主要介紹了SpringBoot應(yīng)用部署到Tomcat中無法啟動的解決方法,需要的朋友可以參考下2017-09-09spring?boot?使用?@Scheduled?注解和?TaskScheduler?接口實(shí)現(xiàn)定時(shí)任務(wù)
這篇文章主要介紹了spring?boot?使用?@Scheduled?注解和?TaskScheduler?接口實(shí)現(xiàn)定時(shí)任務(wù),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06Spring MVC通過添加自定義注解格式化數(shù)據(jù)的方法
這篇文章主要給大家介紹了關(guān)于Spring MVC通過添加自定義注解格式化數(shù)據(jù)的方法,文中先對springmvc 自定義注解 以及自定義注解的解析進(jìn)行了詳細(xì)的介紹,相信會對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11java設(shè)計(jì)模式-單例模式實(shí)現(xiàn)方法詳解
單例模式,屬于創(chuàng)建類型的一種常用的軟件設(shè)計(jì)模式。通過單例模式的方法創(chuàng)建的類在當(dāng)前進(jìn)程中只有一個(gè)實(shí)例(根據(jù)需要,也有可能一個(gè)線程中屬于單例2021-07-07Java8 Zip 壓縮與解壓縮的實(shí)現(xiàn)
這篇文章主要介紹了Java8 Zip 壓縮與解壓縮的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03