javascript
Spring Boot——读取.properties配置文件解决方案
解決方案
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 Map<String, String> login = new HashMap<String, String>();public List<String> 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<String, String> getLogin() {return login;}public void setLogin(Map<String, String> login) {this.login = login;}public List<String> getUrls() {return urls;}public void setUrls(List<String> 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 @RestController public class Applaction {@Value("${com.zyd.type}")private String type;/*** * 第二種方式:使用`@Value("${propertyName}")`注解* */@RequestMapping("/value")public Map<String, Object> value() throws UnsupportedEncodingException {Map<String, Object> map = new HashMap<String, Object>();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 @RestController public class Applaction {@Autowiredprivate Environment env;/*** * 第三種方式:使用`Environment`* */@RequestMapping("/env")public Map<String, Object> env() throws UnsupportedEncodingException {Map<String, Object> map = new HashMap<String, Object>();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
https://blog.csdn.net/thc1987/article/details/78789426
參考文章
https://blog.csdn.net/thc1987/article/details/78789426
https://blog.csdn.net/dkbnull/article/details/81953190
https://blog.csdn.net/liuchuanhong1/article/details/78106648
?
總結
以上是生活随笔為你收集整理的Spring Boot——读取.properties配置文件解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Boot——[Spring
- 下一篇: JAVA——基于HttpClient的正