Spring应用程序上下文包含我的bean,但@autowired看不到它

时间:2019-06-21 19:48:51

标签: java spring autowired

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

如您所见,我尝试检查Bean是否已真正加载,因此在运行应用程序时,可以在控制台中看到Bean的名称。 另外,我尝试将'scanBasePackages =“ com.log.iei.Logistica”添加到我的@SpringBootApplication批注中,但是它什么都没有改变。 另外,我尝试了现场自动接线

这是我的主要课程:

@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实现通用接口上。

2 个答案:

答案 0 :(得分:0)

您应该检查文件是否在基本软件包中。 例如,如果您有:

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);
       }

   }

答案 1 :(得分:0)

首先,您必须将基本软件包设置为此:

@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());
    }
}