繁体   English   中英

没有可用的合格Bean类型

[英]No qualifying bean of type available

线程“主” 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" />

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM