作为Google Guice的新功能,我尝试在JavaFX项目中使用它,目的是向控制器类注入服务。但是java.lang.reflect.InvocationTargetException
总是出现。主要部分在这里:
public class App extends Application {
private static Scene scene;
private static Injector injector;
@Override
public void start(Stage stage) throws IOException {
scene = new Scene(loadFXML("ui"), 640, 480);
stage.setScene(scene);
stage.show();
}
private static Parent loadFXML(String fxml) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(fxml + ".fxml"));
fxmlLoader.setControllerFactory(initilizedClass->{
return injector.getInstance(initilizedClass);
});
return fxmlLoader.load();
}
public static void main(String[] args) {
launch();
}
public void init() {
App.injector = Guice.createInjector(new DiModule());
}
//...
}
//
public class DiModule extends AbstractModule {
@Override
protected void configure() {
bind(IUserListService.class).to(BroadcastUserListService.class);
}
}
//
public interface IUserListService {}
public class BroadcastUserListService implements IUserListService {}
//
public class UIController {
@FXML
private ListView listView;
@Inject
private IUserListService us;
public void initialize() {
//listView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
us.sendOnlineMessage();
}
}
我使用模块,错误详细信息是:
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: com.google.common.util.concurrent.ExecutionError: java.lang.ExceptionInInitializerError
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2199)
at com.google.common.cache.LocalCache.get(LocalCache.java:3934)
at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3938)
at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4821)
at com.google.common.cache.LocalCache$LocalLoadingCache.getUnchecked(LocalCache.java:4827)
at guice@4.0/com.google.inject.internal.FailableCache.get(FailableCache.java:48)
at guice@4.0/com.google.inject.internal.ConstructorInjectorStore.get(ConstructorInjectorStore.java:50)
at guice@4.0/com.google.inject.internal.ConstructorBindingImpl.initialize(ConstructorBindingImpl.java:136)
at guice@4.0/com.google.inject.internal.InjectorImpl.initializeJitBinding(InjectorImpl.java:547)
at guice@4.0/com.google.inject.internal.InjectorImpl.createJustInTimeBinding(InjectorImpl.java:884)
at guice@4.0/com.google.inject.internal.InjectorImpl.createJustInTimeBindingRecursive(InjectorImpl.java:805)
at guice@4.0/com.google.inject.internal.InjectorImpl.getJustInTimeBinding(InjectorImpl.java:282)
at guice@4.0/com.google.inject.internal.InjectorImpl.getBindingOrThrow(InjectorImpl.java:214)
at guice@4.0/com.google.inject.internal.InjectorImpl.getInternalFactory(InjectorImpl.java:890)
at guice@4.0/com.google.inject.internal.FactoryProxy.notify(FactoryProxy.java:46)
at guice@4.0/com.google.inject.internal.ProcessedBindingData.runCreationListeners(ProcessedBindingData.java:50)
at guice@4.0/com.google.inject.internal.InternalInjectorCreator.initializeStatically(InternalInjectorCreator.java:134)
at guice@4.0/com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:107)
at guice@4.0/com.google.inject.Guice.createInjector(Guice.java:96)
at guice@4.0/com.google.inject.Guice.createInjector(Guice.java:73)
at guice@4.0/com.google.inject.Guice.createInjector(Guice.java:62)
at com.freelance.ChatterBox/com.freelance.ChatterBox.App.main(App.java:43)
... 11 more
Caused by: java.lang.ExceptionInInitializerError
at guice@4.0/com.google.inject.internal.cglib.reflect.$FastClassEmitter.<init>(FastClassEmitter.java:67)
at guice@4.0/com.google.inject.internal.cglib.reflect.$FastClass$Generator.generateClass(FastClass.java:72)
at guice@4.0/com.google.inject.internal.cglib.core.$DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
at guice@4.0/com.google.inject.internal.cglib.core.$AbstractClassGenerator.create(AbstractClassGenerator.java:216)
at guice@4.0/com.google.inject.internal.cglib.reflect.$FastClass$Generator.create(FastClass.java:64)
at guice@4.0/com.google.inject.internal.BytecodeGen.newFastClass(BytecodeGen.java:204)
at guice@4.0/com.google.inject.internal.DefaultConstructionProxyFactory.create(DefaultConstructionProxyFactory.java:54)
at guice@4.0/com.google.inject.internal.ProxyFactory.create(ProxyFactory.java:159)
at guice@4.0/com.google.inject.internal.ConstructorInjectorStore.createConstructor(ConstructorInjectorStore.java:90)
at guice@4.0/com.google.inject.internal.ConstructorInjectorStore.access$000(ConstructorInjectorStore.java:29)
at guice@4.0/com.google.inject.internal.ConstructorInjectorStore$1.create(ConstructorInjectorStore.java:37)
at guice@4.0/com.google.inject.internal.ConstructorInjectorStore$1.create(ConstructorInjectorStore.java:33)
at guice@4.0/com.google.inject.internal.FailableCache$1.load(FailableCache.java:37)
at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3524)
at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2317)
at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2280)
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2195)
... 32 more
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to module guice
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:340)
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:280)
at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:198)
at java.base/java.lang.reflect.Method.setAccessible(Method.java:192)
at guice@4.0/com.google.inject.internal.cglib.core.$ReflectUtils$2.run(ReflectUtils.java:56)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at guice@4.0/com.google.inject.internal.cglib.core.$ReflectUtils.<clinit>(ReflectUtils.java:46)
... 49 more
Exception running application com.freelance.ChatterBox.App