SpringBoot 02 外部配置文件applicatiion.properties


全局配置文件

SpringBoot使用application.properties或者applicaiton.yaml文件作为全局配置文件,文件位置一般在src/main/resource目录下,使用Spring Initializer方式构建或者https://start.spring.io/ 构建Spring Boot项目后会在src/main/resource目录下自动创建一个名称为applicatiion.properties 文件

    src
      ├─main
      │  ├─java
      │  └─resources
      │          application.properties
      └─test

application文件可以实现定义Spring Boot 项目的相关属性,可以是系统属性,命令参数,变量等,applicatiion.properties和application.yamal 文件中可以定义的内容是相同的,只是格式不同, 例如上文提到可以修改server.prot属性将默认端口从8080修改为其他端口

application.properties定义端口,使用“key=value”格式

server.port=9090

application.yaml定义端口,使用 “key: 空格value ”格式

server:
  port: 9090

  • YAML文件的扩展名可以使用.yml或者 .yaml
  • application.yml文件使用 “key: 空格value ”格式,使用锁紧控制层级关系所以看起来更直观简洁一些


读取全局配置文件

读取全局配置有两种方式

  • 使用@Value注入属性,读取配置文件中的属性逐个注入到对应的属性中,可省略setXX方法
  • 使用@Component + @ConfigurationProperties注入配置文件属性,实现将配置文件中的属性映射到类组件中

1.@Value注入方式

application.yaml配置文件中自定义属性

##自定义属性用于测试
project:
  author:
    name: PNZ.BeijingL
    from: StarCraft PNZ TEAM

创建PropertiesController对象使用@Value注入属性信息

package com.boot.basic.conf;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PropertiesController {

    @Value("${project.author.name}")
    private String author;
    @Value("${project.author.from}")
    private String from;

    /**
     *方式 使用@Value注入属性,读取配置文件中的属性逐个注入到对应的属性中,可省略setXX方法
     * @return
     */
    @RequestMapping("/author")
    public String getAuthor() {
        return "getAuthor: Name:" + author + " from:" + from;
    }

}

启动后访问 http://127.0.0.1:8080/author

2.@Component + @ConfigurationProperties注入方式

修改application.yaml配置文件中自定义属性

##自定义属性用于测试
project:
  name: Demo for Spring Boot
  version: 0.0.1
  author:
    name: PNZ.BeijingL
    from: StarCraft PNZ TEAM

创建项目信息类

  • @Component //将本类作为Bean组件放到Spring容器中,
  • @ConfigurationProperties(prefix = "project") //通过注解prefix属性指定properites的匹配位置
package com.boot.basic.conf;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;


@Component 
@ConfigurationProperties(prefix = "project") 
public class ProjectSettings {

    private String name;
    private String version;
    
    //省略getter和setter
    
    @Override
    public String toString() {
        return "ProjectSettings{" +
                "name='" + name + '\'' +
                ", version='" + version + '\'' +
                '}';
    }
}

修改PropertiesController对象注入ProjectSettings 对象bean


@RestController
public class PropertiesController {

    @Value("${project.author.name}")
    private String author;
    @Value("${project.author.from}")
    private String from;

    @Autowired
    private ProjectSettings settings;

    /**
     *方式 使用@Value注入属性,读取配置文件中的属性逐个注入到对应的属性中,可省略setXX方法
     * @return
     */
    @RequestMapping("/author")
    public String getAuthor() {
        return "getAuthor: Name:" + author + " from:" + from;
    }

    /**
     * 方式 使用@Component + @ConfigurationProperties注入配置文件属性,实现将配置文件中的属性映射到projectSettings类组件中
     * @return
     */
    @RequestMapping("/project")
    public String getProject() {
        return "getProject: "+settings.toString();
    }

}

 启动后访问


自定义配置文件

resource目录下创建自定义文件myapplication.properties

demo.conf.test.name=zhangsan
demo.conf.test.level=99

定义配置对象MyProperties

  • @Configuration 注解表示当前类是一个自定义配置类,将其看做是Spring容器组件
  • @PropertySource("classpath:myapplication.properties") 注解指定了自定义配置文件的位置和名称
  • @EnableConfigurationProperties(MyProperties.class) , 注解使使用 @ConfigurationProperties 注解的类生效。
  • @ConfigurationProperties(prefix = "demo.conf.test") 注解将自定义配置文件中以demo.conf.test开头的属性注入到配置属性中
package com.boot.basic.conf;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:myapplication.properties")
@EnableConfigurationProperties(MyProperties.class)
@ConfigurationProperties(prefix = "demo.conf.test")
public class MyProperties {

    private String name;
    private int level;

    //省略getter和setter

    @Override
    public String toString() {
        return "MyProperties{" +
                "name='" + name + '\'' +
                ", level=" + level +
                '}';
    }
}

修改PropertiesController 对象注入MyProperties 

@RestController
public class PropertiesController {
    
    //省略

    @Autowired
    private MyProperties myProperties;

    @RequestMapping("/myconf")
    public String getMyProperties() {
        return myProperties.toString();
    }
}

访问http://127.0.0.1:8080/myconf 

如果属性配置不对时,String为null字符串,  int会取默认值0


多环境配置文件

SpringBoot资源目录下 src/main/resource,可以创建多个环境的配置文件

├─main
│  ├─java
│  │
│  └─resources
│          application-dev.properties  //开发环境
│          application-prod.properties //生产环境
│          application-test.properties //测试环境
│          application.properties  //全局配置文件
│          myapplication.properties //自定义配置文件
│
└─test

 在application.properties文件中  通过spring.profiles.active属性启动使用的环境配置文件,这样就很容易切换开发环境、测试环境的配置, 例如

spring.profiles.active=dev

上一篇:SpringBoot 01 快速入门

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

=PNZ=BeijingL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值