我是 Dagger 2 的新手,正在尝试 IntelliJ 中的 Dagger 2 Coffee Example,它似乎不会生成 DaggerCoffeeApp_Coffee(它应该生成它),即使我密切关注 github 中 Dagger 2 的代码示例.
Public class CoffeeApp {
@Singleton
@Component(modules = {DripCoffeeModule.class})
public interface Coffee {
CoffeeMaker maker();
}
public static void main(String args[]){
Coffee coffee = DaggerCoffeeApp_Coffee.builder().build();
}
}
这是我的 pom.xml 文件。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tim.test</groupId>
<artifactId>Dagger2Experiment</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<slf4j-api.version>1.7.12</slf4j-api.version>
</properties>
<repositories>
<repository>
<id>sonatype</id>
<name>sonatype-repo</name>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.google.dagger</groupId>
<artifactId>dagger</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>com.google.dagger</groupId>
<artifactId>dagger-compiler</artifactId>
<version>2.0.1</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j-api.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j-api.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
我尝试了以下主题的各种解决方案,但似乎没有任何效果:
我还在应用程序的运行时中将 jar 文件添加到我的应用程序构建路径 dagger-2.0.1.jar 中,并在编译时将 dagger-compiler-2.0.1.jar 添加到构建路径中。
更新 我在上面的代码 fragment 中使用了 DaggerCoffeeApp_Coffee.builder().build() ,因为在找不到构造函数后我编辑了我的代码以遵循下面 Dagger 2 的 github 中的代码示例。链接如下:
https://github.com/google/dagger/blob/master/examples/simple/src/main/java/coffee/CoffeeApp.java
任何帮助将不胜感激。
最佳答案
更新 是的,你发现我在浏览这个问题。我没能阅读您的代码示例,否则我会发现 pom.xml 不是唯一的问题。
我假设您的 DripCoffeeModule 已正确注释并且在其构造函数中没有参数,因此您无需在构建组件时指定它。例如。 :
@Module public DripCoffeeModule { //Uses default constructor }
我以前从未见过作为内部类实现的组件,但我敢打赌 Dagger 在实例化方面不会对它有任何不同的处理。 (我建议将它移出 CoffeeApp 类。)但是,命名是不正确的。而不是
DaggerCoffeeApp_Coffee.builder().build();
您必须遵循 Dagger 定义的命名约定。来自 Dagger 网站上标题为构建图表的部分:
The implementation has the same name as the interface prefixed with Dagger.
所以你需要将这一行修改为:
DaggerCoffee.builder().build();
或者您可以使用便捷的方法:
DaggerCoffee.create();
如果我对第 1 点的看法是错误的,那么您也需要像这样构建您的模块:
DaggerCoffee.builder().dripCoffeeModule(new DripCoffeeModule()).build();
原创
将编译器从依赖项部分移至编译器部分。来自 Dagger 2 官方网站。
In a Maven project, one would include the runtime in the dependencies section of your pom.xml, and the dagger-compiler artifact as a dependency of the compiler plugin:
例如:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<dependencies>
<dependency>
<groupId>com.google.dagger</groupId>
<artifactId>dagger-compiler</artifactId>
<version>2.0</version>
<optional>true</optional>
</dependency>
</dependencies>
</plugin>
关于java - Dagger 2 组件不生成用于构建的 Dagger 前缀类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32981749/