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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40107252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:24:38  来源:igfitidea点击:

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

javaspringhibernatespring-mvc

提问by Manihtraa

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)}

org.springframework.beans.factory.BeanCreationException:创建名为“roomController”的 bean 时出错:自动装配依赖项的注入失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:com.Room.Dao.RoomDao com.Room.Controller.RoomController.roomDao; 嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:未找到类型为 [com.Room.Dao.RoomDao] 的合格 bean 依赖项:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{@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:

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:

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

采纳答案by Prasanna Kumar H A

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

文档:上下文组件扫描用于扫描类(带注释)以创建 bean。

In your case roomDaois a bean which has to create while initializing. But in your case you are just scanning controllers,so only controller bean will be created notothers which present other than com.Room.Controllerpackage.

在您的情况下roomDao是一个必须在初始化时创建的 bean。但是在您的情况下,您只是在扫描控制器,因此只会创建控制器 bean,而不是除了com.Room.Controller包之外的其他控制器 bean 。

 <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 BeanCreationExceptionwill go.

所以让它扫描所有带注释的类。然后将创建所有必需的(带注释的)bean 并将其BeanCreationException删除。