Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 />

like image 552
Manihtraa Avatar asked Jul 10 '26 08:07

Manihtraa


1 Answers

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.

like image 62
Prasanna Kumar H A Avatar answered Jul 13 '26 22:07

Prasanna Kumar H A



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!