gpt4 book ai didi

java - 如何从不同的属性文件中为 Bean 列表配置 Spring 配置?

转载 作者:行者123 更新时间:2023-11-30 03:03:04 24 4
gpt4 key购买 nike

我有可用的 Spring XML 配置,我想将其移至注释配置。

问题是如何从不同的属性文件创建不同的bean。

这是 XML 片段:

<bean id="auditorium1" class="net.lelyak.edu.entity.Auditorium"
c:name="${auditorium1.name}" c:numberOfSeats="${auditorium1.number-of-seats}"
c:vipSeats="${auditorium1.vip-seats}"/>
<bean id="auditorium2" class="net.lelyak.edu.entity.Auditorium"
c:name="${auditorium2.name}" c:numberOfSeats="${auditorium2.number-of-seats}"
c:vipSeats="${auditorium2.vip-seats}"/>
<bean id="auditorium3" class="net.lelyak.edu.entity.Auditorium"
c:name="${auditorium3.name}" c:numberOfSeats="${auditorium3.number-of-seats}"
c:vipSeats="${auditorium3.vip-seats}"/>

<util:list id="auditoriumsList">
<ref bean="auditorium1"/>
<ref bean="auditorium2"/>
<ref bean="auditorium3"/>
</util:list>

<bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean" />

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:properties/auditorium1.properties</value>
<value>classpath:properties/auditorium2.properties</value>
<value>classpath:properties/auditorium3.properties</value>
<value>classpath:db/dp.properties</value>
</list>
</property>
<property name="ignoreResourceNotFound" value="true"/>
<property name="systemPropertiesMode">
<util:constant
static-field="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
</property>
</bean>

属性文件示例:

auditorium1.name=yellow
auditorium1.number-of-seats=150
auditorium1.vip-seats=1,2,3,4,5,6,7,8,9

我想将其移至注释配置,例如:

@Configuration
public class AuditoriumConfiguration {

@Bean
public Auditorium auditorium1() {
return new Auditorium();
}

@Bean
public Auditorium auditorium2() {
return new Auditorium();
}

@Bean
public Auditorium auditorium3() {
return new Auditorium();
}

@Bean
public List<Auditorium> auditoriumsList() {
return Arrays.asList(auditorium1(), auditorium2(), auditorium3());
}

@Bean
public ConversionService conversionService() {
return new DefaultConversionService();
}
}

这里是 POJO 礼堂:

@Component
public class Auditorium {
private Integer id;
private String name;
private Integer numberOfSeats;
private Set<Integer> vipSeats;
// get / set

如何为每个属性文件创建不同的 Auditorium 实例?

我想我可以使用@Value,但我找不到为每个属性文件创建对象的方法。

有什么建议吗?

最佳答案

首先,您不需要列表,如果您需要类型列表,只需添加 @Autowired到一个集合,spring 会将该 bean 的所有类型注入(inject)到该集合中。

要获取属性,您可以使用 Environment您可能想为此添加一个帮助器/工厂方法。

转换服务已默认配置,因此无需添加。

要加载配置文件,请添加 @PropertySource到您的配置类。如果您需要@Value或替换占位符,您需要添加 PropertySourcesPlaceholderConfigurer作为static bean 。

基本上,这样的事情应该可以实现你想要的。

@Configuration
@PropertySource({"classpath:properties/auditorium1.properties",
"classpath:properties/auditorium2.properties",
"classpath:properties/auditorium3.properties",
"classpath:db/dp.properties"})
public class AuditoriumConfiguration {

@Autowired
private Environment env;

@Bean
public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}

private Auditorium createAuditorium(String prefix) {
String nameString = env.getProperty(prefix + ".name");
String strNumberOfSeats = env.getProperty(prefix + ".number-of-seats");
Integer[] integers = env.getProperty(prefix + ".vip-seats", Integer[].class);
Set<Integer> integerSet = Arrays.stream(integers).collect(Collectors.toSet());

Auditorium auditorium = new Auditorium();
auditorium.setName(nameString);
auditorium.setNumberOfSeats(Integer.valueOf(strNumberOfSeats));
auditorium.setVipSeats(integerSet);
return auditorium;
}

@Bean
public Auditorium auditorium1() {
return createAuditorium("auditorium1");
}

@Bean
public Auditorium auditorium2() {
return createAuditorium("auditorium2");
}

@Bean
public Auditorium auditorium3() {
return createAuditorium("auditorium3");
}
}

现在,如果您想要这些类的列表,只需添加 List<Auditorium> spring 会将所有依赖项注入(inject)到该列表中。

public class MyClass {
@Autowired
private List<Auditorium> auditoriums;
}

关于java - 如何从不同的属性文件中为 Bean 列表配置 Spring 配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35443942/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com