当前选择的SpringBoot的版本:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> <relativePath /> </parent> |
若新增的配置信息,写在application.properties,则不需要使用注解@PropertySource载入文件。
反之,若是自定义的properties文件,则需要手动载入,在配置类加注解@PropertySource来载入,例如:
自定义properties文件:(yml文件不支持注解@PropertySource)
在spring boot启动类或配置类加注解,可启动时载入自定义的配置文件(写法有多种)
@PropertySource("classpath:config/xxx.properties")
@PropertySource(value = "classpath:/a.properties")
@PropertySources(@PropertySource(value = "classpath:/service.properties"))
@PropertySources(@PropertySource(value = "classpath:/service.properties",ignoreResourceNotFound = true, encoding = "UTF-8"))
若同时载入多个文件
@PropertySource(value={"classpath:config/a.properties","classpath:config/b.properties"})
自定义如下:
service.properties
info.name=kikiname info.age=12 |
配置类:
1、@Value注解方式读取
@Component @PropertySource(value = "classpath:/service.properties") public class ServerPropertiesTwo { @Value("${info.name}") private String name; @Value("${info.age}") private Integer age; public String getName() { return name; } public Integer getAge() { return age; } } |
2、@ConfigurationPropterties注解读取方式-批量注入到类变量
在application.properties 中配置以info为前缀的参数
在java代码中用@ConfigurationPropterties将以info为前缀的参数注入到当前变量中,需要用setXXX()方法
@Component @PropertySource(value = "classpath:/service.properties") @ConfigurationProperties(prefix = "info") public class PropertiesConfigurationService { private String name; private Integer age; public String getName() { return name; } public Integer getAge() { return age; } public void setName(String name) { this.name = name; } public void setAge(Integer age) { this.age = age; } } |
3、Environment读取方式
以上所有加载出来的配置都可以通过Environment注入获取到
import org.springframework.core.env.Environment;
@Autowired
private Environment env;
// 获取参数
String value = env.getProperty(String key);
package com.kiki.info.resource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.kiki.info.config.util.readproperties.PropertiesConfigurationService; import com.kiki.info.config.util.readproperties.PropertiesStateReadService; import com.kiki.info.config.util.readproperties.PropertiesValueService; import com.kiki.info.util.business.CommonResponse; /** * 读取配置文件的俩种方式 * * @ClassName: MessageReource * @author kiki * @date 2018年10月8日 * @version: V1.0 */ @RestController @RequestMapping("/message") public class PropertiesResource { private static Logger logger = LoggerFactory .getLogger(PropertiesResource.class); @Autowired private Environment env; /** * * getinfo1 获取属性值方式不同 * * @return 参数 CommonResponse 返回类型 */ @GetMapping("/getname") public CommonResponse getinfo1() { System.out.println(env.getProperty("info.name")); String value = propertiesValueService.getName(); logger.info("@Value name:" + value); logger.info("@ConfigurationProperties name:" + propertiesConfigurationService.getName()); logger.info("static @Value AGE:" + PropertiesStateReadService.AGE + " NAME:" + PropertiesStateReadService.NAME); return CommonResponse.getInstance(value); } @Autowired PropertiesValueService propertiesValueService; @Autowired PropertiesConfigurationService propertiesConfigurationService; } |
4、还有一种特别的读取方式:通过类名静态去获取。
这种方式的有效性与 spring-boot-starter-parent版本有关,若是
<version>1.5.2.RELEASE</version>
则读取不到。当前测试版本使用的是 <version>2.0.5.RELEASE</version>能够读取到。
@Component @PropertySources(@PropertySource(value = "classpath:/service.properties")) public class PropertiesStateReadService { private static Logger logger = LoggerFactory .getLogger(PropertiesStateReadService.class); public static String NAME = "name"; public static Integer AGE = 1; @Value("${info.age}") public void setAGE(Integer aGE) { AGE = aGE; } @Value("${info.name}") public void setNAME(String nAME) { logger.info("save name ................"); NAME = nAME; } } |