java - Spring Boot @Autowired 字段为空
问题描述
我的Abc.classabcController
中有一个为空的字段:
@Service(id = "ABC", name = "service-abc") // THIS IS NOT SPRING ANNOTATION it marks class as Service Singleton, loads custom annotations like @AfterInitialization
@SpringBootApplication
@ComponentScan(basePackages = "com.package*")
@ComponentScan("healthchecker")
public class Abc {
@Autowired
AbcController abcController;
@AfterInitialization
public void afterInit() throws GwConfigurationException {
// initialize Spring Boot
SpringApplication application = new SpringApplication(Dmsgw.class);
// run with command line arguments
CmdLineArguments cmdLineArguments = new CmdLineArguments();
String[] args = cmdLineArguments.getArgStrings();
if (args == null) {
args = new String[0];
}
application.run(args);
abcController.doSomething(); // abcController is null
}
}
这是我的AbcController.class:
@Service
public class AbcController {
...// some fields
...// some methods
}
到目前为止,我尝试abcController
使用@Service
or@Component
或 or进行注释,@Controller
但它是相同的。
这是MyServiceConfiguration.class
:
@Configuration
public class ServiceConfiguration {
@Bean
public AbcController defaultAbcController() {
return new AbcController();
}
}
我检查并加载了Bean。
解决方案
推荐阅读
- android - 如何获得使用范围存储的 Android 11 中的 File 对象编辑文件的权限?
- python - 有没有办法用熊猫只提取一列csv文件?
- c++ - OpenSSL 链接创建非常大的 exec 文件大小
- mongodb - 具有嵌套数组的 MongoDB 映射过滤器未按预期工作
- python - 通过传递 vars 在 python 中生成 bash 脚本
- amazon-web-services - http2 请求在 AWS 上通过 route53 连接的云端不起作用
- amazon-web-services - 双重代理请求 - 获取原始 IP
- python - 使用 python 自动化 Google Meet
- c++ - QPixmap - 当尺寸大于源图像时如何重复像素图
- oracle-apex - Oracle Apex 19 - 从日期选择器中选择空值