springboot javafx整合

最近学习javafx 使用spring容器构建,maven建的项目

只引入spring容器

pom文件



    4.0.0

    www.dcssn.com
    weian
    1.0-SNAPSHOT

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.2.RELEASE
         
    

    
        
            org.springframework.boot
            spring-context
        

        
            org.springframework.boot
            spring-boot-starter-data-mongodb
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            net.sourceforge.htmlunit
            htmlunit
            2.25
        
        
        
            org.apache.poi
            poi
            3.16-beta2
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    1.8
                    1.8
                
            
        
    

不要用

            org.springframework.cloud
            spring-cloud-starter-web
这个会开启web服务,只用spring-context就好了
main函数是这样的
package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.concurrent.CompletableFuture;

@SpringBootApplication
public class Main extends Application {
    public static ConfigurableApplicationContext applicationContext;

    @Override
    public void init() throws Exception {
        CompletableFuture.supplyAsync(() -> {
            ConfigurableApplicationContext ctx = SpringApplication.run(this.getClass());
            return ctx;
        }).thenAccept(this::launchApplicationView);
//        Main.applicationContext = SpringApplication.run(this.getClass());
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 600, 400));
        primaryStage.show();
    }

    private void launchApplicationView(ConfigurableApplicationContext ctx) {
        Main.applicationContext = ctx;
    }

    public static void main(String[] args) {
        launch(args);
    }
}
@Autowired
CustomerRepository repository;
从spring 容器中获取bean
CustomerRepository repository = (CustomerRepository) applicationContext.getBeanFactory().getBean("customerRepository");

开启springboot 服务

https://github.com/lihy123456/springboot-javafx.git,这个只是基础的配置,可以git clone 下来参考,用javafx内置的浏览器访问我们的springboot,就是下载什么的要自己实现,有需要的留言,我再把代码贴上来

springboot javafx整合_第1张图片


你可能感兴趣的:(javaFx)