springBoot系列常用注解(小結(jié))
作用是:對(duì)自定義的properties文件加載
使用:@PropertySource(value={'classpath:people.properties'})或者@PropertySource(value='classpath:people.properties')
properties文件,獲取到值亂碼問(wèn)題
亂碼解決:
file ->settings -->file encoding--> 勾選Transparent native-to-ascill conversion
作用:可以讓spring的配置文件生效
使用:在啟用類上加ImportResource注解,如@ImportResource(value = 'classpath:person.xml')或者@ImportResource(locations ={'classpath:person.xml'})
@Conditional作用:必須是@Conditional指定的條件成立,才給容器中添加組件或者是讓自動(dòng)配置類生效。
以 HttpEncodingAutoConfiguration 自動(dòng)配置類舉例
@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@ConditionalOnMissingBeanpublic 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è)自動(dòng)配置類HttpEncodingAutoConfiguration
@ConditionalOnProperty(prefix = 'server.servlet.encoding', value = 'enabled', matchIfMissing = true)作用是如果配置文件中配置了server.servlet.encoding=enabled則才能夠?qū)嵗哌@個(gè)個(gè)自動(dòng)配置類HttpEncodingAutoConfiguration
springBoot中所有的自動(dòng)配置類位置:
orgspringframeworkbootspring-boot-autoconfigure2.4.5spring-boot-autoconfigure-2.4.5.jar!META-INFspring.factories文件中
如何判斷spring.factories這個(gè)文件中那些自動(dòng)配置類是生效的?
在yml或者applicaton.properties中配置,會(huì)在控制臺(tái)打印自動(dòng)配置類生效報(bào)告:
########打印自動(dòng)配置類生效的報(bào)告##########debug =true
其中:Negative match:表示未生效;Positive match:表示生效的
到此這篇關(guān)于springBoot系列常用注解(小結(jié))的文章就介紹到這了,更多相關(guān)springBoot常用注解內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向2. 淺談python出錯(cuò)時(shí)traceback的解讀3. python matplotlib:plt.scatter() 大小和顏色參數(shù)詳解4. JSP數(shù)據(jù)交互實(shí)現(xiàn)過(guò)程解析5. PHP設(shè)計(jì)模式中工廠模式深入詳解6. Python importlib動(dòng)態(tài)導(dǎo)入模塊實(shí)現(xiàn)代碼7. Ajax實(shí)現(xiàn)表格中信息不刷新頁(yè)面進(jìn)行更新數(shù)據(jù)8. 利用promise及參數(shù)解構(gòu)封裝ajax請(qǐng)求的方法9. windows服務(wù)器使用IIS時(shí)thinkphp搜索中文無(wú)效問(wèn)題10. .NET中l(wèi)ambda表達(dá)式合并問(wèn)題及解決方法
