java - 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);
解决方案
现在好了!
我删除了 ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
推荐阅读
- c# - 如何为 Hangfire 的 mySQL 数据库设置“connectionString”?
- javascript - 更改 React 父组件状态
- javascript - 使用 appendChild 将图像添加到元素会产生错误
- android - 使用 ConstraintSet 以编程方式向视图添加约束不会在 Android 中给出预期结果?
- r - 如何在 R 中删除包含任何非字母字符(连字符和撇号除外)的单词
- algorithm - 检测 stl 网格文件中的特征
- docker - 如何在正在运行的 docker 容器中配置自动启动服务 [nginx]
- java - 打印字符串中不同字母的数量并打印出现频率最高的字母的次数
- python - 如何修复''分配前引用的局部变量'结果'''
- python - 将元素 1×1 排队还是一个大对象排队更好(更快)