Java 在从@Configuration创建bean定义之后
问题是: 我有一个bean(Java 在从@Configuration创建bean定义之后,java,spring,spring-annotations,Java,Spring,Spring Annotations,问题是: 我有一个bean(ConversionService),它需要创建一个Converter集合。因此,在我的@配置类中,我有一个@Bean,它是一个集合,带有一个特定的@限定符 对于我的转换服务@Bean,我使用我的@限定符接收转换器集合作为参数,如下所示: @Bean public ConversionService createConversionService(@Qualifier("converters") converters) { // here I perform
ConversionService
),它需要创建一个Converter
集合。因此,在我的@配置
类中,我有一个@Bean
,它是一个集合
,带有一个特定的@限定符
对于我的转换服务
@Bean
,我使用我的@限定符
接收转换器
集合作为参数,如下所示:
@Bean
public ConversionService createConversionService(@Qualifier("converters") converters) {
// here I perform the ConversionService creation
}
@Configuration
public class MyConfiguration {
@Autowired
@Qualifier("converters")
private Collection<Converter> converters;
public void init() {
converters.add(xy);
}
}
这就是我想要的效果。但是我有几个@Configuration
类,每个类都应该能够向转换器
集合添加一些内容。我最初认为可能有一种方法可以实现从@Configuration
类读取bean定义后调用的方法。大概是这样的:
@Bean
public ConversionService createConversionService(@Qualifier("converters") converters) {
// here I perform the ConversionService creation
}
@Configuration
public class MyConfiguration {
@Autowired
@Qualifier("converters")
private Collection<Converter> converters;
public void init() {
converters.add(xy);
}
}
@配置
公共类MyConfiguration{
@自动连线
@限定符(“转换器”)
私人收藏转换器;
公共void init(){
添加(xy);
}
}
甚至
@Configuration
public class MyConfiguration {
public void init(@Qualifier("converters") Collection<Converter> converters) {
converters.add(xy);
}
}
@配置
公共类MyConfiguration{
公共void init(@限定符(“转换器”)集合转换器){
添加(xy);
}
}
您应该能够使用注释向@配置中的转换器
添加内容
@Configuration
public class MyConfiguration {
@Autowired
@Qualifier("converters")
private Collection<Converter> converters;
@PostConstruct
public void init() {
converters.add(xy);
}
}
@配置
公共类MyConfiguration{
@自动连线
@限定符(“转换器”)
私人收藏转换器;
@施工后
公共void init(){
添加(xy);
}
}
有没有一种方法可以只用spring类来实现这一点@PostConstruct是一个javax annotation.perfect,我实现了org.springframework.beans.factory.InitializingBean,谢谢你的帮助@JavaMentor:你能发布你的答案吗?@NandkumarTekale:我通过实现类似于:@Configuration公共类CoreConfiguration implements InitializingBean的东西解决了这个问题{
,该方法非常类似于我的第一个@Configuration
类示例,但我将在PropertieSet()抛出异常{
后实现public void,而不是方法init
。这个答案使用javax.annotation
解决了问题,事实上也会得到相同的结果。