没有可用类型的合格 bean

No qualifying bean of type available

线程 "main" 中的异常 org.springframework.beans.factory.NoSuchBeanDefinitionException: 没有可用的 'com.spring.dto.Car2' 类型的合格 bean


Controller.java -

public class Controller {
public static void main(String[] args) {
    ConfigurableApplicationContext  context = new ClassPathXmlApplicationContext("configu.xml");
    Car2 c2 = (Car2) context.getBean(Car2.class);
    System.out.println(c2);
}

}


Car2.java -

@ToString @Component
public class Car2 {
    @Autowired
    private Engine engine;
}

Engine.java -

@Setter @ToString
public class Engine {
    private String modelYear;
   }

configu.xml -

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <context:component-scan base-package="com.spring.dto.Car2" />
    <context:annotation-config />
    <bean  class="com.spring.dto.Engine">
    <property name="modelYear" value="2015"></property>
    </bean>

    </beans>

请忽略注释@ToString 和@Setter- 为了简单起见,我正在使用 Lombok 项目。

我认为错误在这里:

<context:component-scan base-package="com.spring.dto.Car2" />

该值应该是一个包,而不是 class。将其更改为 "com.spring.dto",然后它应该可以工作。