簡體   English   中英

依賴注入和容器類(java-ee 7)

[英]Dependency injection and container class (java-ee 7)

我是DI的新手,但突然之間我需要在我的EJB應用程序中使用它,所以我嘗試重新制作它。

該方法包括具有2個字段的容器類--2個實現。 它可以根據參數與一個或兩個實現一起使用。 容器是在singleton的方法調用中創建的,但是被其他ejb bean使用。

在這里,我需要幫助 - 如何使SecurityContainer類與其他CDI托管類(ejb bean)正常工作或成為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 bean。 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 )。

首先,帶有SecurityContainer的字段需要是一個注入點,所以添加@Inject

@Inject
private volatile SecurityHelper securityHelper;  // container

注意:記住要刪除setter方法,在使用CDI時,您不需要/不需要它。

現在我們如何生產它。 你的開始工作基本上已經完成了工作! 您只需要使它成為生產者,設置正確的返回類型並確保您獲得參數。 所以,一步一步:

1)生產者方法參數

生產者方法的所有參數都被自動視為另一種可注射源。 例如,你將能夠@Inject String passwordP )。 哎呀,這不會馬上工作,我不知道你是如何准確地檢索這些參數的。

2)生產方法如何運作

它是CDI 每次需要創建實例調用的方法。 因此,您必須在SecurityHelper范圍內下定決心。 由於這在@Singleton start方法中設置了一次,我想它是在運行時不會改變的東西。 在這種情況下,您可能會想要@ApplicationScoped

3)這一切看起來如何呢?

這是一個應該做你想要的代碼,假設我沒有誤解它:

@Produces  // telling CDI this is how it is supposed to create instances of this type
@ApplicationScoped // what scope does the produced bean have?
public SecurityHelper produceSecHelper() {
  // TODO add logic to retrieve these params!
  String passwordP;
  String passwordH;

  SecurityHelper result;
  // application work with one or two implementations of security
  if ("P".equals(DbParams.getServerSecurityFlag()){
      result = new SecurityContainer(new SecurityHelperImplP(DbWorkerImpl.getInstance(), ResponseBuilderImpl.getInstance()), 
                                null); 
  } else {
      result = new SecurityContainer( new SecurityHelperImplP(DbWorkerImpl.getInstance(), ResponseBuilderImpl.getInstance()), 
                                new SecurityHelperImplH(DbWorkerImpl.getInstance(), ResponseBuilderImpl.getInstance()));
  }
  result.createSecurity(passwordP, passwordH);
  return result;
}

注意:CDI將自行調用此方法。 您不應該手動操作,即使您這樣做,也不會被CDI識別。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM