1
votes

When I run this code it was shows this kind of error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'roomController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.Room.Dao.RoomDao com.Room.Controller.RoomController.roomDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.Room.Dao.RoomDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

How can I solve this error?

Controller:

@RestController
@RequestMapping("/service/user/")
public class RoomController {

@Autowired
RoomDao roomDao;

@RequestMapping(method = RequestMethod.GET,headers="Accept=application/json")
public String getAllUsers() {
    String users="hello welcome";
    return users;
}

public List<RoomMembers> getRoomMembers() {
    List<RoomMembers> roomMemberList=roomDao.listMember();
    //User user=userService.getUserById(id);
    return roomMemberList;
}
}

Model class:

@Entity
@Table(name="RoomMembers")
public class RoomMembers{

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "memberId")
private int memberId;

@Column(name="memberName")
private String memberName;

@Column(name="Phone")
private long phone;

@Column(name="address")
private String address;

public int getMemberId() {
    return memberId;
}

public void setMemberId(int memberId) {
    this.memberId = memberId;
}

public String getMemberName() {
    return memberName;
}

public void setMemberName(String memberName) {
    this.memberName = memberName;
}

public long getPhone() {
    return phone;
}

public void setPhone(long phone) {
    this.phone = phone;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}


}

Dao:

public interface RoomDao {

public List<RoomMembers> listMember();

}

DaoImpl:

@Repository
@Transactional
public class RoomDaoImpl implements RoomDao{

@Autowired
private SessionFactory sessionFactory;

@SuppressWarnings("unchecked")
public List<RoomMembers> listMember() {
    List<RoomMembers> roomMemberList= (List<RoomMembers>)  sessionFactory.getCurrentSession().createCriteria(RoomMembers.class).list();
    return roomMemberList;
}

}

XML:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <context:property-placeholder location="classpath:resources/database.properties" />
<context:component-scan base-package="com.Room.Controller" />
<tx:annotation-driven transaction-manager="hibernateTransactionManager"/>


<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${database.driver}" />
    <property name="url" value="${database.url}" />
    <property name="username" value="${database.user}" />
    <property name="password" value="${database.password}" />
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
    <list>
        <value>com.Room.Model.RoomMembers</value>
    </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>             
        </props>
    </property>
</bean>

<bean id="hibernateTransactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
<mvc:annotation-driven />

2
check your <context:component-scan base-package="com.Room.Controller" />, possible your dao impl out of the scanningSergii Getman
You're doing the component-scan in com.Room.Controller, do you have your components there?Carlos Rivero
Where you have defined RoomDao bean? I don't see it in the xml file.Sachin Mesare

2 Answers

1
votes

Document : context component scan is used scan for classes(annotated) to create beans.

In your case roomDao is a bean which has to create while initializing. But in your case you are just scanning controllers,so only controller bean will be created not others which present other than com.Room.Controller package.

 <context:component-scan base-package="com.Room.Controller" />

So make it to scan all annotated classes. Then all required(annotated) beans will be created and BeanCreationException will go.

0
votes

Most of the times when you develop a springboot application and perform dependency injection in other classes, you get an error stating

{@org.springframework.beans.factory.annotation.Autowired(required=true)}

This is because the Main of the application resides in a package for example (com.example.project)

and your other packages have a different naming structure such as

com.example.entites,
com.example.services,
com.example.repositories

And hence, your (main class) cannot find these other packages automatically.

What you have to do to resolve this problem, is to name your other packages to follow the (main) package structure, so it scans them automatically, so you have the error bean problem resolved.

So now our package structure will have to be (refactored) and (renamed) as follows:

com.example.project.entities
com.example.project.services
com.example.project.repositories

And voila!!!, the main class scans all the other packages automatically, since it is within its folder.

Don't forget, (a package) is a folder, hence, all the other (packages) are stored within the (package) and hence, it can easily find the other folders within it.