繁体   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