没有合格的bean类型可用 [英] No qualifying bean of type available
本文介绍了没有合格的bean类型可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
线程main中的异常org.springframework.beans.factory.NoSuchBeanDefinitionException:没有'com.spring.dto.Car2'类型的限定bean可用
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.spring.dto.Car2' available
Controller.java -
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 -
Car2.java -
@ToString @Component
public class Car2 {
@Autowired
private Engine engine;
}
Engine.java -
Engine.java -
@Setter @ToString
public class Engine {
private String modelYear;
}
configu.xml -
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项目是为了我的简单。
please ignore the annotation @ToString and @Setter- I am using Lombok project for my simplicity.
推荐答案
我认为错误在于:
<context:component-scan base-package="com.spring.dto.Car2" />
该值应该是包,而不是类。将其更改为com.spring.dto,然后它应该可以工作。
The value should be a package, not a class. Change it to "com.spring.dto", then it should work.
这篇关于没有合格的bean类型可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文