gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:06:06 26 4
gpt4 key购买 nike

当我运行此代码时,它显示了这种错误:

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

如何解决这个错误?

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

模型类:

@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;
}


}

道:

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

最佳答案

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

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

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

所以让它扫描所有带注释的类。然后将创建所有必需的(带注释的)bean,并且 BeanCreationException 将消失。

关于java - 注释 : {@org. springframework.beans.factory.annotation.Autowired(required=true)},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40107252/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com