@Value
读取配置参数
- 在 application.properties 文件设置属性
- 使用 @Value 读取配置文件
1 2 3 4 5 6 7 8
| @Component public class ReadProperties { @Value("${my.name}") private String name;
@Value("${my.age}") private Integer age; }
|
- 并且还可以设置一个默认值,从配置文件读取不到时使用默认值
1 2 3 4 5 6 7 8 9
| @Component public class ReadProperties { @Value("${my.name:默认姓名}") private String name;
@Value("${my.age:18}") private Integer age; }
|
给参数设定值
使用@Value 注解给参数设定值,达到跟 “=” 号一样的赋值效果
1 2 3 4 5 6
| @Component public class ReadProperties { @Value("#{'test value'}") private String value; }
|
读取系统属性
1 2 3 4 5 6
| @Component public class ReadProperties {
@Value("#{systemProperties['os.name']}") private String systemPropertiesName; }
|
读取 Bean 的属性
1 2 3 4 5 6
| @Data public class User{ private String name; private String age; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @Component public class ReadProperties {
@Bean public User user(){ User user = new User(); user.setName("测试"); user.setAge("18"); return user; }
@Value("#{user.name}") private String value; }
|
使用 SPEL 表达式
1 2 3 4 5 6 7
| @Component public class ReadProperties {
@Value("#{ T(java.lang.Math).random() * 100.0 }") private double random; }
|
读取 Resource 资源文件
1 2 3 4 5 6 7 8 9 10 11 12 13
| @Component public class ReadProperties {
@Value("classpath:application.properties") private Resource resourceFile;
public void test(){ if(resourceFile.exists()){ System.out.println(resourceFile.getFilename()); } } }
|
读取 List、Map
1 2
| test.list=topic1,topic2,topic3 test.maps="{key1: 'value1', key2: 'value2'}"
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| @Value("${test.list.ids:1,2,3}") private List<String> testList;
@Value("${test.list.ids:1,2,3}") private String[] testList;
@Value("#{'${test.list}'.split(',')}") private List<String> list;
@Value("#{${test.maps}}") private Map<String,String> maps;
|
静态变量赋值
1 2 3
| @Value("${ES.CLUSTER_NAME}") private static String CLUSTER_NAME;
|
1 2 3 4 5 6 7 8
| private static String CLUSTER_NAME;
@Value("${ES.CLUSTER_NAME}") public void setClusterName(String clusterName) { CLUSTER_NAME = clusterName; }
|
@ConfigurationProperties
读取 String 类型
- 在 application.properties 文件设置属性
- 使用 @ConfigurationProperties 注解读取对应配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @Configuration
@ConfigurationProperties(prefix = "my") public class ConfigurationReadConfig {
private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
|
读取 List 类型
- 在 application.properties 文件设置属性
1 2 3
| my.list[0]=a my.list[1]=b my.list[2]=c
|
- 使用 @ConfigurationProperties 注解读取对应配置
1 2 3 4 5 6 7 8 9 10 11 12 13
| @Configuration @ConfigurationProperties(prefix = "my") public class ConfigurationReadConfig {
private List<String> list;
public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } }
|
读取 Map 类型
- 在 application.properties 文件设置属性
1 2 3
| my.map.name=xiao-li my.map.sex=man my.map.age=20
|
- 使用 @ConfigurationProperties 注解读取对应配置
1 2 3 4 5 6 7 8 9 10 11 12 13
| @Configuration @ConfigurationProperties(prefix = "my") public class ConfigurationReadConfig {
private Map<String, String> map; public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } }
|
读取 Time 类型
- 在 application.properties 文件设置属性
- 使用 @ConfigurationProperties 注解读取对应配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| @Configuration @ConfigurationProperties(prefix = "my") public class ConfigurationReadConfig {
@DurationUnit(ChronoUnit.SECONDS) private Duration time; public Duration getTime() { return time; } public void setTime(Duration time) { this.time = time; } }
|
读取参数并进行 Valid 校验
- 在 application.properties 文件设置属性
1 2
| my.name=xiao-ming my.age=20
|
- 使用 @ConfigurationProperties 注解读取对应配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| @Validated @Configuration @ConfigurationProperties(prefix = "my") public class ConfigurationReadConfigAndValid {
@NotNull(message = "姓名不能为空") private String name; @Max(value = 20L,message = "年龄不能超过 20 岁") private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
|
读取配置到新建 Bean 中
- 在 application.properties 文件设置属性
1 2
| user.name=mydlq user.age=22
|
- 使用 @ConfigurationProperties 注解读取对应配置到新建的 Bean 对象中
1 2 3 4 5 6
| @Data public class User { private String name; private Integer age; }
|
1 2 3 4 5 6 7 8 9
| @Configuration public class ConfigurationReadObject {
@Bean("user") @ConfigurationProperties(prefix = "user") public User createUser(){ return new User(); } }
|
从指定配置文件读取
使用 @ConfigurationProperties 注解是默认从 application.properties 或者 application.yaml 中读取配置,有时候我们需要将特定的配置放到单独的配置文件中,这时候需要 @PropertySource 与 ConfigurationProperties 配置使用,使用 @PropertySource 注解指定要读取的文件,使用 @ConfigurationProperties 相关属性
- 在 test.txt 文件设置属性
- 使用 @ConfigurationProperties 注解读取对应配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @Configuration @ConfigurationProperties(prefix = "my") @PropertySource(encoding = "UTF-8", ignoreResourceNotFound = true, value = "classpath:test.txt") public class ConfigurationReadConfig {
private String name;
public String getName() { return name; } public void setName(String name) { this.name = name; } }
|
Environment
- 在 application.properties 文件设置属性
- 使用 Environment 读取配置
1 2 3 4 5 6 7 8 9 10 11 12
| @Component public class EnvironmentReadConfig {
private String name;
@Autowired private Environment environment;
public String readConfig(){ name = environment.getProperty("my.name", "默认值"); } }
|
1 2 3 4 5
| @Service
@EnableConfigurationProperties(MyConfigurationProperties.class) public class HelloServiceImpl implements HelloService { }
|
PropertiesLoaderUtils
- 在 application.properties 文件设置属性
- 使用 PropertiesLoaderUtils 读取配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public class PropertiesReadConfig {
private String name;
public void readConfig() { try { ClassPathResource resource = new ClassPathResource("application.properties"); Properties properties = PropertiesLoaderUtils.loadProperties(resource); name = properties.getProperty("my.name", "默认值"); } catch (IOException e) { log.error("", e); } } }
|
Properties
- setProperty(String key, String value):调用 Hashtable 的方法 put
- getProperty(String key):用指定的键在此属性列表中搜索属性
- getProperty(String key, String defaultValue):用指定的键在属性列表中搜索属性,没有则输出 defaultValue
1 2 3 4 5 6 7 8 9 10 11 12
| public static void main(String[] args) { Properties properties=new Properties(); properties.put("das","fsdf"); properties.put("ds","czx"); properties.put("czx","fds"); Set<Object> objects = properties.keySet(); for (Object key:objects){ Object o = properties.get(key); System.out.println(key+" "+o); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public static void main(String[] args) { Properties properties=new Properties(); properties.setProperty("fasf","cz"); properties.setProperty("czx","vc"); properties.setProperty("vxc","gfd"); System.out.println(properties.getProperty("vxc")); System.out.println(properties); Set<String> strings = properties.stringPropertyNames(); for (String s:strings){ String property = properties.getProperty(s); System.out.println(s+" "+property); } }
|
- load(InputStream inStream):从输入流中读取属性列表(键和元素对)
- load(Reader reader):按简单的面向行的格式从输入字符流中读取属性列表(键和元素对)
- loadFromXML(InputStream in):将指定输入流中由 XML 文档所表示的所有属性加载到此属性表中
- store(OutputStream out, String comments):以适合使用 load(InputStream) 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流
- store(Writer writer, String comments):以适合使用 load(Reader) 方法的格式,将此 Properties 表中的属性列表(键和元素对)写入输出字符
- storeToXML(OutputStream os, String comment):发出一个表示此表中包含的所有属性的 XML 文档
- storeToXML(OutputStream os, String comment, String encoding):使用指定的编码发出一个表示此表中包含的所有属性的 XML 文档
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| public static void main(String[] args) throws IOException { myStore(); mtLoad(); }
private static void mtLoad() throws IOException{ Properties properties=new Properties(); FileReader fileReader=new FileReader("1.txt"); properties.load(fileReader); fileReader.close(); System.out.println(properties); }
private static void myStore() throws IOException{ Properties properties=new Properties(); properties.setProperty("das","cz"); properties.setProperty("czx","vxc"); properties.setProperty("cz","vcx"); FileWriter fileWriter=new FileWriter("1.txt"); properties.store(fileWriter,"test1"); fileWriter.close(); }
|