java - Spring在父类和子类之间的使用。 Spring 注入(inject)后设定值的问题

标签 java spring

我有一个父类和一个子类,如下所示:

@Component
public class Parent {
  public AndroidDriver<MobileElement> driver;

  public void method() {};
}


@Component
public class Child extends Parent {

  @Override
  public void method() {

     //do something with the parent's driver variable
     driver.findElement(...);
  }
}

现在在我的主类中,我注入(inject)了这两个类。为了记录在案,我必须在收到用户的一些输入后设置驱动程序变量,如下所示。

@Resource
private Parent parent;

@Resource
private Child child;

private static ConfigurableApplicationContext applicationContext;

public static void main(String[] args)
{
     applicationContext = SpringApplication.run(DemoAppiumProjectApplication.class, args);
}

@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx)
    {
        //Get some value from the user input, then set driver;
        parent.setDriver(new AndroidDriver<>(${some input from the user}));

        child.method();
    }

我的猜测是,parent.setDriver(driver)已设置,但它不是它扩展的注入(inject)子类的父实例,因此当它进入child.method时,driver变量为null并最终为空指针异常。

我可以调用child.setDriver来实现我的目标,但是如果我有一组子类并且我只希望父类调用一次setDriver,那么所有子类都可以访问它怎么办? 我怎样才能正确地做到这一点?

最佳答案

执行代码时会发生如下情况:

@Resource
private Parent parent;

这将创建一个 Parent 类型的 bean,它将在您注入(inject) Parent 字段的任何地方使用。为了简单起见,我们将创建的对象称为:object1

parent.setDriver(new AndroidDriver<>(${some input from the user}));

这显然是在object1上执行的。 object1 现在有一个驱动程序集。

@Resource
private Child child;

这里创建了一个Child类型的bean。由于object1是Parent类型的bean,因此它不能被重用,因此必须创建一个新的bean。这是object2

child.method();

...
@Override
  public void method() {

     //do something with the parent's driver variable
     driver.findElement(...);
  }

这里发生的情况是,您尝试调用object2上的驱动程序,但该驱动程序从未设置。 这就是问题发生的原因。

解决方案:

我目前能想到的唯一解决方案是将Parent bean注入(inject)Child类中:

@Component
public class Parent {
  private AndroidDriver<MobileElement> driver;

  public AndroidDriver<MobileElement> getDriver() {
    return driver;
  }

  public void setDriver(AndroidDriver<MobileElement> driver) {
    this.driver = driver;
  }
}


@Component
public class Child {

  private Parent parent;

  @Autowired //or @Resource, depends on what you want
  public Child(Parent parent) {
    this.parent = parent;
  }

  public void method() {

     this.parent.getDriver().findElement(...);
  }
}

主类

@Resource
private Parent parent;

@Resource
private Child child;

private static ConfigurableApplicationContext applicationContext;

public static void main(String[] args)
{
     applicationContext = SpringApplication.run(DemoAppiumProjectApplication.class, args);
}

@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx)
    {
        //Get some value from the user input, then set driver;
        parent.setDriver(new AndroidDriver<>(${some input from the user}));

        child.method();
    }

这也意味着现在每个先前的子类都必须注入(inject) Parent bean,但这是您无法摆脱的东西。

我希望这会有所帮助。

关于java - Spring在父类和子类之间的使用。 Spring 注入(inject)后设定值的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61316912/

相关文章:

java - 在 Spring Boot 中存储和检索密码以形成服务器连接

java - 带/不带 TRUNCATE_EXISTING 的 StandardOpenOption.WRITE + StandardOpenOption.CREATE 之间的区别?

java - 如何让客户端为每个其他客户端创建一个套接字

java - 嵌入式ActiveMQ启动报错 : Temporary Store limit is 51200 mb

java - netbeans freermarker 插件错误 : The plugin Lexer to NetBeans Bridge is requested in implementation version

java - Ignite Spring Data 支持 Spring Boot 2 吗?

java - 通过多个键查找集合的条目

java - 野蝇 8/JAX-RS : UriInfo is null when injected into RequestScoped bean

java - 是否有可视化工具可以检查 Java 代码库并报告包间依赖关系?

Spring SpEL 访问 Configuration-Bean