我正在尝试学习spring MVC并创建了一个休息服务,但我得到了bean自动装配问题。
Could not autowire field: taxApp.dao.daoImpl.userDaoImpl taxApp.controller.loginController.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [taxApp.dao.daoImpl.userDaoImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency
我的春季项目的结构看起来像
src
main
java
taxApp
controller
loginController.java
model
user.java
dao
daoImpl
userDaoImpl.java
userDAO.java
resources
database
data-source-cfg.xml
user
spring-user.xml
spring-module.xml
webapp
web-inf
dispatcher-servlet.xml
web.xml
index.jsp
以下是文件的外观
调度-servlet.xml中
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="taxApp" />
<context:annotation-config />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
弹簧user.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="userDAO" class="dao.impl.userDaoImpl">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
弹簧module.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Using Oracle datasource -->
<import resource="database/data-source-cfg.xml" />
<import resource="dao/spring-user.xml" />
</beans>
所有其他实现,如控制器,DAO,服务和模型看起来像
loginController.java
@RestController
public class loginController {
@Autowired
userDaoImpl userService; //Service which will do all data retrieval/manipulation work
//other methods
}
userDAO.java
public interface userDAO {
public void insert(user _user);
public user findUserByEmail(String email);
}
userDaoImpl.java
public class userDaoImpl implements userDAO{
private DataSource dataSource;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
//other methods
}
答案 0 :(得分:0)
将你的daoimpl标记为
@Repository public class userDaoImpl实现userDAO {}
自动装配界面而非标记具体类。 使用java配置进行spring应用程序更容易理解。
答案 1 :(得分:0)
您可以更改以下内容:
@RestController
public class loginController {
@Autowired
userDaoImpl userService; //Service which will do all data retrieval/manipulation work
//other methods
}
通过以下内容:
@RestController
public class loginController {
@Autowired
@Qualifier("userDAO")
userDaoImpl userService; //Service which will do all data retrieval/manipulation work
//other methods
}
并查看结果?
答案 2 :(得分:0)
你在豆子里有一个二传手注射你想自动装你必须做这样的事情
//Autowired annotation on variable/setters is equivalent to autowire="byType"
@Autowired
private Employee employee;
@Autowired
public void setEmployee(Employee emp){
this.employee=emp;
}