开发者学堂课程【SpringBoot 实战教程: 全局配置文件】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/651/detail/10772
全局配置文件
内容介绍:
一、定义
二、举例
一、定义
1、springboot 会根据在 pom 中依赖的 jar 包做自动配置,但是有些情况下不希望使用 springboot 的默认配置,那么可以使用 springboot 全局配置文件解决这个问题。
2、Spring Boot 项目使用一个全局的配置文件 application.properties 或者是 application.yml,,这里需要注意全局配置文件名称必须叫 application,扩展名可以是 properties 或者是 yml,yml 格式目前使用的比较多,它是使用 yaml 的语言进行定义。yml 语言是以数据为中心的语言,具备面向对象的特征。在 resources 目录下或者类路径下的 /config. 下,一般放到 resources 下。
二、举例
1、在 resources 下创建全局配置文件,注意名称必须是 application,首先创建 properties 文件。
2、比如 springboot 下默认配置的 tomcat 端口是8080,如果不想使用8080,可以用 server.port 属性名进行改变。修改 tomcat 的端口为 8088:server.port=8088
3、properties 左边是名称,右边是值,如果是 yml 格式呢?可以进行对比,注意写数值之前加空格:
Server
:
Port
:
8088
4、写了两个配置文件,springboot 都会进行读取,配置的值是一样的。
5、启动,可以看到端口号变成8088。
Tomcat
started on
port(s) :
8
088(http)
进入 DispatcherServlet 的规则为: * .html
server.context- path=/hello
6、访问 springboot 工程路径,默认的是/,改变默认,改成 ww。
Server
.
context
-
path
=/
ww
在 properties 文件里面都要写 server,但是在 yml 格式里面直接写 context-pat,server 可以省略,相比较下 yml 格式简便。主要加空格。
context
-
path
: /
ww
7、重新启动,可以看到 /ww。
8、输入http://localhost:8088/
访问。上下文路径是 ww。回车可以看到 hello world!
9、如果要改变 springboot 默认配置通过全局配置文件 application 进行改变, application 全局文件有两种格式。
10、默认配置如下,在使用时进行查找即可。
# BANNER 关于 banner 的配置,启动的图标
banner.charset=UTF-8 # Banner file encoding.
banner. location=classpath:banner.txt # Banner file Location.
banner. image . location=classpath:banner.gif # Banner image file
Location(jpg/png can also be used).
banner. image.width= # Width of the banner image in chars (default 76)
banner. image.height= # Height of the banner image in chars (default
based on image height)
banner. image. margin= # Left hand image margin in chars (default 2)
banner. image. invert= # If images should be inverted for dark terminal
themes (default false)
# LOGGING 和日志相关的配置
logging.config= # Location of the
logging configuration file. For
instance c Lasspath: Logback. xmL for Logback
logging. exception- conversion-word=%wEx # Conversion word used when
logging exceptions.
logging.file= # Log file name. For instance myapp. Log
logging.level.*= # Log
LeveLs severity mapping. For instance
‘logging. level. org. springframework=DEBUG
logging.path= # Location of the log file. For instance‘ /var/Log’
logging. pattern.console- # Appender pattern for output to the console.
OnLy supported with the default logback setup.
logging. pattern.file= # Appender pattern for output to the file. Only
supported with the default
Logback setup.
logging. pattern.level= # Appender pattern for Log Level (default%5p).
OnLy supported with the default logback setup.
logging. register- shutdown-hook=false # Register a shutdown hook, for
the Logging system when it is initialized.
# AOP
spring. aop.auto=true # Add @EnabLeAspectJAutoProxy.
spring.aop. proxy-target-class=false # Whether subclass-based (CGLIB)
proxies are to be created ( true) as opposed to standard Java
interface-based proxies (false).
# IDENTITY (ContextIdApplicationContextInitializer)
spring. application. index= # AppLication index.
spring. application.name= # Application name.
# ADMIN (SpringApplicationAdminJmxAutoConfiguration)
spring. application. admin. enabled=false # Enable admin features for
the application.
spring. application. admin. jmx-name=org. springfr amework. boot: type=Admin, name=SpringApplication # JMX name of the application admin MBean.
# AUTO-CONFIGURATION
spring. autoconfigure.exclude= # Auto-configuration classes to
excLode .
# SPRING CORE
spring. beaninfo .ignore-trueネSkip search of BeanInfo cLasses.
#
SPRING CACHE (cacheProperties)
spring缓存
spring. cache.cache-names= Comma-seporated list of coche names to
create if supported by the underlying cache manager.
spring. cache.caffeine.spec= # The sped to use ta create caches. Check
CaffeineSpec for more details on the spec format.
spring. cache . couchbase , expiration=0并Entry expiration in
millisecords.. By defouLt the eritries
n
ever expire.
spring. cache. ehcache.config= # The location of the configuration file
to use
to initialize EhCache.
spring. cache. guava.spec= # The spec to use to create caches. Check
CacheBuilderSpec for more details on the spec format.
spring. cache. infinispan. config= # The Location of the configuration
file to use to initialize Infinispan.
spring. cache.jcache. config= # The location of the configuration file to use to initialize the cache manager.
spring. cache. jcache. provider= # Fully qualified name of the CachingProvider impLementation to use to retrieve the JSR-107
compliant cache manager. OnLy needed if more than one
JSR-107
impLementation is available on the cLasspath.
spring.cache. type= # Cache type, auto-detected according to the environment by default.
# SPRING CONFIG - using environment property only
(ConfigFileApplicationListener)
spring. config.location= # Config file locations.
spring. config . name=application # Config file name.
# HAZELCAST (HazelcastProperties)
spring. hazelcast.config= # The Location of the configuration file to
use to initialize Hazelcast.
# PROJECT INFORMATION (ProjectInfoProperties )
spring. info. build. location=classpath :META-INF /build- info. Properti
es # Location of the generated build-info.properties file.
spring. info. git .location=classpath:git. properties # Location of the
enerated git. properties file.
#JMX
spring. jmx. default-domain= # JMX domain name.
spring. jmx. enabled=true # Expose management beans to the JMX domain
.
spring. jmx.server=mbeanServer # MBeanServer bean name.
# Email (MailProperties)
spring. mail. default-encoding=UTF-8 # Default MimeMessage encoding.
spring. mail.host= # SMTP server host. For instance ’smtp. example. Com‘
spring. mail. jndi-name= # Session JNDI name. When set, takes precedence
to others mail settings.
spring. mail.password= # Login password of the SMTP server.
spring. mail.port= # SMTP server port.
spring. mail. properties.*= # Additional JavaMail session properties.
spring. mail. prötocol=smtp # ProtocoL used by the SMTP server.
spring. mail. test- connection=false # Test
that the mail server is
available on startup .
spring. mail. username= # Login user of the SMTP server.
# APPLICATION SETTINGS (SpringApplication)
spring. main. banner- mode=console # Mode used to dispLay the banner when
the appLication runs.
spring. main. sources= # Sources (cLass name, package name or XML
resource location) to include in the AppL icationContext.
spring. main. web-environment= # Run the application in a web
-
environment (auto-detected by default) .
# FILE ENCODING (FileEncodingApplicationL istener )
spring. mandatory-file-encoding= # Expected character encoding the
appLication must use.
# INTERNATIONALIZATION (MessageSourceAutoConfiguration)
spring. messages. always-use-message-format=false # Set whether to
always apply the MessageFormat rules, parsing even messages without
arguments.
spring. messages .basename=messages # Comma-separated List of
basenames ,
each following the ResourceBundle convention.
spring. messages.cache-seconds=-1 # Loaded resource bundle files
cache expiration, in seconds. When set to -1, bundLes are cached
forever .
spring. messages .encoding=UTF-8 # Message bundles encoding.
spring. messages. fallback-to-system- locale=true # Set whether to fall
back to the system Locale if no files for a specific Locale have been
found.
# OUTPUT
spring. output. Ans.enabled=detect # Configure the ANSI output.
# PID FILE (ApplicationPidFileWriter)
spring. pid. fail-on-write-error= # FaiL if AppLicationPidFileWriter
# PROFILES
spring. profiles.active= # Comma-separated List (orlist if using YAML )of active profiles .
spring. profiles .include= # Unconditionally octivate the specified
comma separated profiles (or List of profiles if using YAML).
# SENDGRID (SendGridAutoConfiguration)
spring. sendgrid.api-key= # SendGrid api key (alternative to
username/ passward)
spring, sendgrid.username= # SendGrid account username
spring. sendgrid.password= # SendGrid account pas sword
spring, sendgrid. proxy,host= # SendGrid proxy host
spr ing. sendgrid.proxy.port= # SendGrid proxy port
还有 jsp 服务器,session,cookice,tomcat,freemarker 页面静态化,encoding 编码,jackson,springmac 前缀后缀。
spring. mvc.view.prefix= # Spring MVC view prefix.
spring. mvc.view.suffix= # Spring MVC view suffix.
还有 jabc,mongo,redis,thymeleaf 网版引擎,webservices,在使用时查阅文档即可,这就是 springboot 的全局配置文件。