我有加载FXML文件和控制器的基本类。当我尝试运行测试时,它会抛出ExceptionInInitializerError:null。如果我评论loader.setControllerFactory(context::getBean);
,则没有错误。有没有人知道什么是错的?感谢帮助!
用于加载FXML文件和控制器的类:
@Component
public class FXMLreader {
private final ApplicationContext context;
private final ResourceBundle resourceBundle;
@Autowired
public FXMLreader(ApplicationContext context, ResourceBundle resourceBundle) {
this.context = context;
this.resourceBundle = resourceBundle;
}
public Parent load(String fxmlPath) throws IOException{
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource(fxmlPath));
loader.setControllerFactory(context::getBean);
loader.setResources(resourceBundle);
Parent parent = loader.load();
return parent;
}
}
测试类:
@RunWith(SpringRunner.class)
@SpringBootTest
public class FXMLreaderTest {
@Autowired
FXMLreader fXMLreader;
public FXMLreaderTest() {
}
@Test
public void test(){
try {
Parent parent = fXMLreader.load("/fxml/Login.fxml");
assertNotEquals(null, parent);
} catch (IOException ex) {
Logger.getLogger(FXMLreaderTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
主要课程
@SpringBootApplication
@EnableAspectJAutoProxy
public class MainAppClass extends Application{
private static final Logger LOG = Logger.getLogger(MainAppClass.class);
protected ConfigurableApplicationContext springContext;
@Override
public void init() throws Exception{
springContext = SpringApplicationContext();
}
@Override
public void start(Stage primaryStage) throws Exception {
}
@Override
public void stop() throws Exception{
springContext.close();
}
public static void main(String[] args) {
launch(args);
}
private ConfigurableApplicationContext SpringApplicationContext() {
SpringApplicationBuilder builder = new SpringApplicationBuilder(MainAppClass.class);
String[] args = getParameters().getRaw().stream().toArray(String[]::new);
builder.profiles("dev");
return builder.run(args);
}
错误
java.lang.ExceptionInInitializerError: null
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:273)
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:268)
at com.sun.javafx.application.PlatformImpl.setPlatformUserAgentStylesheet(PlatformImpl.java:550)
at com.sun.javafx.application.PlatformImpl.setDefaultPlatformUserAgentStylesheet(PlatformImpl.java:512)
at javafx.scene.control.Control.<clinit>(Control.java:87)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.Class.newInstance(Class.java:442)
at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.constructValue(FXMLLoader.java:1009)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:746)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at com.myapp.view.FXMLreader.load(FXMLreader.java:43)
at com.myapp.view.FXMLreaderTest.test(FXMLreaderTest.java:63)