No qualifying bean of type available

做~自己de王妃 提交于 2019-12-21 22:50:50

问题


Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.spring.dto.Car2' available


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>

please ignore the annotation @ToString and @Setter- I am using Lombok project for my simplicity.


回答1:


I think the error is here:

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

The value should be a package, not a class. Change it to "com.spring.dto", then it should work.



来源:https://stackoverflow.com/questions/46487567/no-qualifying-bean-of-type-available

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!