的列表,我有一個關於屬性在春季與性質的工作,使我與春性工作對象
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()方法
我認爲這不是一個好主意,但我不知道其他解決方法。 任何解決方案或任何想法?
在此先感謝