Java web · 2019年7月11日 0

Spring boot 读取properties

## Spring boot 读取properties

> 在开发中我们需要通过属性文件配置常用属性,例如数据库相关、日志相关、测试相关等。
>
> 1. 自定义properties文件获取属性
> 2. application.properties获取属性

### 自定义properties文件获取属性

> 使用@configurationProperties((prefix=”xxx.yyy”)) 和 @PropertySource(“classpath:xxxconfig.properties”)
>
> xxx.yyy表示的是属性文件中的前缀(有时候我们希望可以分的更清楚)
>
> 使用自定义的时候需要给类添加@Component 让Spring管理类的生命周期
>
> 在使用的地方使用@Autowired 让系统进行初始化
>
> 1. // PropertySource默认取application.properties
> 2. // @PropertySource(value = “xxxconfig.properties”)

例如

“`
@Component
@ConfigurationProperties(prefix = “com.xxx”)
@PropertySource(“classpath:myconfig.properties”)
public class TestBean {
private String host;

public String getHost() {
return host;
}

public void setHost(String host) {
this.host = host;
}
}

//使用地方

@Autowired
private TestBean testBean
“`

### application.properties获取属性

> 共有三种
>
> 1. 参考上面自定义 只是不用设置PropertySource PropertySource默认取application.properties
> 2. 使用@Value注解
> 3. 使用Environment

#### 使用@Value

“`
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class Applaction {
@Value(“{com.zyd.type}”) private String type; @Value(“{com.zyd.title}”) private String title;
/** * * 第二种方式:使用`@Value(“${propertyName}”)`注解 *
* @throws UnsupportedEncodingException * @since JDK 1.7 */
@RequestMapping(“/value”) public Map value() throws UnsupportedEncodingException {
Map map = new HashMap();
map.put(“type”, type);
// *.properties文件中的中文默认以ISO-8859-1方式编码,因此需要对中文内容进行重新编码
map.put(“title”, new String(title.getBytes(“ISO-8859-1”), “UTF-8”));
return map;
}
public static void main(String[] args) throws Exception {
SpringApplication application = new SpringApplication(Applaction.class);
application.run(args);
}
}
“`

#### 使用Environment

“`
方式一 通过Context获取Environment
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class Application {
static Logger logger = LogManager.getLogger(WljcbaseApplication.class);
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(WljcbaseApplication.class, args);
logger.info(“启动成功!”);
logger.trace(“这是trace日志…”);
logger.debug(“这是debug日志…”);
// Springboot默认给我们使用的是info级别的,没有指定级别的就用SpringBoot默认规定的级别,root级别
logger.info(“这是info日志…”);
logger.warn(“这是warn日志…”);
logger.error(“这是error日志…”);
String host = context.getEnvironment().getProperty(“host”);
logger.error(“host is ” + host);
}

}
方式二 通过注解获取Environment
@SpringBootApplication
@RestController
public class Applaction {
@Autowired private Environment env;
@RequestMapping(“/env”) public Map env() throws UnsupportedEncodingException {
Map map = new HashMap();
map.put(“type”, env.getProperty(“com.zyd.type2”));
map.put(“title”, new String(env.getProperty(“com.zyd.title2”).getBytes(“ISO-8859-1”), “UTF-8”));
return map;
}
public static void main(String[] args) throws Exception {
SpringApplication application = new SpringApplication(Applaction.class);
application.run(args);
}
}
“`

Share this: