SpringBoot系列教程12SpringBoot自动配置原理探析.docx
《SpringBoot系列教程12SpringBoot自动配置原理探析.docx》由会员分享,可在线阅读,更多相关《SpringBoot系列教程12SpringBoot自动配置原理探析.docx(3页珍藏版)》请在冰豆网上搜索。
SpringBoot系列教程12SpringBoot自动配置原理探析
SpringBoot系列教程12--SpringBoot自动配置原理探析
SpringBoot系列教程12--SpringBoot自动配置原理探析
作者:
一一哥
一.SpringBoot自动配置概述
1.概述
在Spring4.x之后,提供了一个按条件配置Bean的功能,并且结合“习惯优于配置”的理念,后面推出了SpringBoot这个全新的开发框架。
在SpringBoot中,自动配置可以说是精髓了。
当然这个自动配置的原理,一般也就只有在面试的时候才用得上,但是我们学习过程中也有必要了解这个自动配置,有助于加深我们对SpringBoot的了解。
2.SpringBoot的配置文件
在SpringBoot中有一个全局的配置文件:
application.properties或application.yml。
我们开发过程中,各种配置属性都可以在这个文件中进行配置,常用的一些配置属性,比如:
server.port、spring.application.name等等,当然实际开发中,我们往往只是用到很少的一部分,其实这些属性非常的多,具体可以参考下面的链接:
https:
//docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/#common-application-properties
下面截取了官方提供的部分属性:
#===================================================================
#COMMONSPRINGBOOTPROPERTIES
#
#Thissamplefileisprovidedasaguideline.DoNOTcopyitinits
#entiretytoyourownapplication.^^^
#===================================================================
#----------------------------------------
#COREPROPERTIES
#----------------------------------------
debug=false#Enabledebuglogs.
trace=false#Enabletracelogs.
#LOGGING
logging.config=#Locationoftheloggingconfigurationfile.Forinstance,`classpath:
logback.xml`forLogback.
logging.exception-conversion-word=%wEx#Conversionwordusedwhenloggingexceptions.
logging.file=#Logfilename(forinstance,`myapp.log`).Namescanbeanexactlocationorrelativetothecurrentdirectory.
logging.file.max-history=0#Maximumofarchivelogfilestokeep.Onlysupportedwiththedefaultlogbacksetup.
logging.file.max-size=10MB#Maximumlogfilesize.Onlysupportedwiththedefaultlogbacksetup.
logging.group.*=#Loggroupstoquicklychangemultipleloggersatthesametime.Forinstance,`logging.level.db=org.hibernate,org.springframework.jdbc`.
logging.level.*=#Loglevelsseveritymapping.Forinstance,`logging.level.org.springframework=DEBUG`.
logging.path=#Locationofthelogfile.Forinstance,`/var/log`.
logging.pattern.console=#Appenderpatternforoutputtotheconsole.SupportedonlywiththedefaultLogbacksetup.
logging.pattern.dateformat=yyyy-MM-ddHH:
mm:
ss.SSS#Appenderpatternforlogdateformat.SupportedonlywiththedefaultLogbacksetup.
logging.pattern.file=#Appenderpatternforoutputtoafile.SupportedonlywiththedefaultLogbacksetup.
logging.pattern.level=%5p#Appenderpatternforloglevel.SupportedonlywiththedefaultLogbacksetup.
logging.register-shutdown-hook=false#Registerashutdownhookfortheloggingsystemwhenitisinitialized.
#AOP
spring.aop.auto=true#Add@EnableAspectJAutoProxy.
spring.aop.proxy-target-class=true#Whethersubclass-based(CGLIB)proxiesaretobecreated(true),asopposedtostandardJavainterface-basedproxies(false).
#IDENTITY(ContextIdApplicationContextInitializer)
spring.application.name=#Applicationname.
#ADMIN(SpringApplicationAdminJmxAutoConfiguration)
spring.application.admin.enabled=false#Whethertoenableadminfeaturesfortheapplication.
spring.application.admin.jmx-name=org.springframework.boot:
type=Admin,name=SpringApplication#JMXnameoftheapplicationadminMBean.
#AUTO-CONFIGURATION
spring.autoconfigure.exclude=#Auto-configurationclassestoexclude.
#BANNER
spring.banner.charset=UTF-8#Bannerfileencoding.
spring.banner.location=classpath:
banner.txt#Bannertextresourcelocation.
spring.banner.image.location=classpath:
banner.gif#Bannerimagefilelocation(jpgorpngcanalsobeused).
spring.banner.image.width=76#Widthofthebannerimageinchars.
spring.banner.image.height=#Heightofthebannerimageinchars(defaultbasedonimageheight).
spring.banner.image.margin=2#Lefthandimagemargininchars.
spring.banner.image.invert=false#Whetherimagesshouldbeinvertedfordarkterminalthemes.