Sabunkar Tejas Sahailesh
Sabunkar Tejas Sahailesh

Reputation: 4926

No qualifying bean of type available

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.

Upvotes: 1

Views: 7408

Answers (1)

Michael Borgwardt
Michael Borgwardt

Reputation: 346536

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.

Upvotes: 4

Related Questions