的列表,我有一个关于属性在春季与性质的工作,使我与春性工作对象
Valores.properties疑问
estudiante.nombre=Antonio, Juan , Maria, Raquel
estudiante.edad=28,20,21,23
现在我有一个类制定豆
public class Estudiante {
public Estudiante() {
}
public Estudiante(String nombre, Integer edad) {
super();
this.nombre = nombre;
this.edad = edad;
}
@Value("${estudiante.nombre}")
private String nombre;
@Value("${estudiante.edad}")
private Integer edad;
public Integer getEdad() {
return edad;
}
public void setEdad(Integer edad) {
this.edad = edad;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
@Override
public String toString() {
return '\n' +"Estudiante{" + "nombre=" + nombre + ", edad=" + edad + '}';
}
}
一个Java类,以使配置
@Configuration
@PropertySource(value="classpath:valores.properties")
public class AppConfig {
@Value("#{'${estudiante.nombre}'.split(',')}")
private List<String> nombres;
@Value("#{'${estudiante.edad}'.split(',')}")
private List<Integer> edades;
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
public List<String> getNombres() {
return nombres;
}
public List<Integer> getEdades() {
return edades;
}
public List<Estudiante> getListaStudents() {
List<Estudiante> listaStudents = new ArrayList<>();
for (int i= 0;i< nombres.size();i++){
listaStudents.add(new Estudiante(nombres.get(i),edades.get(i)));
}
return listaStudents;
}
}
和主Java类
public class Principal {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
AppConfig appConfig = context.getBean(AppConfig.class);
System.out.println(appConfig.getListaStudents());
((ConfigurableApplicationContext)context).close();
}
}
该项目工程和输出是OK
[
Estudiante{nombre=Antonio, edad=28},
Estudiante{nombre= Juan , edad=20},
Estudiante{nombre= Maria, edad=21},
Estudiante{nombre= Raquel, edad=23}]
但我不知道这是否是发展的正道。我不想在AppConfig类中构建一个getListaStudents()方法来创建objets列表,我不喜欢在Spring中使用new()方法
我认为这不是一个好主意,但我不知道其他解决方法。 任何解决方案或任何想法?
在此先感谢