2016-05-02 12 views
3

JavaFXでSpringブート1.3.3を使用しています。アプリケーションが正常にSpringアプリケーションコンテキストがnullです

@SpringBootApplication 
public class PirconApplication extends Application{ 

@Bean(name = "primaryStage") 
public Stage getPrimaryStage() { 
    return new Stage(StageStyle.DECORATED); 
} 

private ConfigurableApplicationContext applicationContext = null; 
private static String[] args = null; 

@Override 
public void stop() throws Exception { 
    super.stop(); 
    Platform.exit(); 
    applicationContext.close(); 
} 

@Override 
public void start(Stage primaryStage) throws Exception { 
    Task<Object> worker = worker(); 
    worker.setOnSucceeded(new EventHandler<WorkerStateEvent>() { 
     @Override 
     public void handle(WorkerStateEvent event) { 
      try { 
       AppSplashController loader = applicationContext.getBean(AppSplashController.class); 
       Stage stage = applicationContext.getBean(Stage.class); 
       stage.setScene(loader.init()); 
       stage.initStyle(StageStyle.UNDECORATED); 
       stage.show(); 
      } catch (IOException e) { 
        e.printStackTrace(); 
        System.exit(0); 
      } 
     } 
    }); 
    worker.setOnFailed(new EventHandler<WorkerStateEvent>() { 

      @Override 
      public void handle(WorkerStateEvent event) { 
        // TODO Auto-generated method stub 
        System.exit(0); 
      } 
    }); 
    worker.run(); 
} 
public static void main(String[] args) { 
    PirconApplication.args = args; 
    launch(PirconApplication.class, args); 
} 

private Task<Object> worker() { 
    return new Task<Object>() { 
     @Override 
     protected Object call() throws Exception { 
       applicationContext = SpringApplication.run(PirconApplication.class, args); 
       return null; 
     } 
    }; 
} 
} 

(この時点でのApplicationContextがnullではない)これでスプラッシュ画面を起動する問題は、すべてのNULLでAppSplashControllerのApplicationContextとprimaryStage豆です。

@Component 
public class AppSplashController implements BootInitializable { 

@FXML 
private ImageView imgLoading; 
@FXML 
private Text lblWelcome; 
@FXML 
private Text lblRudy; 
@FXML 
private VBox vboxBottom; 
@FXML 
private Label lblClose; 

private Stage primaryStage; 

private ApplicationContext springContainer; 
/** 
* Initializes the controller class. 
*/ 
@Override 
public void initialize(URL url, ResourceBundle rb) { 
    System.out.println("applicationContext: "+springContainer); 
    System.out.println("primaryStage: "+primaryStage); 
} 

} 

@Autowired 
@Override 
public void setPrimaryStage(Stage primaryStage) { 
    this.primaryStage = primaryStage; 
} 

@Override 
public Scene init() throws IOException { 
    Parent root = FXMLLoader.load(getClass().getResource("/com/pircon/views/splash.fxml")); 
    return new Scene(root); 
} 

@Override 
public void setApplicationContext(ApplicationContext ac) throws BeansException { 
    this.springContainer = ac; 
} 

} 

BootInitializableインタフェース

public interface BootInitializable extends Initializable, ApplicationContextAware { 
    public Scene init() throws IOException; 
    public void setPrimaryStage(Stage primaryStage); 
} 

この春や春ブーツの私の最初のプロジェクトがあるので、自分の無知をご容赦。

答えて

5

私はこれらの豆をautowireに設定するにはspringを指定する必要があると思います。たとえば、

@Autowired 
private ApplicationContext springContainer; 

もう1つの提案は、Spring ApplicationContextAwareインターフェイスを実装するUse ApplicationContextProviderです。

public class ApplicationContextProvider implements ApplicationContextAware { 
    private static ApplicationContext context; 

    public static ApplicationContext getApplicationContext() { 
     return context; 
    } 

    @Override 
    public void setApplicationContext(ApplicationContext ctx) { 
     context = ctx; 
    } 
} 

あなたはApplicationContextProvider.getApplicationContext();

+0

注意を経由してアプリケーション・コンテキストを取得できます。提供されるこの問題のない完全なコンテキストが存在しないため、この答えは、コメントに基づいている豆がヌルであることを述べました。 – vinayknl

+0

autowiredアノテーションを追加しようとしましたが、まだsystem.outのときはnullが返されます。問題はビュー(fxml)のコントローラにありますが、私はprimaryStageとspringContainerにアクセスできません。 –

+0

もう1つの提案は、ApplicationContextAwareを実装するApplicationContextProviderを使用することです。答えを更新します。 – vinayknl

関連する問題