jakarta-ee - CDI 中的参数化 bean
问题描述
我正在尝试使用参数创建一个 CDI bean。我的问题是我希望注入类传递这些参数。我怎样才能做到这一点?
例如
class MyBean() {
}
@Dependent
class MyInjectingClass() {
@Inject MyBean myBean; // and somehow add two int parameters here that MyBean will use in its methods
}
解决方案
如果int
s 只与 的产生完全相关MyBean
,那么您可以编写一个生产者方法MyBean
来创建它喜欢的实例。例如:
@ApplicationScoped // the "host" of a producer method must itself be a bean
class WhereverYouWantTheProducerMethodToLive {
@Produces
@Dependent // make MyBean instances in @Dependent scope
private MyBean makeMyBean() {
return new MyBean(1, 2);
}
}
推荐阅读
- javascript - WebView2 - 调用/执行 Javascript 函数
- powerbi - 在 DAX 中应用具有日期条件的帕累托原理
- android - 如何在 android studio/kotlin/observer/ 中只观察一侧
- php - 分页链接不反映帖子计数
- git - git commit 给出“git-together.active”错误
- powershell - Powershell:将文件夹结构从本地驱动器复制到 blob 存储
- javascript - Jest + Styled Components - 嵌套组件上的 .toHaveStyleRule
- firebase - SwiftUI + TabView + ScrollView + Kingfisher
- python - Python中的虚拟变量回归
- python - 从 Continuous_threading.PeriodicThread 获取特定字符串,然后执行操作