Spring Boot——读取.properties配置文件解决方案
程序员文章站
2022-05-02 11:41:19
...
解决方案
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 {
@Autowired
private 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
下一篇: 线程池源码中retry的理解
推荐阅读
-
spring boot application properties配置实例代码详解
-
详解spring boot starter redis配置文件
-
spring boot启动时加载外部配置文件的方法
-
spring boot静态变量注入配置文件详解
-
Spring Boot 配置文件详解(小结)
-
IDEA开发spring boot应用时 application.yml 或 application.properties 自定义属性提示
-
Spring Boot: 配置文件详解
-
荐 Spring-boot-study02-spring.xml配置文件注入组件和@Bean注解注入组件差别
-
Python读取properties配置文件操作示例
-
Vue项目部署在Spring Boot出现页面空白问题的解决方案