首页 > 解决方案 > springboot创建bean 2次

问题描述

我有带有 springboot 的 JavaFX 应用程序。问题是一个 bean 使用 @PostConstruct 创建了 2 次,并且出现了关于已使用串行端口的异常。然而,我注意到我有两个 @SpringBootApplication 已经包含 @ComponentScan 和 @Configuration 注释。我在根包中有 SpringConfig 类。

主类

package sample;

@SpringBootApplication
public class AppStart extends Application {

    private Stage primaryStage;

    @Override
    public void start(Stage primaryStage) throws Exception {
        this.primaryStage = primaryStage;
        Platform.setImplicitExit(false);
        Parent root = FXMLLoader.load(getClass().getResource("/primal.fxml"));
        primaryStage.setTitle("ИС СиАТВ АО ГНЦ НИИАР");
        primaryStage.getIcons().add(new Image("/icon.png"));
        primaryStage.setScene(new Scene(root, 1400, 900));
        createTray();
        primaryStage.show();

        primaryStage.setOnCloseRequest(event -> {
            primaryStage.hide();
        });

    }


    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);

        SpringApplicationBuilder builder = new SpringApplicationBuilder(AppStart.class);
        builder.headless(false);
        ConfigurableApplicationContext context = builder.run(args);

        launch(args);
    }

SpringConfig

package sample;
@Configuration
    @PropertySource({"classpath:com.properties", "classpath:application.properties"})
    @ComponentScan
    public class SpringConfig {

        @Bean
        public SerialPort serialPort(@Value("${serialPort.portName}") String portName){
            return new SerialPort(portName);
        }

        @Bean
        public AnnotationMBeanExporter annotationMBeanExporter(){
            AnnotationMBeanExporter annotationMBeanExporter = new AnnotationMBeanExporter();
            annotationMBeanExporter.addExcludedBean("dataSource");
            return annotationMBeanExporter;
        }
    }

ComReader - 此类创建 2 次并通过 openPort() 函数调用异常

package sample.Model
@Component
public class ComReader {

    @Autowired
    private EventListener eventListener;

    @Autowired
    public SerialPort serialPort;

    @Value("${serialPort.baudRate}")
    private int baudRate;
    @Value("${serialPort.dataBits}")
    private int dataBits;
    @Value("${serialPort.stopBits}")
    private int stopBits;
    @Value("${serialPort.parity}")
    private int parity;

    @PostConstruct
    public void init(){
        try {
            System.out.println("Opening port: " + serialPort.getPortName());
            serialPort.openPort();
            serialPort.setParams(baudRate,dataBits,stopBits,parity);
            serialPort.addEventListener(eventListener, 1);
        } catch (SerialPortException e) {
            e.printStackTrace();
        }
    }
}

源文件层次结构:

-sample (folder)
  -Model (folder)
    -ComReader.java
    -Controller.java
  -Repository (folder)
    -CRUD interfaces
  -AppStart.java
  -SpringConfig.java

在这种情况下,我有工作程序,但只收到“使用中的端口”异常。

如果我从主类中删除 @SpringBootApplication 注释,我会收到异常 -没有 'sample.Repository.CallDetailRecordRepository 类型的 qyalifying bean 预计至少有 1 个 bean 有资格作为自动装配候选者。

如果我删除 @ComponentScan 我会收到异常 -没有可用的“sample.Model.Controller”类型的合格 bean;在 setContextFactory(ctx::getBean);

标签: javaspringjavafx

解决方案


现在好了!

我删除了 ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);


推荐阅读