本文共 4041 字,大约阅读时间需要 13 分钟。
Spring Boot 读取properties配置文件时,默认读取的是application.properties。
方法一:@ConfigurationProperties注解方式
@Component 表示将该类标识为Bean
@ConfigurationProperties(prefix = "demo")用于绑定属性,其中prefix表示所绑定的属性的前缀。
@PropertySource(value = "config.properties")表示配置文件路径。
import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.springframework.boot.context.properties.ConfigurationProperties;//import org.springframework.context.annotation.PropertySource;import org.springframework.stereotype.Component;/** * @date 2017年6月1日 下午4:34:18 * @version V1.0 * @since JDK : 1.7 */@Component@ConfigurationProperties(prefix = "com.zyd")// PropertySource默认取application.properties// @PropertySource(value = "config.properties")public class PropertiesConfig { public String type3; public Maplogin = new HashMap (); public List urls = new ArrayList<>(); public String getType3() { return type3; } public String getTitle3() { try { return new String(title3.getBytes("ISO-8859-1"), "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return title3; } public Map getLogin() { return login; } public void setLogin(Map login) { this.login = login; } public List getUrls() { return urls; } public void setUrls(List urls) { this.urls = urls; }}
方法二:@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;/** * @date 2017年6月1日 下午3:49:30 * @version V1.0 * @since JDK : 1.7 */@SpringBootApplication@RestControllerpublic class Applaction { @Value("${com.zyd.type}") private String type; /** * * 第二种方式:使用`@Value("${propertyName}")`注解 * */ @RequestMapping("/value") public Mapvalue() throws UnsupportedEncodingException { Map map = new HashMap (); map.put("type", type); return map; } public static void main(String[] args) throws Exception { SpringApplication application = new SpringApplication(Applaction.class); application.run(args); }}
注:如果@Value${}所包含的键名在application.properties配置文件中不存在的话,会抛出异常:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configBeanValue': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'demo.name' in value "${demo.name}"
方法三:Environment
import java.io.UnsupportedEncodingException;import java.util.HashMap;import java.util.Map;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.core.env.Environment;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * @date 2017年6月1日 下午3:49:30 * @version V1.0 * @since JDK : 1.7 */@SpringBootApplication@RestControllerpublic class Applaction { @Autowired private Environment env; /** * * 第三种方式:使用`Environment` * */ @RequestMapping("/env") public Mapenv() throws UnsupportedEncodingException { Map map = new HashMap (); map.put("type", env.getProperty("com.zyd.type2")); return map; } public static void main(String[] args) throws Exception { SpringApplication application = new SpringApplication(Applaction.class); application.run(args); }}
方法四:PropertiesLoaderUtils
转载地址:http://srgaf.baihongyu.com/