2 回答
TA贡献1810条经验 获得超4个赞
编写自定义BeanFactoryPostProcessor
来玩转 bean 定义。
@Bean
public BeanFactoryPostProcessor getBeanFactoryPostProcessor() {
return beanFactory -> {
for (int i = 0; i < MyBeanType.values().length; i++) {
beanFactory.registerSingleton(MyBeanType.class.getSimpleName() + i,
new MyBean(MyBeanType.values()[i]));
}
};
}
TA贡献1820条经验 获得超9个赞
您可以简单地以编程方式注册 bean。应该这样做。
@Configuration
public class MyBeanConfig() implements ApplicationContextAware {
@Override
public void setApplicationContext(final ApplicationContext ctx) {
final ConfigurableListableBeanFactory beanFactory = ((ConfigurableApplicationContext) ctx).getBeanFactory();
for(final MyBeanType beanType: MyBeanType.values()) {
beanFactory.registerSingleton(MyBean.class.getCanonicalName() + "_" + beanType, new MyBean(beanType));
}
}
}
添加回答
举报