gpt4 book ai didi

java - 应用程序启动时存储库未初始化

转载 作者:行者123 更新时间:2023-12-04 10:55:54 25 4
gpt4 key购买 nike

正如标题所说,我们的应用程序的存储库没有被初始化,但我们也没有实例化,正如其他在线修复所建议的那样。

这是应用程序的游戏启动器。

    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



当您更新您的问题时,我也更新了我的答案。而且我仍然指出我之前描述的类似事情!
  • 你不见了@SpringBootApplication您的 JavaFxApplication 上的注释类,似乎你有它在你的PacManLauncher
  • 您的 JavaFxApplication main-class (注解@SpringBootApplication) 必须在父包和repositories ,组件,子目录中的配置类,以便spring-boot将注入(inject)/配置所有必需的组件、配置和存储库。

    Spring-Boot application directory tree structure

  • 我也更新了你的代码,
    @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 run JavaFxApplication .. I hope it should fix your issue:)

    关于java - 应用程序启动时存储库未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59198816/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com