我有一个带有@Resource
的bean - 带注释的字段:
@ApplicationScoped
open class UtilProducer {
...
@Resource(lookup = "java:jboss/datasources/mj2")
private lateinit var dataSource: DataSource
...
我想让Weld在这个dataSource
字段中注入一些内容。
我尝试添加自己的ResourceInjectionServices
实现 - MyResourceInjectionServices
,但似乎它甚至没有尝试实例化我的类
val weld = Weld()
.disableDiscovery()
.addPackages(true, UtilProducer::class.java)
.addPackages(true, CDIViewProvider::class.java)
.addBeanClass(MyResourceInjectionServices::class.java)
如何配置Weld SE以注入@Resource
- 带注释的字段?
答案 0 :(得分:0)
最后,我在文档中发现,在这种情况下,我需要子类化Weld
对象。并覆盖createDeployment
方法:
public class MyWeld extends Weld {
protected Deployment createDeployment(ResourceLoader resourceLoader, CDI11Bootstrap bootstrap) {
return super.createDeployment(new MyResourceLoader(), bootstrap);
}
}
就我在Kotlin的案子来说:
val weld = object : Weld() {
override fun createDeployment(resourceLoader: ResourceLoader?, bootstrap: CDI11Bootstrap?): Deployment {
val deployment = super.createDeployment(resourceLoader, bootstrap)
deployment.services.add(ResourceInjectionServices::class.java, MyResourceInjectionServices())
return deployment
}
}.apply {
disableDiscovery()
addPackages(true, UtilProducer::class.java)
addPackages(true, CDIViewProvider::class.java)
}