我在 Spring 中使用属性,我对属性有疑问
Valores.properties
estudiante.nombre=Antonio, Juan , Maria, Raquel
estudiante.edad=28,20,21,23
现在我有一个类来开发bean
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();
}
}
程序运行,输出正常
[
Estudiante{nombre=Antonio, edad=28},
Estudiante{nombre= Juan , edad=20},
Estudiante{nombre= Maria, edad=21},
Estudiante{nombre= Raquel, edad=23}]
但是不知道这样的开发方式对不对。我不喜欢在 AppConfig 类中构建方法 getListaStudents() 来制作对象列表,我也不喜欢在 Spring 中使用 new() 方法
我认为这不是一个好主意,但我不知道其他解决方法。 任何解决方案或任何想法?
提前致谢
最佳答案
我认为这是一种适当的方法,几乎没有改进,但根据数据大小和要求,您可能不想从属性文件加载数据。属性文件通常用于配置选项,例如不同环境的数据库配置、缓存配置等。
在你的 AppConfig 中,你不需要像这样使用 SpringEL 来解析 Integer
或 String
的 List
,
@Value("#{'${estudiante.nombre}'.split(',')}")
private List<String> nombres;
我建议改用这样的东西,
@Value("${estudiante.edad}") List<Integer> nombres
但要使其工作,您需要为 Spring 的 ConversionService 添加一个 bean 配置。
@Bean
public ConversionService conversionService() {
return new DefaultConversionService();
}
这样 Spring 的转换服务将有默认转换器来转换字符串或整数列表,这样您就可以避免可读性稍差的 #{'${estudiante.edad}'.split(',')}
在 @Value
注释中。
现在您可以使用上面的 @Value
直接在新 bean 中使用来创建一组像这样的学生。
@Bean
public List<Estudiante> getStudents(
@Value("${estudiante.edad}") List<Integer> numbers,
@Value("${estudiante.nombre}") List<String> names) {
List<Estudiante> listaStudents = new ArrayList<Estudiante>();
for (int i= 0;i< numbers.size();i++){
listaStudents.add(new Estudiante(names.get(i), numbers.get(i)));
}
return listaStudents;
}
并且您可以使用 @Resource
注入(inject) Estudiante
列表,
@Resource(name = "getStudents")
private List<Estudiante> estudiantes;
我没有发现使用 new
关键字创建 Estudiante
对象有什么问题。正如我们在使用 @Bean
注释的其他方法中使用 new
一样,这是一个完全有效的场景。如果你愿意,无论出于何种原因在创建 Estudiante
时避免使用 new
,你可以注入(inject) ApplicationContext并使用 Estudiante studiante = applicationContext.getBean(Estudiante.class);
获取 Estudiante
并且不要忘记使用 @ 标记
Estudiante
类范围(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
I dón't like to build a method getListaStudents() in the AppConfig class to make a list of objects
据我所知,没有简单和适当的方法来创建 Estudiante
对象的 List
,因为他们需要有自己的对象值。您可以将学生创建到它自己的配置类,例如 EstudianteConfiguration
并将其导入您的主 AppConfig
类 @Import注释。
关于java - 在 Spring 中使用属性来制作对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36379577/