正如标题所示,我们的应用程序的存储库没有初始化,但我们也没有实例化,正如其他在线修复所建议的那样。
这是应用程序的游戏启动器。
public class JavaFxApplication extends Application {
private static ConfigurableApplicationContext context;
public static void main(String[] args) {
Application.launch(JavaFxApplication.class, args);
}
@Override
public void init() throws Exception {
ApplicationContextInitializer<GenericApplicationContext> initializer =
ac -> {
ac.registerBean(Application.class, () -> JavaFxApplication.this);
ac.registerBean(Parameters.class, this::getParameters);
ac.registerBean(HostServices.class, this::getHostServices);
};
context = new SpringApplicationBuilder()
.sources(PacManLauncher.class)
.initializers(initializer)
.run(getParameters().getRaw().toArray(new String[0]));
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Pac-Man");
primaryStage.getIcons().add(new Image("images/pacman/pacman.png"));
Parent root = new LoginPage(primaryStage);
Scene scene = new Scene(root, 600, 400);
scene.getStylesheets().add("stylesheet/stylesheet.css");
primaryStage.setScene(scene);
primaryStage.show();
}
@Override
public void stop() throws Exception {
context.close();
Platform.exit();
}
}
这是登录验证页面。
//more code
@Autowired
private UserRepository userRepository;
private static BCryptPasswordEncoder passwordEncoder;
/**
* Instantiates a new Authentication service with the User repository.
*
* @param userRepository the user repository
*/
public AuthenticationService(UserRepository userRepository) {
this.userRepository = userRepository;
}
/**
* Login form.
*
* @param username the username
* @param password the password
* @return a boolean (true if user is correctly logged in, false if the opposite)
*/
public boolean login(String username, String password) {
passwordEncoder = new BCryptPasswordEncoder();
Optional<User> user = userRepository.findByName(username);
if (user.isPresent()) {
if (passwordEncoder.matches(password,
user.get().getPassword())) {
UserSession.getInstance(username, user.get().getId());
}
}
return (UserSession.getUserId() != null && UserSession.getUserName() != null);
}
//more code
登录代码在一个单独的项目中工作,但在整个项目中,在 Autowiring 上为我们提供了一个空指针。有谁知道我们如何在不出现空指针的情况下创建存储库,或者您至少可以通过一些来源引导我们走向正确的方向。
提前致谢。
编辑:我在应用程序上放置了错误的游戏启动器,因此如果有人可以提供帮助,我们将不胜感激。 :)
最佳答案
Edit
当您更新问题时,我也更新了我的答案。我仍然指出我之前描述过的类似事情!
- 您的
JavaFxApplication
类上缺少@SpringBootApplication
注释,并且您的PacManLauncher
中似乎有它 您的 JavaFxApplication 主类(用 @SpringBootApplication 注解)必须位于父包中,而存储库、组件、配置类必须位于子目录中,以便
spring-boot
将注入(inject)/配置所有必需的组件、配置和存储库。
我也更新了您的代码,
@SpringBootApplication
public class JavaFxApplication extends Application {
private static ConfigurableApplicationContext context;
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void init() throws Exception {
context = springBootApplicationContext();
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Pac-Man");
primaryStage.getIcons().add(new Image("images/pacman/pacman.png"));
Parent root = new LoginPage(primaryStage);
Scene scene = new Scene(root, 600, 400);
scene.getStylesheets().add("stylesheet/stylesheet.css");
primaryStage.setScene(scene);
primaryStage.show();
}
@Override
public void stop() throws Exception {
context.close();
}
private ConfigurableApplicationContext springBootApplicationContext() {
SpringApplicationBuilder builder = new SpringApplicationBuilder(JavaFxApplication.class);
String[] args = getParameters().getRaw().stream().toArray(String[]::new);
return builder.run(args);
}
}
PS: I have removed your code from
init()
method which may not required as the above code can inject every spring components. I also removed source ->PacManLauncher
class as well. Please runJavaFxApplication
.. I hope it should fix your issue:)
关于java - 应用程序启动时存储库不会初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59198816/