我想在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配置中执行:
...
<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
还有一些不错的实用工具可以将这样的文件转换为我List
类的Student
。
Sofar我选择了单独的.xml配置文件作为学生列表,该文件包含在我的spring配置中,但我并不特别喜欢向客户端提供部分xml配置的想法。我相信这应该是分开的。
所以问题是:有没有可以为我做这个的很酷的弹簧实用程序?还是由我来写一个?
答案 0 :(得分:4)
所以我决定证明我不是那么懒。该解决方案有一些明显的局限性,例如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 + '\'' +
'}';
}
}
Spring定义:
<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'}
]
工厂bean本身:
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);
}
}
答案 1 :(得分:0)
发布此帖子,以帮助仍在尝试寻找答案的其他人。使用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;
}