【发布时间】:2014-12-09 09:05:19
【问题描述】:
Example:
@Dependant
public class SomeStartingPoint {
@Inject
private SomeService someService;
public void doSomething(Long aLong, MyCustomObject myObject) {
NotABean notABean = new NotABean(aLong, myObject);
someService.doStuffWithNotABean(notABean);
}
}
public class NotABean {
@Inject
private WouldBePerfectIfThisWouldBeManagedBean somehowInjectedBean;
public NotABean(Long aLong, MyCustomObject myObject) {
//set state
}
}
所以问题是,有没有一种很好的方法可以将一些东西注入到 NotABean 对象中,它应该有状态,因此由 new() 创建?
当然,在当前情况下,我可以将 WillBePerfectIfThisWouldBeManagedBean 作为参数传递给构造函数,但这与问题无关。
【问题讨论】:
-
也许您真的希望 CDI bean 成为可以结合注入参数和调用时参数的工厂。
-
您使用的是 CDI 1.1 还是 1.0?
-
1.0. 1.1有这个功能吗?
-
在 1.1 中做起来要容易一些。我会将两者都包含在答案中。
标签: java dependency-injection cdi