我习惯于Spring框架依赖注入和东西,所以我对JBoss非常绿色。 我可能想要做一些不可能的事情。
在我们公司,我们从一个拥有所有依赖注入的原型开始。 我们有一个这样声明的类:
@ApplicationScoped public class MessageHandlerImpl implements MessageHandler { ... }我把它注入到另一个类如下:
@ApplicationScoped public class MessageReceiverImpl implements MessageReceiver { @Inject MessageHandler messageHandler; ... }原型被接受了,现在我们正在组织这个项目,在核心项目中隔离一些通用代码。
这些注射工作正常,直到我们分开它。 关于这个问题的更多细节在这个问题上被问到: 只有一个@ApplicationScoped类的模糊依赖
我退后一步,考虑到我正在做的事情是不可能用wildfly-swarm或者我正在尝试的整个概念是错误的。
我想要做的是在我的核心项目上完成所有CDI,并且只在我的依赖项目中使用@Inject注释。 所以在我的依赖项目中,我会有这样的东西:
@ApplicationScoped public class WSClient { @Inject MessageReceiver messageReceiver; ... }那么,这是行不通的,因为我得到一个模糊的依赖关系,你可以看到我链接的问题。 我想知道我应该做什么是使用一个生产者或类似的东西。 任何人都可以启发我有效的方法,或者可能是另一种选择?
I'm used to Spring framework dependency injection and stuff, so I'm really green with JBoss. I might be wanting to do something that is not possible.
In our company we started with a prototype that has all the dependency injection on it. We have one class declared this way:
@ApplicationScoped public class MessageHandlerImpl implements MessageHandler { ... }and I inject it on another class like this:
@ApplicationScoped public class MessageReceiverImpl implements MessageReceiver { @Inject MessageHandler messageHandler; ... }The prototype was accepted and now we are organizing this project isolating some common code on a core project.
Those injections were working fine until we separated it. More details about this problem was asked on this question: Ambiguous dependency with only one @ApplicationScoped class
I'm taking a step back and considering that maybe what I'm doing is not possible with wildfly-swarm or the whole concept that I'm trying is wrong.
What I would like to do is to have all the CDI done on my core project and only use it with an @Inject annotation on my dependent project. So in my dependent project I would have something like:
@ApplicationScoped public class WSClient { @Inject MessageReceiver messageReceiver; ... }Well, that doesn't work because I get an Ambiguous dependency as you can see on the question I linked. I'm wondering if what I should be doing is use a Producer or something like that. Can anyone enlighten me on a valid approach for this, or maybe an alternative?
最满意答案
当你想以某种可能变化的方式实例化一个类型然后把它交给CDI说“嘿,如果有人注入这种类型,给他这个实例”(当然是非常简化的版本)时,就会使用生产者。 从你所描述的情况来看,你应该对简单的注射和豆类创作足够好,我不认为需要生产者。
从你的两个问题中,我可以看到注入事实上甚至起作用,但也有一些含糊之处。 我的意思是,一个注射点有几个可能的候选者。 只有在你的情况下,它们是相同的,这意味着该类必须在某处以某种方式加载两次 - 继续向该方向挖掘。
从架构的角度来看,使用CDI将您的核心提取为CDI库完全可以(我会说很常见),然后由项目的其他部分使用。 只要确保你所有的档案都被认为是“bean档案”。 实现这一目标的最简单方法是确保在其中包含beans.xml 。
Producers are used when you want to instantiate a type in a certain, possibly varying, way and then just hand it over to CDI saying "Hey, if someone injects this type, give him this instance" (very simplified version of course). From what you described, you should be good enough with plain injection and bean creation, I don't see a need for producer.
From both of your questions I can gleam that the injection in fact even works, but there are some ambiguities. By that I mean that one injection point has several possible candidates. Only in your case they are identical which means the class must have been loaded twice somehow, somewhere - keep digging in that direction.
From the architecture point of view, with CDI it is perfectly ok (and I would say common) to extract your core as a CDI library which is then used by other parts of your project. Just make sure all of your archives are considered "bean archives". The easiest way to achieve that is to make sure you include beans.xml in them.
更多推荐
发布评论