【问题标题】:springboot create bean 2 timesspring boot 创建 bean 2 次
【发布时间】:2020-04-27 06:18:28
【问题描述】:

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

Main.class

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) 上;

【问题讨论】:

  • builder.run() 不为您创建一个ApplicationContext 吗?因此,您正在启动两个应用程序上下文。如果删除ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class); 行会发生什么?
  • @James_D,它工作正常,但我声明 SpringConfig 现在它工作正常或自动添加这个自定义配置? // new AnnotationConfigApplicationContext(SpringConfig.class);
  • SpringBootApplication 等价于 EnableAutoConfiguration。所以是的,它会自动添加你的配置类。
  • @RinatIbragimov 你还在面对这个问题吗?如果是这样,我想我可以帮助你... :)

标签: java spring javafx


【解决方案1】:

现在好了!

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

【讨论】:

    猜你喜欢
    • 2016-12-15
    • 1970-01-01
    • 2017-02-03
    • 2019-12-25
    • 2020-07-02
    • 2018-01-20
    • 1970-01-01
    • 2019-04-03
    • 2019-09-08
    相关资源
    最近更新 更多