Spring Boot 配置
- Spring Boot 的配置主要有基本配置、外部配置、日志配置和 Profile 配置这几个方面的配置。
一、基本配置
1、入口类
- Spring Boot 项目和 C++ 工程项目一样,有一个入口类,其类名称往往形如 项目名Application,该类使用 @SpringBootApplication进行注解;类体里有一个 main 方法(标准的 Java main 方法),main 方法中使用 SpringApplication.run(Ch523Application.class, args); 启动项目。
- @SpringBootApplication 是一个组合注解,组合了 @Configuration、@EnableAutoConfiguration、@ComponentScan 这三个注解。
- @SpringBootApplication 注解的源码如下:
package org.springframework.boot.autoconfigure; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @Configuration @EnableAutoConfiguration @ComponentScan public @interface SpringBootApplication { Class<?>[] exclude() default {}; String[] excludeName() default {}; }
- 其中的 @EnableAutoConfiguration 让 Spring Boot 根据类路径中的 jar 包依赖为当前项目进行自动配置。
- 通常入口类放置在 groupId+artifactID 组合的包名下。
- 使用 @SpringBootApplication 的 exclude 参数可以关闭特定的自动配置,例如:
@SpringBootApplication(exclude={DemoConfiguration.class})
2、Banner 个性化
- Spring Boot 启动时有一个默认启动图案,如下:
- 需要的话我们可以将这个图案修改的,步骤如下:
- 1)先在项目的 src/main/resources 目录下新建一个 banner.txt
- 2)去 http://patorjk.com/software/taag 生成一个自己想要的 taag,例如我的:
$$\ $$\ $$$$$$\ $$\ $$\ $$\ \$$\ $$ | $$ __$$\ $$ | \$$\ $$ | \$$\ $$ /$$\ $$\ $$ / \__|$$$$$$$\ $$$$$$\ $$$$$$$\ $$$$$$\\$$\ $$ /$$$$$$\ $$$$$$$\ $$$$$$\ \$$$$ / $$ | $$ |$$ | $$ __$$\ $$ __$$\ $$ __$$\ $$ __$$\\$$$$ / \____$$\ $$ __$$\ $$ __$$\ \$$ / $$ | $$ |$$ | $$ | $$ |$$$$$$$$ |$$ | $$ |$$ / $$ |\$$ / $$$$$$$ |$$ | $$ |$$ / $$ | $$ | $$ | $$ |$$ | $$\ $$ | $$ |$$ ____|$$ | $$ |$$ | $$ | $$ | $$ __$$ |$$ | $$ |$$ | $$ | $$ | \$$$$$$ |\$$$$$$ |$$ | $$ |\$$$$$$$\ $$ | $$ |\$$$$$$$ | $$ | \$$$$$$$ |$$ | $$ |\$$$$$$$ | \__| \______/ \______/ \__| \__| \_______|\__| \__| \____$$ | \__| \_______|\__| \__| \____$$ | $$\ $$ | $$\ $$ | \$$$$$$ | \$$$$$$ | \______/ \______/
- 3)保存 banner.txt,然后再次运行,启动图案便会修改成你自己自定义的启动图案:
- 当然 Banner 能够自定义修改,也就能够关闭,关闭 Banner 的操作就是在入口类的 main 里修改
SpringApplication app = new SpringApplication(Ch523Application.class); app.setShowBanner(false); app.run(args); //等价写法 new SpringApplicationBuilder(Ch523Application.class) .showBanner(false) .run(args);
- 在 main 方法中做如上更改后,那么 Spring Boot 在启动时就不会显示启动图案了。
3、properties 文件
- Spring Boot 项目使用 application.properties 文件作为全局配置文件,位于项目的 src/main/resources/ 目录下,可以在该文件填写一些在整个项目都经常会用到的属性值,如数据库的 URL 和用户名以及密码之类的。
4、XML 配置
- 虽然 Spring Boot 建议零配置,但是有些时候必须用到 XML 配置,此时可以通过 Spring 提供的 @ImportResource 来加载 xml 配置:
@ImportResource({"classpath:one-context.xml","classpath:another-context.xml"})
二、外部配置
1、属性配置
- 当我们在 properties 文件中写好相应地属性值后,我们可以在 java 源文件中使用 @PropertySource 和 @Value 来注入 properties 文件中地值,示例如下:
1.1、编辑 properties 文件
- 先在 application.properties 文件中编辑一些属性和属性值:
author.name=YuChengyang author.mail=2923616405
- 接着修改一下入口类文件地内容,增加对上面两个属性和属性值地使用
@RestController @SpringBootApplication public class Ch523Application { @Value("${author.name}") private String authorName; @Value("${author.mail}") private String authorMail; @RequestMapping("/") String index(){ return "Spring Boot Demo Project, "+"author name is:"+authorName +", author mail is:"+authorMail; } public static void main(String[] args) { new SpringApplicationBuilder(Ch523Application.class) .showBanner(true) .run(args); } }
- 运行,浏览 localhost:8080 可以看到如下页面:
2、基于 properties 的类型安全配置
- Spring Boot 提供了基于类型安全的配置方式,通过 @ConfigurationProperties 将 properties 属性和一个 Bean 及其属性关联,从而实现类型安全的配置。
- 在上面的基础上,在项目的 src/main/java 目录下的适当位置新建一个 config 文件夹,在里面新建一个 bean,代码如下:
package com.wisely.ch5_2_3.config; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "author") public class AuthorSettings { private String name; private String mail; private Long age; public void setName(String name) { this.name = name; } public String getName() { return name; } public void setAge(Long age) { this.age = age; } public Long getAge() { return age; } public void setMail(String mail) { this.mail = mail; } public String getMail() { return mail; } }
- 对入口类进行重新编辑,代码如下:
@RestController @SpringBootApplication public class Ch523Application { @Autowired private AuthorSettings authorSettings; @RequestMapping("/") String index(){ return "author name is"+authorSettings.getName()+", age is:"+authorSettings.getAge() +", mail is:"+authorSettings.getMail(); } public static void main(String[] args) { new SpringApplicationBuilder(Ch523Application.class) .showBanner(true) .run(args); } }
- 运行效果类似上面。
3、日志配置和 Profile 配置
- Spring Boot 支持 Java Util Logging、LoganJ、Log4J2 和 Logback 作为日志框架;Spring Boot 默认使用 Logback 作为日志框架。
配置日志文件 logging.file=D:/mylog/log.log 配置日志文件级别 logging.level.包名=级别 logging.level.org.springframework.web=DEBUG
- Profile 是 Spring 用来针对不同环境对不同的配置提供支持的,全局的 Profile 配置使用 application-{profile}.properties。
- 在 src/main/resources 目录,也就是 application.properties 的同级目录里新建两个文件:application-dev.properties 和 application-prod.properties。
- 在 application-dev.properties 文件中写上内容:
server.port=8888
- 在 application-prod.properties 文件中写上:
server.port=80
- 在生产和开发环境往往用的端口是不一样的,接着在 application.properties 文件中增加如下一句话:
spring.profiles.active=prod
- 再次运行项目,则项目将在端口 8888 运行
- 若改成:
spring.profiles.active=dev
- 则打开的端口是 80