【问题标题】:How to use HK2 dependency injection in following case?在以下情况下如何使用 HK2 依赖注入?
【发布时间】:2021-06-21 15:48:45
【问题描述】:

Class School,Teacher,Test 属于同一个包,本身就是文件。正如我们在测试文件中看到的,我正在使用 new 来制作所有对象,但我想知道如何使用 HK2 来做同样的事情。我知道如何使用 guice 或 spring(通过使用配置文件或使用 xml 文件),但我不知道如何在 HK2 中进行 DI。我经历了这个source,但即使从那里阅读后也无法做到。

public class School
{
   public Teacher t;
}
public class Teacher
{
   public void intro
   {
      System.out.println("I am Math Teacher");
   }

}
public class Test
{
   public static void main(String[] args)
   {
     School s = new School();
     s.t = new Teacher();
     s.t.intro();
   }
}

如果提供额外的信息,例如如何使用构造函数或设置器对 HK2 进行 DI,将会有很大帮助。

【问题讨论】:

  • Getting Started 是一个很好的起点。您需要一个使用您的服务填充的 ServiceLocator。然后你会从定位器中得到School 类,它会被注入你的Teacher 实例。只需在带有Teacher 参数的School 构造函数上添加@Inject

标签: java dependency-injection hk2


【解决方案1】:

开始使用 HK2 的最简单方法是使用 hk2-inhabitant-generator。 此插件将生成一个META-INF/hk2-locator/default 文件,HK2 将在您调用时使用该文件填充ServiceLocator

ServiceLocatorUtilities.createAndPopulateServiceLocator();

该文件将填充有@Service 注释的服务类。只需将 hk2-inhabitant-generator 插件添加到您的 pom.xml 中

<plugin>
    <groupId>org.glassfish.hk2</groupId>
    <artifactId>hk2-inhabitant-generator</artifactId>
    <version>${hk2.version}</version>
    <executions>
        <execution>
            <goals>
                <goal>generate-inhabitants</goal>
            </goals>
        </execution>
    </executions>
</plugin>

还有课程

@Service
public class School {

    private final Teacher teacher;

    @Inject
    public School(Teacher teacher) {
        this.teacher = teacher;
    }
}

@Service
public class Teacher {

    private final String name;

    public Teacher(String name) {
        this.name = name;
    }

    public Teacher() {
        this(DEFAULT_NAME);
    }
}

然后就可以从ServiceLocator获取服务了

public static void main(String... args) {
    ServiceLocator locator = ServiceLocatorUtilities.createAndPopulateServiceLocator();
    Teacher t = locator.getService(Teacher.class);
    System.out.println(t.getName());
}

完成项目

https://github.com/psamsotha/hk2-getting-started

更新:hk2-metadata-generator

repo 还包括一个分支metadata-generator,它使用hk2-metadata-generator 而不是hk2-inhabitants-generator。两者的区别在于metadata-generator 将在编译期间创建驻留文件。它只需要在编译期间位于类路径上。这可能更自然地使用。您可以将hk2-metadata-generator 包含在maven-compiler-plugin 配置中

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    <inherited>true</inherited>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <annotationProcessorPaths>
            <annotationProcessorPath>
                <groupId>org.glassfish.hk2</groupId>
                <artifactId>hk2-metadata-generator</artifactId>
                <version>${hk2.version}</version>
            </annotationProcessorPath>
        </annotationProcessorPaths>
    </configuration>
</plugin>

另见

【讨论】:

  • 这是说不能调用 getName() 因为 t 是 NULL
  • 请克隆项目,cd到目录并运行README中提到的命令。我刚做了,效果很好。输出是“宫城先生”。请看看你在做什么与项目不同。
  • 我将您答案中的代码 sn-ps 粘贴到文件中,编写了所需的导入语句,然后运行了包含最后一个代码 sn-p 的文件,但它给出了错误。我会尝试使用你的 git 代码
  • 本文中的代码只是sn-ps。我链接到 repo 以获得完整的可运行示例。
  • 我克隆了你的项目并用 eclipse 打开。它仍然给出 t is null 错误(线程“main”java.lang.NullPointerException 中的异常:无法调用“com.demo.Teacher.getName()”,因为“t”在 com.demo.Main.main(Main.爪哇:11))。此外,我在第 40 行的 pom.xml 文件中遇到错误:生命周期配置未涵盖插件执行:org.glassfish.hk2:hk2-inhabitant-generator:3.0.2:generate-inhabitants(执行:默认,阶段:进程-类)。导入语句没有任何错误
猜你喜欢
  • 2020-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-12
  • 2019-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多