복붙노트

[SPRING] JavaFX : Spring 프레임 워크를 JavaFX 앱과 통합 (잘못된 구성)

SPRING

JavaFX : Spring 프레임 워크를 JavaFX 앱과 통합 (잘못된 구성)

저는 JavaFX 응용 프로그램을 작성 중이며 Spring 기능을이 응용 프로그램과 통합하려고합니다. 현재 코드는 오류없이 컴파일되지만 @Transactional 및 @Service로 태그 지정된 서비스 계층 메서드를 요청하면 NullPointerException이 발생합니다. Spring 구성에서 내가 뭘 잘못하고 있는지 이해할 수 없다. JavaFX에 대한 제 코드는 다음과 같습니다.

주 수업 :

public class Main extends Application {

    private static final SpringFxmlLoader loader = new SpringFxmlLoader();

    @Override
    public void start(Stage stage) throws Exception {
       Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("login.fxml"));
        stage.setTitle("APPNAME");
        stage.setScene(new Scene(root, 300, 600));
        stage.setFullScreen(false);
        stage.setMaximized(false);
        stage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }

}


@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages = {"packagename"})
public class ApplicationConfiguration {


    @Bean
    public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        Properties properties = new Properties();
        propertySourcesPlaceholderConfigurer.setProperties(properties);
        return propertySourcesPlaceholderConfigurer;
    }

    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasenames("messages", "org.springframework.security.messages");
        messageSource.setUseCodeAsDefaultMessage(true);
        return messageSource;
    }
}

SpringLoader :

public class SpringFxmlLoader {

    private static final ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationConfiguration.class);

    public Object load(String url) {
        try (InputStream fxmlStream = SpringFxmlLoader.class
                .getResourceAsStream(url)) {
            System.err.println(SpringFxmlLoader.class
                    .getResourceAsStream(url));
            FXMLLoader loader = new FXMLLoader();
            loader.setControllerFactory(new Callback<Class<?>, Object>() {
                @Override
                public Object call(Class<?> clazz) {
                    return applicationContext.getBean(clazz);
                }
            });
            return loader.load(fxmlStream);
        } catch (IOException ioException) {
            throw new RuntimeException(ioException);
        }
    }
}

이제 내 컨트롤러에서 나는 다음과 같은 것을 가지고있다.

@Component
public class Controller implements Initializable {
 @FXML
    public TextField usernameField;
    @FXML
    public PasswordField passwordField;
    @FXML
    public Button submitButton;
@Autowired
    private PersonService personService;
// Now the above personService throws me a NPE.
}

어떻게 든 JavaFX 용 Spring 설정을 망칠까요? 친절하게 알려주세요. 고마워. :-)

최신 정보

제임스 D에서 제안한 변경 후 ... 다음 오류가 발생합니다.

null
Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(LauncherImpl.java:182)
    at com.sun.javafx.application.LauncherImpl$$Lambda$2/1058634310.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: inputStream is null.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2459)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2429)
    at tooltank.MainClass.SpringFxmlLoader.load(SpringFxmlLoader.java:28)
    at tooltank.MainClass.Main.start(Main.java:15)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
    at com.sun.javafx.application.LauncherImpl$$Lambda$57/667705538.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl$$Lambda$53/767743416.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
    at com.sun.javafx.application.PlatformImpl$$Lambda$55/1195477817.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
    at com.sun.javafx.application.PlatformImpl$$Lambda$54/1403425489.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)
    at com.sun.glass.ui.gtk.GtkApplication$$Lambda$43/1429486634.run(Unknown Source)
    ... 1 more

Process finished with exit code 1

SpringFXMLLoader.java에서 다음 라인에서 발생합니다 :

 return loader.load(fxmlStream);

해결법

  1. ==============================

    1.SpringFxmlLoader를 생성했지만 사용하지는 않습니다. 너는 원한다.

    SpringFxmlLoader를 생성했지만 사용하지는 않습니다. 너는 원한다.

    SpringFxmlLoader loader = new SpringFxmlLoader();
    Parent root = (Parent) loader.load(getClass().getResource("login.fxml").toExternalForm());
    

    FXMLLoader를 직접 사용하는 대신

    사실 SpringFxmlLoader를 다르게 작성하여 표준 FXMLLoader API와 조금 더 가깝게 일치 시켰습니다.

    public class SpringFxmlLoader {
    
        private static final ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationConfiguration.class);
    
        public <T> T load(URL url) {
            try  {
                FXMLLoader loader = new FXMLLoader(url);
                loader.setControllerFactory(applicationContext::getBean);
                return loader.load();
            } catch (IOException ioException) {
                throw new RuntimeException(ioException);
            }
        }
    }
    

    그런 다음 시작 메소드는 다음과 같습니다.

    SpringFxmlLoader loader = new SpringFxmlLoader();
    Parent root = loader.load(getClass().getResource("login.fxml"));
    

    설정에 따라 정확한 경로를 찾아야 할 수도 있습니다.

  2. from https://stackoverflow.com/questions/31164743/javafx-integrating-spring-framework-with-javafx-appincorrect-configuration by cc-by-sa and MIT license