Spring Boot自定义配置属性详解
在项目开发的过程中,经常需要自定义系统业务方面的配置文件及配置项,Spring Boot 如何实现自定义属性配置呢?
其实非常简单,Spring Boot 提供了 @Value 注解、@ConfigurationProperties 注解和 Environment 接口等 3 种方式自定义配置项。
下面通过示例来演示使用 @Value 注解添加自定义配置项。
首先,在 application.properties 配置文件中添加自定义配置项:
然后,在使用的位置调用 @Value 注解来获取配置项的值:
需要注意的是:
使用 Environment 接口时,无须其他的额外配置,只要在使用的类中注入 Environment 即可。
下面通过示例演示 Environment 读取系统自定义的配置项。
首先,在 application.properties 配置文件中增加如下的配置项:
Environment 读取的是系统中所有的配置。我们既可以在 application.properties 中设置自定义的配置项,又可以在自定义配置文件中添加配置项。
然后,创建单元测试方法,并注入 Environment 读取系统配置。示例代码如下:
下面通过示例演示 @ConfigurationProperties 注解如何读取配置文件。
通过上面的 WebSiteProperties 类即可读取全部对应的配置项。
声明:《Java系列教程》为本站“54笨鸟”官方原创,由国家机构和地方版权局所签发的权威证书所保护。
其实非常简单,Spring Boot 提供了 @Value 注解、@ConfigurationProperties 注解和 Environment 接口等 3 种方式自定义配置项。
@Value注解
在实际项目中,经常需要在配置文件中定义一些简单的配置项,Spring Boot 提供 @Value 注解来设置简单的配置项,默认读取 application.properties 文件中的配置属性。下面通过示例来演示使用 @Value 注解添加自定义配置项。
首先,在 application.properties 配置文件中添加自定义配置项:
com.weiz.costum.firstname=Zhang com.weiz.costum.secondname=Weiz在上面的示例中,我们添加了 firstname 和 secondname 两个自定义配置项。
然后,在使用的位置调用 @Value 注解来获取配置项的值:
@Value("${com.weiz.costum.firstname}") private String firstName; @Value("${com.weiz.costum.secondname}") private String secondName;在上面的示例中,通过 @Value 注解获取了配置文件中对应的配置项的值。
需要注意的是:
- 使用 @Value 注解时,所在类必须被 Spring 容器管理,也就是使用 @Component、@Controller、@Service 等注解定义的类;
- @Value 需要传入完整的配置项的 Key 值;
- @Value 注解默认读取 application.properties 配置文件,如果需要使用其他的配置文件,可以通过 @PropertySource 注解指定对应的配置文件。
Environment接口
Environment 是 Spring 为运行环境提供的高度抽象的接口,它会自动获取系统加载的全部配置项,包括命令行参数,系统属性,系统环境,随机数,配置文件等。使用 Environment 接口时,无须其他的额外配置,只要在使用的类中注入 Environment 即可。
下面通过示例演示 Environment 读取系统自定义的配置项。
首先,在 application.properties 配置文件中增加如下的配置项:
com.weiz.costum.firstname=Zhang com.weiz.costum.secondname=Weiz在上面的示例中,我们在 application.properties 中配置了 firstname 和 secondname 两个自定义配置项。
Environment 读取的是系统中所有的配置。我们既可以在 application.properties 中设置自定义的配置项,又可以在自定义配置文件中添加配置项。
然后,创建单元测试方法,并注入 Environment 读取系统配置。示例代码如下:
@Autowired private Environment env; @Test void getEnv() { System.out.println(env.getProperty("com.weiz.costum.firstname")); System.out.println(env.getProperty("com.weiz.costum.secondname")); }上面就是 Environment 使用的示例代码,非常简单。不过,使用 Environment 时还需要注意以下两点:
- 使用 Environment 无须指定配置文件,其获取的是系统加载的全部配置文件中的配置项;
- 需要注意配置文件的编码格式,默认为 ISO8859-1。
@ConfigurationProperties
在实际项目开发中,需要注入的配置项非常多时,前面所讲的 @value 和 Environment 两种方法就会比较烦琐。这时可以使用注解 @ConfigurationProperties 将配置项和实体 Bean 关联起来,实现配置项和实体类字段的关联,读取配置文件数据。下面通过示例演示 @ConfigurationProperties 注解如何读取配置文件。
1) 创建自定义配置文件
在 resources 下创建自定义的 website.properties 配置文件,示例代码如下:com.weiz.resource.name=weiz com.weiz.resource.website=www.weiz.com com.weiz.resource.language=java在上面的示例中,创建了自定义的 website.properties 配置文件。增加了 name、website、language 等三个配置项,这些配置项的名称的前缀都是 com.weiz.resource。
2) 创建实体类
创建 WebSiteProperties 自定义配置对象类,然后使用 @ConfigurationProperties 注解将配置文件中的配置项注入到自定义配置对象类中,示例代码如下:@Configuration @ConfigurationProperties(prefix = "com.weiz.resource") @PropertySource(value = "classpath:website.properties") public class WebSiteProperties { 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 类即可读取全部对应的配置项。
3) 调用配置项
使用配置实体类中的方式也非常简单,只需将 WebSiteProperties 注入到需要使用的类中,示例代码如下:@Autowired private WebSiteProperties website; @Test void getProperties() { System.out.println(website.getName()); System.out.println(website.getWebsite()); System.out.println(website.getLanguage()); }
使用配置文件注意事项
在实际项目中会碰到很多读取配置文件的应用场景,需要注意各种坑,否则会让你很惆怅。所以,我总结了一些使用配置文件时需要注意的事项:- 使用 YML 文件时注意空格和格式缩进;
- Properties 文件默认使用的是 ISO8859-1 编码格式,容易出现乱码问题。如果含有中文,加入 spring.http.encoding.charset=UTF-8 配置即可;
- Properties 配置的优先级高于 YML 文件。因为 YML 文件的加载顺序先于 Properties 文件,如果两个文件存在相同的配置,后面加载的 Properties 中的配置会覆盖前面 YML 中的配置;
- @PropertySource 注解默认只会加载 Properties 文件,YML 文件不能使用此注解;
- 简单值推荐使用 @Value,复杂对象推荐使用 @ConfigurationProperties;
- 只有 Spring 容器中的组件才能使用容器提供的各类方法,所以,配置读取类需要增加 @Component 注解才能加入 Spring 容器中。
声明:《Java系列教程》为本站“54笨鸟”官方原创,由国家机构和地方版权局所签发的权威证书所保护。