【Spring Boot】springboot中自定义配置项

2. 自定义配置项

在项目开发的过程中,经常需要自定义系统业务方面的配置文件及配置项,Spring Boot提供了@value注解、@ConfigurationProperties注解和Environment接口等3种方式自定义配置项。

@value

在实际项目中,经常需要在配置文件中定义一些简单的配置项,Spring Boot提供@Value注解来设置简单的配置项,默认读取application.properties文件中的配置属性。

我们在application.properties配置文件下自定义配置属性。

在这里插入图片描述

然后在使用的位置调用@Value注解来获取配置项的值,如下所示:

@Value("${com.jingt.name.firstName}")
    private String firstName;

    @Value("${com.jingt.name.secondName}")
    private String secondName;
    
    public String testValue(){
        return firstName + "|" + secondName;
    }
@Autowired
private HelloServices helloServices;

 @Test
    void testValue() {
        System.out.println(helloServices.testValue());
    }

在这里插入图片描述

注意:

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

  2. @Value需要传入完整的配置项的Key值。

  3. @Value注解默认读取application配置文件,如果需要使用其他的配置文件,可以通过@PropertySource注解指定对应的配置文件。

    在启动类上加注解@PropertySource

@SpringBootApplication
@PropertySource(value = {"classpath:application_test.properties"})
public class HelloworldApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloworldApplication.class, args);
    }
}

在application_test.properties 配置文件中加入

com.jingt.name.firstName=guo1
com.jingt.name.thirdName=jingt1
@Value("${com.jingt.name.firstName}")
private String firstName;

@Value("${com.jingt.name.thirdName}")
private String secondName;

public String testValue(){
    return firstName + "|" + secondName;
}

把secondName改为thirdName,firstName从guo改为guo1

在这里插入图片描述

会发现thirdName也已经获取到了,firstName还是为guo,这是因为application.properties配置文件加载的优先级的原因,application.properties会覆盖application_test.properties对应文件中的值。

Environment接口

Environment是Spring为运行环境提供的高度抽象的接口,它会自动获取系统加载的全部配置项,包括命令行参数,系统属性,系统环境,随机数,配置文件等。使用时无须其他的额外配置,只要在使用的类中注入Environment即可。

在application.properties文件中增加自定义配置项

com.jingt.name.firstName=guo
com.jingt.name.secondName=jingt

Environment读取的是系统中所有的配置。我们既可以在application.properties中设置自定义的配置项,又可以在自定义配置文件中添加配置项。

Environment对象注入,获取系统配置

   @Autowired
    private Environment env;

    @Test
    void getEnv(){
        System.out.println(env.getProperty("com.jingt.name.firstName"));
        System.out.println(env.getProperty("com.jingt.name.secondName"));
    }

引入的是import org.springframework.core.env.Environment;,不要引入错了。

在这里插入图片描述

使用Environment时还需要注意以下两点:

  1. 使用Environment无须指定配置文件,其获取的是系统加载的全部配置文件中的配置项。
  2. 需要注意配置文件的编码格式,默认为ISO8859-1。

@ConfigurationProperties

在实际项目开发中,需要注入的配置项非常多时,@value和Environment两种方法就会比较烦琐。这时可以使用注解@ConfigurationProperties将配置项和实体Bean关联起来,实现配置项和实体类字段的关联,读取配置文件数据。

在resources下创建自定义的website.properties配置文件,增加配置属性。

com.jingt.resource.name=jingt
com.jingt.resource.website=com.jingt.com
com.jingt.resource.language=java

创建一个配置类

在这里插入图片描述

@Configuration
@ConfigurationProperties(prefix = "com.jingt.resource")
@PropertySource(value = "classpath:website.properties")
public class ResourceConfig {
    private String name;
    private String website;
    private String language;

    public String getName() {
        return name;
    }

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

    public String getWebsite() {
        return website;
    }

    public void setWebsite(String website) {
        this.website = website;
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }
}

我们使用了@Configuration注解、@ConfigurationProperties和@PropertySource三个注解来定义WebSiteProperties实体类:

  • @Configuration定义此类为配置类,用于构建bean定义并初始化到Spring容器。
  • @ConfigurationProperties(prefix = “com.weiz.resource”)绑定配置项,其中prefix表示所绑定的配置项名的前缀。
  • @PropertySource(value = “classpath:website.properties”)指定读取的配置文件及其路径。@PropertySource不支持引入YML文件。

通过上面的WebSiteProperties类即可读取全部对应的配置项。

在单元测试中,我们测试是否可以通过

@Autowired
    private ResourceConfig config;

    @Test
    void getResourceConfig(){
        System.out.println(config.getName());
        System.out.println(config.getWebsite());
        System.out.println(config.getLanguage());
    }

在这里插入图片描述

使用配置文件注意事项

  1. 使用YML文件时注意空格和格式缩进。
  2. Properties文件默认使用的是ISO8859-1编码格式,容易出现乱码问题。如果含有中文,加入spring.http.encoding.charset=UTF-8配置即可。
  3. Properties配置的优先级高于YML文件。因为YML文件的加载顺序先于Properties文件,如果两个文件存在相同的配置,后面加载的Properties中的配置会覆盖前面YML中的配置。
  4. @PropertySource注解默认只会加载Properties文件,YML文件不能使用此注解。
  5. 简单值推荐使用@Value,复杂对象推荐使用@ConfigurationProperties。
  6. 只有Spring容器中的组件才能使用容器提供的各类方法,所以,配置读取类需要增加@Component注解才能加入Spring容器中。
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值