【Spring Boot】Spring Boot的系统配置 — 自定义配置项

自定义配置项

本节将介绍Spring Boot实现自定义配置项(也称为配置属性)。在项目开发的过程中,经常需要自定义系统业务方面的配置文件及配置项,Spring Boot如何实现自定义属性配置呢?其实非常简单,Spring Boot提供了**@Value注解**、@ConfigurationProperties注解Environment接口等3种方式自定义配置项。

1.@Value

在实际项目中,经常需要在配置文件中定义一些简单的配置项,Spring Boot提供@Value注解来设置简单的配置项,默认读取application.properties文件中的配置属性。下面通过示例来演示使用@Value注解添加自定义配置项。

首先,在application.properties配置文件中添加自定义配置项:

com.lxmb.costum.firstname=Zhang
com.lxmb.costum.secondname=Weiz

在上面的示例中,我们添加了firstname和secondname两个自定义配置项。然后,在使用的位置调用@Value注解来获取配置项的值:

@Value("${com.lxmb.costum.firstname]")
private String firstName;
@Value("${com.lxmb.costum.secondname]")
private String secondName;

在上面的示例中,通过@Value注解获取了配置文件中对应的配置项的值。

需要注意的是:

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

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

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

2.Environment接口

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

首先,在application.properties配置文件中增加如下的配置项:

com.lxmb.costum.firstname=Zhang
com.lxmb.costum.secondname=Weiz

在上面的示例中,我们在application.properties中配置了firstname和secondname两个自定义配置项。Environment读取的是系统中所有的配置。我们既可以在application.properties中设置自定义的配置项,又可以在自定义配置文件中添加配置项。然后,创建单元测试方法,并注入Environment读取系统配置。示例代码如下:

@Autowired
private Environment env;

@Test
void getEnv() {
    
    
	System.out.printIn(env.getProperty("com.lxmb.costum.firstname"));
	System.out.println(env.getProperty("com.lxmb.costum.secondname"));
}

上面就是Environment使用的示例代码,非常简单。不过,使用Environment时还需要注意以下两点:

1)使用Environment无须指定配置文件,其获取的是系统加载的全部配置文件中的配置项。

2)需要注意配置文件的编码格式,默认为ISO8859-1。

3.@ConfigurationProperties注解

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

3.1创建自定义配置文件

在resources下创建自定义的website.properties配置文件,示例代码如下:

com.lxmb.resource.name=weiz
com.lxmb.resource.website=www.lxmb.com
com.lxmb.resource.language=java

在上面的示例中,创建了自定义的website.properties配置文件。增加了name、website、language等三个配置项,这些配置项的名称的前缀都是com.lxmb.resource。

3.2创建实体类

创建WebSiteProperties自定义配置对象类,然后使用@ConfigurationProperties注解将配置文件中的配置项注入到自定义配置对象类中,示例代码如下:

@Configuration
@ConfigurationProperties(prefix= "com.lxmb.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实体类:

1)@Configuration定义此类为配置类,用于构建bean定义并初始化到Spring容器。

2)@ConfigurationProperties(prefix = “com.lxmb.resource”)绑定配置项,其中prefix表示所绑定的配置项名的前缀。

3)@PropertySource(value = “classpath:website.properties”)指定读取的配置文件及其路径。@PropertySource不支持引入YML文件。通过上面的WebSiteProperties类即可读取全部对应的配置项.

3.3调用配置项

使用配置实体类中的方式也非常简单,只需将WebSiteProperties注入到需要使用的类中,示例代码如下:

@Autowired
private WebSiteProperties website;

@Test
void getProperties() {
    
    
	System.out.printIn(website.getName());
	System.out.println(website.getWebsite());
	System.out.printIn(website.getLanguage());
}

3.4使用配置文件注意事项

在实际项目中会碰到很多读取配置文件的应用场景,需要注意各种坑,否则会让你很惆怅。为此总结了一些使用配置文件时需要注意的事项:

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容器中。

猜你喜欢

转载自blog.csdn.net/weixin_45627039/article/details/131701168