java - 是否可以使用 Spring 注入 bean 列表
问题描述
我想从一个连接创建一个 JMS MessageProducer 列表。这些生产者中的每一个都需要从单独的会话中创建。
我想在春天做这样的事情,但显然这不起作用。
@Bean(destroyMethod = "close")
@Scope("prototype")
public Session jmsSession(final Connection jmsConnection) {
<create a session>
}
@Bean(destroyMethod = "close")
public List<MessageProducer> jmsProvider(List<Session> jmsSessionList) {
<Create a list of MessageProducers from the list of jmsSessionList>
}
有什么办法可以做到这一点?
- 从一个连接创建特定数量的会话。(会话数是 yml 文件中的配置属性)
- 将 Session 列表注入另一个 bean 以创建 MessageProducer
- 指定destroyMethod 来销毁它们。
解决方案
你应该能够做你想做的事。
- 为此,您需要创建多个
Session
bean,如下所示:@Configuration class SessionsConfiguration { @Autowired private GenericApplicationContext applicationContext; @Value("${your.configuration.key}") private int sessionsCount; @PostConstruct fun init() { for (int i : sessionsCount) { applicationContext.registerBean() // Here you have multiple possibilities. // Check reference documentation at https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/support/GenericApplicationContext.html } } }
- 您的代码应该已经这样做了。
- 您的代码应该已经这样做了。
推荐阅读
- spring-boot - 当我们在 @InboundChannelAdapter 中使用自定义 pollerMetadata 时,PollerMetadata 为空
- docker - 如何删除 Docker 中的非最新图像?
- javascript - 如何将环境变量传递给 Web 扩展
- ios - 在 React Native 中构建暂存方案时出现“CFBundleIdentifier 不存在”
- c# - 将 .NET Core Razor 页面(也可能是 MVC)发布到 Mac OSX 时,静态文件不起作用
- razor - EnumDropDownListFor 本地化 resx 值排序
- python - 如何将图像的 HH 子带传递给 graycomatrix 函数?
- ajax - 带有 JSF 的 Primefaces 上的模态编辑对话框
- php - Codeigniter 3 路由和 404 页面
- java - 为什么这个 Kotlin 类属性不公开?