简体   繁体   English

应用程序初始化后如何调用类中的方法?

[英]How to call a method in a class after the application has been initalized?

I have classes as below.我有如下课程。 I want statements in constructor of DataService to be called itself after the application or DataService class has has been initialized and before DataHandler is intitalized.我希望在应用程序或 DataService 类初始化之后和 DataHandler 初始化之前调用 DataService 构造函数中的语句本身。 But dataLoader object is null in the constructor.但是 dataLoader 对象在构造函数中为空。 I am new to guice and want to know how can i achieve this using GUICE我是 guice 的新手,想知道如何使用 GUICE 实现这一目标

@Singleton
@Managed
class DataService{

    @Inject private DataLoader dataLoader;

   DataService(){
      dataLoader.load(); // I am trying to udnerstand why dataLoader is null?
   }
}

@Singleton
@Managed
class DataHandler{

    @EventHandler
    public void handle(StaticData data){
       //some logic om data
    }
}

Class StaticDataModule extends AbstractModule{

    @Override
    protected void configure(){
        bind(DataService.class).asEagerSingletin();
        bind(DataHandler.class).asEarSingleton();
    }
}

dataLoader is null because it is not yet initialized dataLoadernull因为它尚未初始化

You request an injection of a field, yet you try to call it before it is even initialized.您请求注入一个字段,但您尝试在它被初始化之前调用它。

In order, what is executed?依次执行什么?

  1. The direct assignment of fields.字段的直接分配。 This means private double pi = 3.14;这意味着private double pi = 3.14;
  2. The constructor.构造函数。
  3. Everything outside the constructor构造函数之外的一切

You have to understand that Guide is outside the constructor, even @Inject .您必须了解 Guide 位于构造函数之外,甚至是@Inject When you instantiate an object, its constructor is first called, then the fields are injected.当您实例化一个对象时,首先调用其构造函数,然后注入字段。 It's like in Spring or in Java EE: the initialization is done using another method, annotated in @PostConstruct .这就像在 Spring 或 Java EE 中一样:初始化是使用另一种方法完成的,在@PostConstruct注释。 However Guide doesn't have such annotation.但是 Guide 没有这样的注释。

So the solutions to solve your problem are因此,解决您的问题的解决方案是

  1. either you inject your field as a constructor parameter, so that you can initialize during the constructor, or要么将您的字段作为构造函数参数注入,以便您可以在构造函数期间进行初始化,要么
  2. After your Injector is created, you get your instance and call its @PostConstruct -like method that you created.创建 Injector 后,您将获取实例并调用您创建的@PostConstruct的方法。

Example:例子:

Injector injector = Guice.createInjector(...);
DataService dataService = injector.getInstance(DataService.class);
dataService.init();

And your DataService is as follows:您的DataService如下所示:

class DataService {
  @Inject DataLoader loader;
  void init() {
    loader.load();
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java - 当抽象类中的方法调用已被覆盖的方法时,继承如何在java中工作? - how does inheritance work in java when a method in abstract class call a method that has been overriden? 如何验证没有调用任何类的方法? - How to verify, that no method of a class has been called? 即使已在子类(java)中重写了该类中的调用方法 - Call method in class even if it has been overridden in a subclass (java) 如何调用在另一个类中创建的一个类的对象? - how to call object of one class that has been created in another class? 仅在JavaFX中的SequentialTransition完成后才如何调用函数? - How to call a function only after a SequentialTransition in JavaFX has been completed? 如何使用已作为变量传递的类中的方法 - How to Use Method from a Class that has been Passed in as a Variable 如何执行已上传的类的方法? - How can I execute a method of a class that has been uploaded? ping 站点后如何运行方法? - How to run a method after the site has been pinged? 如何解决:提交响应后无法调用 sendRedirect() - How to fix: Cannot call sendRedirect() after the response has been committed 如何声明在抛出异常后没有调用方法? - How to assert that a method has not been called after an exception has been thrown?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM