gpt4 book ai didi

java - Spring应用程序上下文有我的bean,但@autowired看不到它

转载 作者:行者123 更新时间:2023-12-02 01:27:21 25 4
gpt4 key购买 nike

我正在尝试使用注释配置我的 Spring Boot 应用程序并在其中使用 @Autowired 注释。当我检查是否加载了 Bean 时,它已加载,但使用 @Autowired 时,它显示 NoSuchBeanDefinitionException

正如您进一步看到的,我尝试检查我的 Bean 是否确实已加载,因此当我运行应用程序时,我可以在控制台中看到我的 Bean 的名称。另外,我尝试将 'scanBasePackages = "com.log.iei.Logistica"' 添加到我的 @SpringBootApplication 注释中,但它没有改变任何内容。另外,我尝试了现场 Autowiring

这是我的主要类(class):

@SpringBootApplication(scanBasePackages = "com.log.iei.Logistica")
public class LogisticaApplication extends Application {
public static ConfigurableApplicationContext context;

@Override
public void init() throws Exception {
SpringApplicationBuilder builder = new SpringApplicationBuilder(AppConfig.class);
context = builder.run(getParameters().getRaw().toArray(new String[0]));
String[] beanNames = context.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}

}

这是VehicleService类的一部分:

@Component("vehicleService")
public class VehicleService implements IDao<VehicleEntity> {

private static VehicleService vehicleService;
GenericDao<VehicleEntity> dao;

public VehicleService(){
dao = new GenericDao<>(VehicleEntity.class);
System.out.println("==== VehicleService was created ====");
}

这是@Autowired 部分的一部分:

@Component("cargoPage")
public class CargoPage extends TablePageTemplate {

@Autowired
public CargoPage(VehicleService vehicleService){
getAboveTableLine().getChildren().addAll(getAboveTableLineSetup());

setTable(getTable(), vehicleService.findAll(), VehicleEntity.getTableMapping());

这里有一个错误:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.log.iei.Logistica.data.controllers.Services.VehicleService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1654)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1213)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1167)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:857)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:760)
... 24 more

UPD:问题可能出在 VehicleService 实现通用接口(interface)上。

最佳答案

首先,您必须将基础包设置为:

@SpringBootApplication(scanBasePackages = "com.log")

您正确映射了所有内容,但是,用于 @Autowire beans 的构造函数不应调用任何其他逻辑。如果您需要在 bean 初始化后立即执行某些操作,请使用 @PostConstruct

这就是您的代码的样子:

@Service
public class CargoPage extends TablePageTemplate {

private VehicleService vehicleService;

@Autowired
public CargoPage(VehicleService vehicleService) {
this.vehicleService = vehicleService;
}

@PostConstruct
public void init() {
getAboveTableLine().getChildren().addAll(getAboveTableLineSetup());
setTable(getTable(), vehicleService.findAll(), VehicleEntity.getTableMapping());
}
}

关于java - Spring应用程序上下文有我的bean,但@autowired看不到它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56709614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com