SpringBoot基本配置详解

我会带着你远行 2023-06-15 04:53 148阅读 0赞

SpringBoot项目有一些基本的配置,比如启动图案(banner),比如默认配置文件application.properties,以及相关的默认配置项。

示例项目代码在:https://github.com/laolunsi/spring-boot-examples


一、启动图案banner

编写banner.txt放入resources文件夹下,然后启动项目即可修改默认图案。

关于banner的生成,可以去一些专门的网站。

比如:https://www.bootschool.net/ascii

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI4Mzc5ODA5_size_16_color_FFFFFF_t_70


二、配置文件application

2.1 application.properties/yml

resources下通常会默认生成一个application.properties文件,这个文件包含了SpringBoot项目的全局配置文件。里面的配置项通常是这样的:

  1. server.port=8080

在这个文件里我们可以添加框架支持的配置项,比如项目端口号、JDBC连接的数据源、日志级别等等。

现在比较流行的是将properties文件改为yml文件。yml文件的格式yaml是这样的:

  1. server:
  2. port: 8080

yml和properties的作用是一样的。而yml的好处是显而易见的——更易写易读。

属性之间互相调用使用${name}:

  1. eknown:
  2. email: eknown@163.com
  3. uri: http://www.eknown.cn
  4. title: 'hello, link to ${eknown.uri} or email to ${eknown.email}'

链接:SpringBoot所有官方配置属性


2.2 多环境配置文件

通常开发一个应用会有多个环境,常见如dev/prod,也会有test,甚至其他一些自定义的环境,SpringBoot支持配置文件的灵活切换。

定义新配置文件需要遵循以下格式:application-{profile}.properties 或者application-{profile}.yml

比如现在有dev和prod两个环境,我需要在application.yml文件之外新建两个文件:

  1. application-dev.yml

    1. server:
    2. port: 8080
  2. application-prod.yml

    1. server:
    2. port: 8081

然后在application.yml中通过application.profiles.active={profile}指明启用那个配置:

  1. application:
  2. profiles:
  3. active: dev

除了在application.yml中指定配置文件外,还可以通过启动命令指定:java -jar xxx.jar --spring.profiles.active=dev


2.2 自定义配置项并获取它

主要介绍两种方式,获取单个配置项和获取多个配置项。

举例:

  1. eknown:
  2. email: eknown@163.com
  3. uri: http://www.eknown.cn

2.2.1 使用@Value注解获取单个配置项

  1. @Value("${eknown.email}")
  2. private String email;
  3. @Value("${eknown.uri}")
  4. private String url;

注意:使用@Value注解的时候,所在类必须被Spring容器管理,也就是被@Component、@Controller、@Service等注解定义的类。

2.2.2 获取多个配置项

第一种,定义一个bean类,通过@Value获取多个配置项:

  1. @Component
  2. public class MyBean {
  3. @Value("${eknown.email}")
  4. private String email;
  5. @Value("${eknown.uri}")
  6. private String uri;
  7. public String getEmail() {
  8. return email;
  9. }
  10. public String getUri() {
  11. return uri;
  12. }
  13. }

然后我们通过get方法来获取这些值:

  1. @RestController
  2. public class IndexAction {
  3. @Autowired
  4. private MyBean bean;
  5. @GetMapping(value = "bean")
  6. public String getConcatByBean() {
  7. return "from bean: <br />" + bean.getEmail() + "<br />" + bean.getUri();
  8. }
  9. }

第二种,使用注解@ConfigurationProperties:

  1. @Component
  2. @ConfigurationProperties(perfix="eknown")
  3. public class MyConfigBean {
  4. private String email;
  5. private String uri;
  6. }

这里只需要通过prefix指定前缀即可,后面的值自动匹配。

这里我们还使用了@Component注解来让spring容器管理这个MyConfigBean。

此外,我们可以不需要引入@Component,转而在Application启动类上加上@EnableConfigurationProperties({MyConfigBean.class})来启动这个配置。

注意:我们这里是从主配置文件,也就是SpringBoot默认的application-profile文件中获取配置数据的。

而从自定义的配置文件,比如test.yml这种形式中获取配置项时,情况是有点不大一样的。


三、自定义配置文件

