在sprinboot中使用@PropertySource注解读取properties文件存放到map中导致顺序错乱的问题
文章目录
- 在sprinboot中使用@PropertySource注解读取properties文件存放到map中导致顺序错乱的问题
- 1.需要使用到的jar包文件
- 2.由于读取到文件后顺序错乱,可能发生的情况进行假设
- 3.按照上面的三种假设逐一排查问题后发现Properties类是继承的Hashtable,导致的读取时发生顺序错乱
- 4.查找解决方案
- 5.解决问题
- 1)由于ResourcePropertySource不能直接更改PropertiesLoaderUtils的调用,那么需要我们按照源码顺序进行修改,即:修改PropertySourceFactory的实现,PropertiesPropertySource的实现及PropertiesLoaderUtils的方法
- 2)修改PropertySourceFactory的实现方法命名LinkedHashMapPropertySourceFactory
- 3)修改ResourcePropertySource的方法重新实现方法PropertiesPropertySource
- 4)修改PropertiesLoaderUtils的类的方法命名LinkedHashMapPropertiesLoaderUtils:
- 5)修改properties的方法
- 5.测试
1.需要使用到的jar包文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
2.由于读取到文件后顺序错乱,可能发生的情况进行假设
1)由于接收的map不是顺序存储。
2)由于springboot在读取过程中使用了无序map进行二次处理
3)java读取的Properties的类是无序的
3.按照上面的三种假设逐一排查问题后发现Properties类是继承的Hashtable,导致的读取时发生顺序错乱
4.查找解决方案
找到问题后,则需想办法进行修改。那么就需要将springboot读取properties的方法替换成自己的方法,所以需要找到可以修改并且可以让@PropertySource注解知道使用自定义的方法的地方。
@PropertySource注解部分源码如下:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(PropertySources.class)
public @interface PropertySource {
String name() default "";
String[] value();
boolean ignoreResourceNotFound() default false;
String encoding() default "";
Class<? extends PropertySourceFactory> factory() default PropertySourceFactory.class;
}
在@PropertySource注解中有factory的参数,该值类型为PropertySourceFactory的类型的class值。那么看一下这个类的具体实现类DefaultPropertySourceFactory,部分源码如下:
public class DefaultPropertySourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException {
return (name != null ? new ResourcePropertySource(name, resource) : new ResourcePropertySource(resource));
}
}
该类中有createPropertySource的方法调用的是ResourcePropertySource的类部分代码如下:
public ResourcePropertySource(String name, EncodedResource resource) throws IOException {
super(name, PropertiesLoaderUtils.loadProperties(resource));
this.resourceName = getNameForResource(resource.getResource());
}
public ResourcePropertySource(EncodedResource resource) throws IOException {
super(getNameForResource(resource.getResource()), PropertiesLoaderUtils.loadProperties(resource));
this.resourceName = null;
}
在这个类中,我们可以看到在创建ResourcePropertySource对象时调用了PropertiesLoaderUtils的类的loadProperties的方法,源码如下:
public static Properties loadProperties(EncodedResource resource) throws IOException {
Properties props = new Properties();
fillProperties(props, resource);
return props;
}
源码看到这里发现Properties的方法调用,那么我们修改PropertiesLoaderUtils的loadProperties方法即可
5.解决问题
找到解决问题的方案后。则需验证方案是否正确。
1)由于ResourcePropertySource不能直接更改PropertiesLoaderUtils的调用,那么需要我们按照源码顺序进行修改,即:修改PropertySourceFactory的实现,PropertiesPropertySource的实现及PropertiesLoaderUtils的方法
2)修改PropertySourceFactory的实现方法命名LinkedHashMapPropertySourceFactory
源码如下:
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
import java.io.IOException;
public class LinkedHashMapPropertySourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
return (name != null ? new LinkedHashMapResourcePropertySource(name, resource) : new LinkedHashMapResourcePropertySource(resource));
}
}
3)修改ResourcePropertySource的方法重新实现方法PropertiesPropertySource
源码如下:
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
import java.io.IOException;
public class LinkedHashMapResourcePropertySource extends PropertiesPropertySource {
@Nullable
private final String resourceName;
public LinkedHashMapResourcePropertySource(String name, EncodedResource resource) throws IOException {
super(name, LinkedHashMapPropertiesLoaderUtils.loadProperties(resource));
this.resourceName = getNameForResource(resource.getResource());
}
public LinkedHashMapResourcePropertySource(EncodedResource resource) throws IOException {
super(getNameForResource(resource.getResource()), LinkedHashMapPropertiesLoaderUtils.loadProperties(resource));
this.resourceName = null;
}
private static String getNameForResource(Resource resource) {
String name = resource.getDescription();
if (!StringUtils.hasText(name)) {
name = resource.getClass().getSimpleName() + "@" + System.identityHashCode(resource);
}
return name;
}
}
4)修改PropertiesLoaderUtils的类的方法命名LinkedHashMapPropertiesLoaderUtils:
源码如下:
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import java.io.IOException;
import java.util.Properties;
public class LinkedHashMapPropertiesLoaderUtils extends PropertiesLoaderUtils {
public static Properties loadProperties(EncodedResource resource) throws IOException {
Properties props = new LinkedHashMapProperties();
fillProperties(props, resource);
return props;
}
}
5)修改properties的方法
源码如下:
import java.util.*;
public class LinkedHashMapProperties extends Properties {
private static final long serialVersionUID = -4627607243846121965L;
private final LinkedHashSet<Object> keys = new LinkedHashSet<>();
public Enumeration<Object> keys() {
return Collections.enumeration(keys);
}
public Object put(Object key, Object value) {
keys.add(key);
return super.put(key, value);
}
public Set<Object> keySet() {
return keys;
}
public Set<String> stringPropertyNames() {
Set<String> set = new LinkedHashSet<>();
for (Object key : this.keys) {
set.add((String) key);
}
return set;
}
}
5.测试
到这里问题就基本解决了,那么需要写个测试类测试一下效果
test.stu.name = 张三
test.stu.age = 20
test.stu.sex=男
test.stu.grade=大二
@Component
@Data
@ConfigurationProperties(prefix ="test")
@PropertySource(value = {"test.properties"},factory = LinkedHashMapPropertySourceFactory.class)
public class TestProperties {
private Map<String,String> stu = new LinkedHashMap<>();
}
通过测试后发现stu里边的顺序为properties定义的顺序。那么功能到这里就完成了