我想在spring中基于属性文件创建bean列表。为了说明这个问题,假设我有一个ClassRoom
。
public class ClassRoom {
private List<Student> students;
public void setStudents(List<Student> students) {
this.students = students;
}
}
public class Student {
private Strign firstName;
private String lastName;
/* cosntructor, setters, getters ... */
}
因此,通常我会在.xml spring-config:中执行
<property name="student">
<list>
<bean id="student1" class="Student" ...>
<property name="firstName" value="${student.name}" />
<property name="lastName" value="${student.surname}" />
</bean>
...
</list>
<property>
但现在我有几个属性文件——每个环境一个,相应的文件是根据定义环境的系统属性包含的——每个环境中的学生人数不同。
所以我要找的是有一个属性文件,比如:
student.1.fistName=Paul
student.1.lastName=Verlaine
student.2.firstName=Alex
student.2.lastName=Hamburger
还有一些很好的实用程序,它将这样的文件转换为我的Student
类的List
。
Sofar我为学生列表创建了一个单独的.xml配置文件,该文件包含在我的spring配置中,但我并不特别喜欢向客户端提供部分xml配置的想法。我认为这应该分开。
那么问题来了:有没有什么冷泉实用程序可以为我做到这一点?还是由我来写?
所以我决定证明我没有那么懒。该解决方案有一些明显的局限性,如1级属性或只能设置基元类型。但满足了我的需求。记录在案:
属性文件:
student.1.firstName=Jan
student.1.lastName=Zyka
student.1.age=30
student.2.firstName=David
student.2.lastName=Kalita
student.2.age=55
学生班级:
package com.jan.zyka.test.dao;
public class Student {
private String firstName;
private String lastName;
private int age;
private String common;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getCommon() {
return common;
}
public void setCommon(String common) {
this.common = common;
}
@Override
public String toString() {
return "Student{" +
"firstName='" + firstName + ''' +
", lastName='" + lastName + ''' +
", age=" + age +
", common='" + common + ''' +
'}';
}
}
弹簧定义:
<bean id="studentFactory" class="com.jan.zyka.test.BeanListFactory">
<property name="propertyPrefix" value="student" />
<property name="commonProperties">
<map>
<entry key="common" value="testCommonValue" />
</map>
</property>
<property name="targetType">
<value type="java.lang.Class">com.jan.zyka.test.dao.Student</value>
</property>
<property name="properties">
<util:properties location="classpath:testListFactory.properties" />
</property>
</bean>
结果:
[
Student{firstName='Jan', lastName='Zyka', age=30, common='testCommonValue'},
Student{firstName='David', lastName='Kalita', age=55, common='testCommonValue'}
]
工厂豆本身:
package com.jan.zyka.test;
import com.google.common.primitives.Primitives;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.FactoryBean;
import java.beans.PropertyDescriptor;
import java.util.*;
/**
* <p>
* Creates list of beans based on property file.
* </p>
* <p>
* Each object should be defined in the property file as follows:
* <pre>
* <prefix>.<index>.<property>
* </pre>
*
* So one example might be:
* <pre>
* student.1.firstName=Paul
* student.1.lastName=Verlaine
* student.2.firstName=Alex
* student.2.lastName=Hamburger
* </pre>
*
* The target class must provide default constructor and setter for each property defined in the configuration file as per
* bean specification.
* </p>
*
* @param <T> type of the target object
*/
public class BeanListFactory<T> implements FactoryBean<List<T>> {
private String propertyPrefix;
private Map<String, Object> commonProperties = Collections.emptyMap();
private Class<T> targetType;
private Properties properties;
private List<T> loadedBeans;
public String getPropertyPrefix() {
return propertyPrefix;
}
public void setPropertyPrefix(String propertyPrefix) {
this.propertyPrefix = propertyPrefix;
}
public Map<String, Object> getCommonProperties() {
return commonProperties;
}
public void setCommonProperties(Map<String, Object> commonProperties) {
this.commonProperties = commonProperties;
}
public Class<T> getTargetType() {
return targetType;
}
public void setTargetType(Class<T> targetType) {
this.targetType = targetType;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public List<T> getObject() throws Exception {
loadedBeans = new ArrayList<T>();
int lastIndex = -1;
T item = null;
for (String property: prefixFilteredProperties()) {
// The actual value
final String propertyValue = properties.getProperty(property);
// Remove Prefix
property = property.substring(propertyPrefix.length() + 1);
// Split by "."
String tokens[] = property.split("\.");
if (tokens.length != 2) {
throw new IllegalArgumentException("Each list property must be in form of: <prefix>.<index>.<property name>");
}
final int index = Integer.valueOf(tokens[0]);
final String propertyName = tokens[1];
// New index
if (lastIndex != index) {
if (lastIndex !=-1) {
loadedBeans.add(item);
}
lastIndex = index;
item = targetType.newInstance();
setCommonProperties(item, commonProperties);
}
// Set the property
setProperty(item, propertyName, convertIfNecessary(propertyName, propertyValue));
}
// Add last item
if (lastIndex != -1) {
loadedBeans.add(item);
}
return loadedBeans;
}
@Override
public Class<?> getObjectType() {
return ArrayList.class;
}
@Override
public boolean isSingleton() {
return false;
}
private Object convertIfNecessary(String propertyName, String propertyValue) throws Exception {
PropertyDescriptor descriptor = BeanUtils.getPropertyDescriptor(targetType, propertyName);
Class<?> propertyType = Primitives.wrap(descriptor.getPropertyType());
if (propertyType == String.class) {
return propertyValue;
}
return propertyType.getDeclaredMethod("valueOf", String.class).invoke(propertyType, propertyValue);
}
private Set<String> prefixFilteredProperties() {
Set<String> filteredProperties = new TreeSet<String>();
for (String propertyName: properties.stringPropertyNames()) {
if (propertyName.startsWith(this.propertyPrefix)) {
filteredProperties.add(propertyName);
}
}
return filteredProperties;
}
private void setCommonProperties(T item, Map<String, Object> commonProperties) throws Exception {
for (Map.Entry<String, Object> commonProperty: commonProperties.entrySet()) {
setProperty(item, commonProperty.getKey(), commonProperty.getValue());
}
}
private static void setProperty(Object item, String propertyName, Object value) throws Exception {
PropertyDescriptor descriptor = BeanUtils.getPropertyDescriptor(item.getClass(), propertyName);
descriptor.getWriteMethod().invoke(item, value);
}
}
发布这个Spring Boot特定的解决方案,希望能帮助其他仍在努力寻找答案的人。使用spring可以开箱即用地将属性文件映射到bean列表。您需要有一个ConfigurationProperties bean,里面有您的bean集合,并使用正确的命名约定,如下所示。
看看我的工作示例:
我有一个带有ConfigurationProperties的包装bean:
@Component
@ConfigurationProperties(prefix = "notification")
@Data
@Valid
public class NotificationProperties {
@NotNull
private Set<AlertThreshold> alertThresholds;
@NotNull
private Set<Subscriber> subscribers;
}
application.properties中的以下属性(yaml还有另一种方法)将映射到NotificationProperties文件中,创建bean集:
notification.alert-thresholds[0].step=PB
notification.alert-thresholds[0].status=RDY
notification.alert-thresholds[0].threshold=500
notification.alert-thresholds[1].step=LA
notification.alert-thresholds[1].status=RDY
notification.alert-thresholds[1].threshold=100
notification.subscribers[0].email=subscriber1@gmail.com
notification.subscribers[1].email=subscriber2@gmail.com
以下是参与此配置的其他模型类:
@Data
public class AlertThreshold {
@NotNull
private String step;
@NotNull
private String status;
@NotNull
private Integer threshold;
}
@Data
public class Subscriber {
@Email
private String email;
}