[英]could not autowire spring
I am trying to learn spring MVC and created a rest service but I am getting bean autowiring problem. 我正在尝试学习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
The structure of my spring project looks like 我的春天项目的结构看起来像
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
Following is how the files looks like 以下是文件的外观
dispatcher-servlet.xml 调度员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>
spring-user.xml 弹簧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>
spring-module.xml 弹簧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>
All the other implementation like controller, DAO, service and model look like 所有其他实现,如控制器,DAO,服务和模型看起来像
loginController.java loginController.java
@RestController
public class loginController {
@Autowired
userDaoImpl userService; //Service which will do all data retrieval/manipulation work
//other methods
}
userDAO.java userDAO.java
public interface userDAO {
public void insert(user _user);
public user findUserByEmail(String email);
}
userDaoImpl.java userDaoImpl.java
public class userDaoImpl implements userDAO{
private DataSource dataSource;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
//other methods
}
Mark your daoimpl as 将你的daoimpl标记为
@repository public class userDaoImpl implements userDAO{} @repository public class userDaoImpl实现userDAO {}
Autowire interface rather marking an concrete class. 自动装配界面而非标记具体类。 Go with java config for spring application much easier to understand.
使用java配置进行spring应用程序更容易理解。
Could you change the following: 你能改变以下内容吗?
@RestController
public class loginController {
@Autowired
userDaoImpl userService; //Service which will do all data retrieval/manipulation work
//other methods
}
by the following: 通过以下方式:
@RestController
public class loginController {
@Autowired
@Qualifier("userDAO")
userDaoImpl userService; //Service which will do all data retrieval/manipulation work
//other methods
}
and see the result? 看到结果?
ur having one setter injection in the bean u want to autowire u have to do something like this 你在豆子里有一个注射器你想要自动装备你必须做这样的事情
//Autowired annotation on variable/setters is equivalent to autowire="byType"
@Autowired
private Employee employee;
@Autowired
public void setEmployee(Employee emp){
this.employee=emp;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.