我正在使用Spring XML配置,我想将其移至Annotation Config。
问题是如何从不同的属性文件创建不同的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
我想将其移至Annotation配置,例如:
@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();
}
}
这是Auditorium POJO:
@Component
public class Auditorium {
private Integer id;
private String name;
private Integer numberOfSeats;
private Set<Integer> vipSeats;
// get / set
如何为每个属性文件创建不同的Auditorium实例?
我想我可以使用@Value
,但我找不到每个属性文件的方法创建对象。
有什么建议吗?
答案 0 :(得分:2)
首先,您不需要列表,如果您需要类型列表,只需将@Autowired
添加到集合中,spring会将所有类型的bean注入该集合。
要获取属性,您可以使用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将把所有依赖项注入该列表。
public class MyClass {
@Autowired
private List<Auditorium> auditoriums;
}
答案 1 :(得分:-1)
@Configuration
public class DataSourceConfig {
@Bean
public DataSource dataSource() {
return new DriverManagerDataSource(...);
}
}
@Configuration
@Import({ DataSourceConfig.class, TransactionConfig.class })
public class AppConfig extends ConfigurationSupport {
// bean definitions here can reference bean definitions in DataSourceConfig or TransactionConfig
}
@Configuration
@PropertySource("classpath:/com/myco/app.properties")
//@ResourceBundles("classpath:/org/springframework/config/java/simple")
public class AppConfig {
@Value("${jdbc.url}")
//@ExternalValue("datasource.username")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public TestBean testBean () {
HashMap<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);
return new TestBean(map);
}
@Bean
public Map<Integer, String> nameMap(){
Map<Integer, String> nameMap = new HashMap<>();
nameMap.put(1, "John");
nameMap.put(2, "Adam");
nameMap.put(3, "Harry");
return nameMap;
}
private Map<Integer, String> nameMap;
@Autowired
public void setNameMap(Map<Integer, String> nameMap) {
this.nameMap = nameMap;
}
public void printNameMap() {
System.out.println(nameMap);
}
@Bean
public List<String> nameList() {
return Arrays.asList("John", "Adam", "Harry");
}
//set
private Set<String> nameSet;
public AppConfig(Set<String> strings) {
this.nameSet = strings;
}
@Bean
public AppConfig getCollectionsBean() {
return new AppConfig(new HashSet<>(Arrays.asList("John", "Adam", "Harry")));
}
public void printNameSet() {
System.out.println(nameSet);
}
private Set<String> nameSet;
public AppConfig(Set<String> strings) {
this.nameSet = strings;
}
public void printNameSet() {
System.out.println(nameSet);
}
}
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
TestBean bean = context.getBean(TestBean.class);
System.out.println(bean.getMap());
}