我在我的项目中遇到一个奇怪的错误,我将Guice添加到了POM.xml中的依赖项中,一切似乎都很好,即使我在编写启动它建议我的Injector的代码时,IDE也能提供所有建议我想使用的方法,但后来将其标记为红色,表示找不到,因为我从未经历过这种情况。也许我忘记了什么。谁能帮我吗?
我的项目是一个Maven JavaFX桌面应用程序。
这是错误的输出
/Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home/bin/java -Dmaven.multiModuleProjectDirectory=/Users/ochamo/Documents/Proyectos/apde-alumnus/APDEAlumnus "-Dmaven.home=/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3" "-Dclassworlds.conf=/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3/bin/m2.conf" "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=52291:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath "/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3/boot/plexus-classworlds-2.5.2.jar" org.codehaus.classworlds.Launcher -Didea.version2019.1 com.zenjava:javafx-maven-plugin:8.8.3:jar
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building APDE-Alumnus 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> javafx-maven-plugin:8.8.3:jar (default-cli) > [jfxjar]package @ APDE-Alumnus >>>
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ APDE-Alumnus ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 10 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ APDE-Alumnus ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 13 source files to /Users/ochamo/Documents/Proyectos/apde-alumnus/APDEAlumnus/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /Users/ochamo/Documents/Proyectos/apde-alumnus/APDEAlumnus/src/main/java/edu/apde/alumnus/main/Main.java:[22,47] cannot find symbol
symbol: class createInjector
location: class com.google.inject.Guice
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.584 s
[INFO] Finished at: 2019-04-03T12:10:32-06:00
[INFO] Final Memory: 21M/309M
[INFO] ------------------------------------------------------------------------
这是pom.xml中的Guice依赖项
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.2.1</version>
</dependency>
我用来在
中引导主类的代码public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
try {
final Injector injector = new Guice.createInjector(new MainModule()); // this is the source of the error It cannot find createInjector.
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(Main.class.getResource("/fxml/main/main-view.fxml"));
System.out.printf("location: " + fxmlLoader.getLocation());
GridPane root = fxmlLoader.load();
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setTitle("Login");
primaryStage.show();
} catch (IllegalStateException ex) {
ex.printStackTrace();
}
}
}
答案 0 :(得分:0)
createInjector
方法是 静态 方法(请参见Javadocs)...
public static Injector createInjector(Module... modules) {
return createInjector((Iterable)Arrays.asList(modules));
}
当您尝试像这样调用它时...
new Guice().createInjector(...)
...您正在尝试调用 instance 方法,由于{{1中没有名为createInjector
的实例方法,该方法无法编译}}类。
相反,您应该像这样调用它:
Guice
要清楚;只需调用Guice.createInjector(...)
即可调用new Guice()
。
对于背景:
Guice
new Foo().doIt()