在Weld SE进行@Resource注射

时间:2017-01-12 17:15:10

标签: weld

我有一个带有@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 - 带注释的字段?

1 个答案:

答案 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)
}