Spring 应用程序上下文有我的 bean 但 @autowired 没有看到它
Spring application context has my bean but @autowired doesn't see it
我正在尝试使用注释配置我的 Spring 引导应用程序,并在其中使用 @Autowired 注释。当我检查我的 Bean 是否已加载时,它已加载,但使用 @Autowired 时显示 NoSuchBeanDefinitionException
如您所见,我尝试检查我的 Bean 是否实际加载,因此当我 运行 我的应用程序时,我可以在控制台中看到我的 Bean 的名称。
此外,我尝试将 'scanBasePackages = "com.log.iei.Logistica"' 添加到我的 @SpringBootApplication 注释中,但它没有任何改变。
另外,我尝试了字段自动装配
这是我的主要内容 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 的一部分 class:
@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 上。
您应该检查您的文件是否在基本包中。
例如,如果您有:
com.log
.service
VehicleService.java
CargoPage.java
LogisticaAplication.java
因此,在您的 LogisticaApplication.java 中,您应该按以下方式添加基数:
@SpringBootApplication(scanBasePackages = "com.log")
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);
}
}
首先,您必须将基础包设置为:
@SpringBootApplication(scanBasePackages = "com.log")
您正确映射了所有内容,但是,用于 @Autowire
bean 的构造函数不应该调用任何其他逻辑。如果您需要在 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());
}
}
我正在尝试使用注释配置我的 Spring 引导应用程序,并在其中使用 @Autowired 注释。当我检查我的 Bean 是否已加载时,它已加载,但使用 @Autowired 时显示 NoSuchBeanDefinitionException
如您所见,我尝试检查我的 Bean 是否实际加载,因此当我 运行 我的应用程序时,我可以在控制台中看到我的 Bean 的名称。 此外,我尝试将 'scanBasePackages = "com.log.iei.Logistica"' 添加到我的 @SpringBootApplication 注释中,但它没有任何改变。 另外,我尝试了字段自动装配
这是我的主要内容 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 的一部分 class:
@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 上。
您应该检查您的文件是否在基本包中。 例如,如果您有:
com.log
.service
VehicleService.java
CargoPage.java
LogisticaAplication.java
因此,在您的 LogisticaApplication.java 中,您应该按以下方式添加基数:
@SpringBootApplication(scanBasePackages = "com.log")
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);
}
}
首先,您必须将基础包设置为:
@SpringBootApplication(scanBasePackages = "com.log")
您正确映射了所有内容,但是,用于 @Autowire
bean 的构造函数不应该调用任何其他逻辑。如果您需要在 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());
}
}