@焊接SE中的资源注入

我有一个@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 -annotated字段?

最后我发现在这种情况下,我需要继承一个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) } 
Interesting Posts