一、定制Banner

Spring Boot项目在启动的时候会有一个默认的启动图案:

我们可以把这个图案修改为自己想要的。在src/main/resources目录下新建banner.txt文件,然后将自己的图案黏贴进去即可。ASCII图案可通过网站 http://www.network-science.de/ascii/http://patorjk.com/software/taag/https://www.bootschool.net/asciihttps://www.degraeve.com/img2txt.php 一键生成,比如输入wno生成图案后复制到banner.txt,启动项目,idea控制台输出如下:

banner也可以关闭,在main方法中:

1
2
3
4
5
public static void main(String[] args) {
SpringApplication app = new SpringApplication(BootApplication.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}

二、全局配置文件

在src/main/resources目录下,Spring Boot提供了一个名为application.properties的全局配置文件,可对一些默认配置的配置值进行修改。

2.1 自定义属性值

Spring Boot允许我们在application.properties下自定义一些属性,比如:

1
2
wno704.blog.name=wno704's blog
wno704.blog.title=Spring Boot

定义一个BlogProperties Bean,通过@Value("${属性名}")来加载配置文件中的属性值:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Component
public class BlogProperties {
@Value("${wno704.blog.name}")
private String name;

@Value("${wno704.blog.title}")
private String title;

public void setName(String name){
this.name = name;
}

public String getName(){
return this.name;
}

public void setTitle(String title){
this.title = title;
}

public String getTitle(){
return this.title;
}
}

编写IndexController,注入该Bean:

1
2
3
4
5
6
7
8
9
10
@RestController
public class IndexController {
@Autowired
private BlogProperties blogProperties;

@RequestMapping("/")
String index() {
return blogProperties.getName()+"——"+blogProperties.getTitle();
}
}

启动项目,访问 http://localhost:8080 ,页面显示如下:

在属性非常多的情况下,也可以定义一个和配置文件对应的Bean:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@ConfigurationProperties(prefix="wno704.blog")
public class ConfigBean {
private String name;

private String title;

public void setName(String name){
this.name = name;
}

public String getName(){
return this.name;
}

public void setTitle(String title){
this.title = title;
}

public String getTitle(){
return this.title;
}
}

通过注解@ConfigurationProperties(prefix="mrbird.blog")指明了属性的通用前缀,通用前缀加属性名和配置文件的属性名一一对应。

除此之外还需在Spring Boot入口类加上注解@EnableConfigurationProperties({ConfigBean.class})来启用该配置:

1
2
3
4
5
6
7
8
9
10
11
@SpringBootApplication
@EnableConfigurationProperties({ConfigBean.class})
public class BootApplication {

public static void main(String[] args) {
SpringApplication app = new SpringApplication(BootApplication.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}

}

之后便可在IndexController中注入该Bean,并使用了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@RestController
public class IndexController {
@Autowired
private BlogProperties blogProperties;

@Autowired
private ConfigBean configBean;

@RequestMapping("/")
String index() {
return blogProperties.getName()+"——"+blogProperties.getTitle() +
"\r\n configBean:" + configBean.getName()+"——"+configBean.getTitle();
}
}

2.2 属性间的引用

在application.properties配置文件中,各个属性可以相互引用,如下:

1
2
3
wno704.blog.name=wno704's blog
wno704.blog.title=Spring Boot
wno704.blog.wholeTitle=${wno704.blog.name}--${wno704.blog.title}

2.3 自定义配置文件

除了可以在application.properties里配置属性,我们还可以自定义一个配置文件。在src/main/resources目录下新建一个test.properties:

1
2
test.name=KangKang
test.age=25

定义一个对应该配置文件的Bean:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@Configuration
@ConfigurationProperties(prefix="test")
@PropertySource("classpath:test.properties")
@Component
public class TestConfigBean {
private String name;

private int age;

public void setName(String name){
this.name = name;
}

public String getName(){
return this.name;
}

public void setAge(int age){
this.age = age;
}

public int getAge(){
return this.age;
}
}

注解@PropertySource("classpath:test.properties")指明了使用哪个配置文件。要使用该配置Bean,同样也需要在入口类里使用注解@EnableConfigurationProperties({TestConfigBean.class})来启用该配置。

三、通过命令行设置属性值

在运行Spring Boot jar文件时,可以使用命令java -jar xxx.jar --server.port=8081来改变端口的值。这条命令等价于我们手动到application.properties中修改(如果没有这条属性的话就添加)server.port属性的值为8081。

如果不想项目的配置被命令行修改,可以在入口文件的main方法中进行如下设置:

1
2
3
4
5
public static void main(String[] args) {
SpringApplication app = new SpringApplication(Application.class);
app.setAddCommandLineProperties(false);
app.run(args);
}

四、使用xml配置

虽然Spring Boot并不推荐我们继续使用xml配置,但如果出现不得不使用xml配置的情况,Spring Boot允许我们在入口类里通过注解@ImportResource({"classpath:some-application.xml"})来引入xml配置文件。

五、Profile配置

Profile用来针对不同的环境下使用不同的配置文件,多环境配置文件必须以application-{profile}.properties的格式命,其中{profile}为环境标识。比如定义两个配置文件:

application-dev.properties:开发环境

server.port=8080

application-prod.properties:生产环境

server.port=8081

至于哪个具体的配置文件会被加载,需要在application.properties文件中通过spring.profiles.active属性来设置,其值对应{profile}值。

如:spring.profiles.active=dev就会加载application-dev.properties配置文件内容。可以在运行jar文件的时候使用命令java -jar xxx.jar --spring.profiles.active={profile}切换不同的环境配置。

代码下载