【发布时间】:2020-10-28 11:10:49
【问题描述】:
我正在开发一个具有可重用逻辑的通用 Java 库,以与一些 AWS 服务进行交互,而这些服务又将被多个消费者应用程序使用。出于here 概述的原因,以及 Spring Boot 似乎为 SQS 集成之类的东西提供了许多样板免费代码这一事实,我决定将这个公共库实现为具有自动配置的自定义 Spring Boot 启动器。
我也是 Spring 框架的新手,因此遇到了一个问题,即我的自动配置类的实例变量没有通过 AutoWired 注释初始化。
为了更好地解释这一点,这里是我常见依赖项的一个非常简化的版本。
CommonCore.java
@Component
public class CommonCore {
@AutoWired
ReadProperties readProperties;
@AutoWired
SqsListener sqsListener; // this will be based on spring-cloud-starter-aws-messaging
public CommonCore() {
Properties props = readProperties.loadCoreProperties();
//initialize stuff
}
processEvents(){
// starts processing events from a kinesis stream.
}
}
ReadProperties.java
@Component
public class ReadProperties {
@Value("${some.property.from.application.properties}")
private String someProperty;
public Properties loadCoreProperties() {
Properties properties = new Properties();
properties.setProperty("some.property", someProperty);
return properties;
}
}
CoreAutoConfiguration.java
@Configuration
public class CommonCoreAutoConfiguration {
@Bean
public CommonCore getCommonCore() {
return new CommonCore();
}
}
通用依赖将被其他应用程序使用,如下所示:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
public class SampleConsumerApp implements ApplicationRunner {
@Autowired
CommonCore commonCore;
public SampleConsumerApp() {
}
public static void main(String[] args) {
SpringApplication.run(SampleConsumerApp.class, args);
}
@Override
public void run(ApplicationArguments args) {
try {
commonCore.processEvents();
} catch (Exception e) {
e.printStackTrace();
}
}
}
我提到的主要问题是CommonCore 实例中的AutoWired 对象未按预期初始化。但是,我认为实际的问题是根深蒂固的;但由于我对 Spring 框架缺乏了解,我发现自己很难调试。
我希望在这些方面提供一些指导
- 这种开发自定义启动器的方法对我的用例有意义吗?
- AutoWired 依赖项无法使用这种方法初始化的原因是什么?
【问题讨论】:
-
很难这样说,但也许它们根本没有实例化。例如,我会为其中一个@Component 类(例如
ReadProperties)创建一个无参数构造函数,并在那里放置一个日志行或一个调试器断点,看看它是否被构造。也许这与您的组件扫描范围有关 -
感谢您的回复。我试过了,构造函数没有被调用。我想要进行自动配置的原因是避免必须为使用公共库的应用程序定义自定义组件扫描路径。如果我在 CommonCoreAutoConfiguration 类本身中自动连接,它就可以工作。但不在我需要的 CommonCore 类中
-
你能从核心 jar 中分享你的 META-INF/spring.factories 文件吗?
标签: java spring spring-boot