I have JavaFX application with springboot. The problem is one bean is created 2 times with @PostConstruct and got an exception about serial port already been used. Hovewer, i noticed that I have both @SpringBootApplication which alreade include @ComponentScan and @Configuration annotations. And i have SpringConfig class in root package.
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 - this class created 2 times and invoke exception by openPort() function
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();
}
}
}
Source files hierarchy:
-sample (folder)
-Model (folder)
-ComReader.java
-Controller.java
-Repository (folder)
-CRUD interfaces
-AppStart.java
-SpringConfig.java
In this case i have working program but only receive "port in used" exception.
If I remove @SpringBootApplication annotation from main class, i receive exception - No qyalifying bean of type 'sample.Repository.CallDetailRecordRepository expected at least 1 bean which qualifies as autowire candidate.
If I remove @ComponentScan i receive exception - No qualifying bean of type 'sample.Model.Controller' available; on setContextFactory(ctx::getBean);
It's okay now!
I removed ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.