上面介绍的配置文件都是springboot默认的application开头的文件。如果要自定义一个配置文件呢,比如test.yml或test.properties,怎么获取其中的配置项呢?

使用**@PageResource**注解即可。

首先我们来看一下读取自定义的properties文件里的内容:

test.properties

  1. hello.time=2019.11.19
  2. hello.name=eknown

定义Configuration类:

  1. @Configuration
  2. @PropertySource("classpath:test.properties")
  3. //@PropertySource("classpath:test.yml") // 注意,yml文件不能直接这样写,会读不出数据
  4. @ConfigurationProperties(prefix = "hello")
  5. public class TestConfiguration {
  6. private String name;
  7. private String time;
  8. // hide get and set methods
  9. }

测试一下:

  1. @RestController
  2. @RequestMapping(value = "test")
  3. public class TestAction {
  4. @Autowired
  5. private TestConfiguration testConfiguration;
  6. @GetMapping(value = "config")
  7. public String test() {
  8. return testConfiguration.getName() "<br/>" testConfiguration.getTime();
  9. }
  10. }

2019112012560176.png


如果将properties文件换成yml文件呢?

我们尝试一下,发现:

20191120125601273.png

读不出数据?

分析一下@PropertySource注解,发现其使用的PropertySourceFactory是DefaultPropertySourceFactory.

这个类的源码如下:

  1. public class DefaultPropertySourceFactory implements PropertySourceFactory {
  2. public DefaultPropertySourceFactory() {
  3. }
  4. public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException {
  5. return name != null ? new ResourcePropertySource(name, resource) : new ResourcePropertySource(resource);
  6. }
  7. }

这个类只能处理properties文件,无法处理yml文件。所以我们需要自定义一个YmlSourceFactory。

  1. public class YamlSourceFactory extends DefaultPropertySourceFactory {
  2. @Override
  3. public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
  4. return new YamlPropertySourceLoader().load(resource.getResource().getFilename()
  5. , resource.getResource()).get(0);
  6. }
  7. }

然后定义test.yml文件的config类:

  1. @Configuration
  2. @PropertySource(value = "classpath:test.yml", encoding = "utf-8", factory = YamlSourceFactory.class)
  3. @ConfigurationProperties(prefix = "yml.hello")
  4. public class TestYamlConfiguration {
  5. private String name;
  6. private String time;
  7. // hide get and set methods
  8. }

注:为了区分test.properties和test.yml,这里的test.yml中的属性以yml.hello开头。

编写一下测试:

  1. @Autowired
  2. private TestYamlConfiguration ymlConfiguration;
  3. @GetMapping(value = "yml")
  4. public String testYml() {
  5. return "yml config: <br/>" ymlConfiguration.getName() "<br/>" ymlConfiguration.getTime();
  6. }

访问:

20191120125601483.png


四、补充@ConfigurationProperties

网上一些资料中,为配合使用@ConfigurationProperties,还使用了@EnableConfigurationProperties注解。

经过测试发现:

  1. 从SpringBoot默认配置文件读取配置信息,使用@ConfigurationProperties + @Component/@Configuration,或者@ConfigurationProperties + 在启动类添加@EnableConfigurationProperties({class})。这两种方式都能解决问题
  2. 从非默认配置文件读取配置信息,需要利用@PropertySource注解。同样两种方式:

    2.1 @PropertySource + @ConfigurationProperties + @Component/@Configuration

    2.2 @PropertySource + @ConfigurationProperties + @Component/@Configuration + @EnableConfigurationProperties,第二种方式存在一个问题,即还是必须要使用@Component注解,如果不使用,则会导致读取配置信息为null,但程序不会报错;而如果采用了,则会导致bean类的set方法被执行两次(也就是生成了两个同样类型的bean类)。这种方式不建议!


交流学习

个人网站:http://www.eknown.cn

GitHub:https://github.com/laolunsi

公众号:猿生物语,“分享技术,也感悟人生”,欢迎关注!
watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI4Mzc5ODA5_size_16_color_FFFFFF_t_70 1

发表评论

表情:
评论列表 (有 0 条评论,148人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Redis基本配置详解

    redis的事务处理 众所周知,事务是指“一个完整的动作,要么全部执行,要么什么也没有做”。 在聊redis事务处理之前,要先和大家介绍四个redis指令,即MULTI