我是DI新手,但突然間需要在我的EJB應用程序中使用它,所以我嘗試重新創建它。依賴注入和容器類(java-ee 7)
該方法包括具有2個字段 - 2個實現的容器類。它根據參數與一個或兩個實現協同工作。該容器是在單例的方法調用中創建的,但被其他ejb bean使用。
在這裏我需要幫助 - 如何使SecurityContainer類與其他CDI託管類(ejb beans)正常工作或成爲CDI管理自己?
我正在給一箇舊的(非CDI)代碼,它是如何工作的。讀取參數和實例容器:
@Singleton
public class MySingleton {
private static final MySingleton instance = new MySingleton();
private volatile SecurityHelper securityHelper; // container
public void setSecurityHelper(SecurityHelper secHelper){ securityHelper=secHelper; }
public SecurityHelper getSecurityHelper(){ return securityHelper; }
/* now it has some @Inject....*/
public void start(String passwordP, String passwordH)
.....
// application work with one or two implementations of security
if ("P".equals(DbParams.getServerSecurityFlag()))
instance.setSecurityHelper(new SecurityContainer(new SecurityHelperImplP(DbWorkerImpl.getInstance(), ResponseBuilderImpl.getInstance()),
null));
else
instance.setSecurityHelper(new SecurityContainer(new SecurityHelperImplP(DbWorkerImpl.getInstance(), ResponseBuilderImpl.getInstance()),
new SecurityHelperImplH(DbWorkerImpl.getInstance(), ResponseBuilderImpl.getInstance())));
securityHelper.createSecurity(passwordP, passwordH);
下面是容器類:
public class SecurityContainer implements SecurityHelper {
private SecurityHelper secPrg;
private SecurityHelper secHard;
public SecurityContainer(SecurityHelper secPrg, SecurityHelper secHard){
this.secPrg=secPrg;
this.secHard=secHard;
}
具體實現目前有注入DbWorker和ResponseBuilder EJB豆。 SecurityHelperImplH看起來一樣。
public class SecurityHelperImplP implements SecurityHelper {
private SecurityPrg securityPrg = null;
private DbWorker ora; // now they are CDI managed
private ResponseBuilder builder;
public SecurityHelperImplP(DbWorker dbworker, ResponseBuilder bld){
this.ora = dbworker;
this.builder = bld;
}
我相信我需要預選賽,也許製片人,但不能點
你認爲這個製片人在哪裏? 現在我離開了SecurityContainer,因爲它是。 MySingleton現在是CDI託管的,其他託管的bean使用mySingleton getSecurityHelper()中的securityContainer。 – Buch
無關緊要,只要生產者駐留在另一個CDI bean中(以便CDI能夠找到它)。從CDI 1.2規範 - 「生產者方法必須是託管bean類或會話bean類的默認訪問,公共,受保護或私有的非抽象方法。 「所以我說,把它放在方便你的地方。 – Siliarus
我應該將SecurityHelperImplP和SecurityHelperImplH作爲參數傳遞給Producer嗎?我試圖通過限定符來傳遞它們,但得到編譯錯誤 – Buch