Я работаю над проектом весны 4 MVC, чтобы узнать рамки весеннего отдыха. Это проект класса, в котором я создаю веб-приложение. Мне удалось написать некоторый весенний код из различных обучающих онлайн. Я получаю bean autowiring
проблема.пружинная фасоль не смогла autowire
Ошибка
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
Эти примеры файлов
loginController.java
@RestController
public class loginController {
@Autowired
userDAO 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
@Service("userDAO")
public class userDaoImpl implements userDAO{
@Autowired
private DataSource dataSource;
//other methods
}
Я создал файлы конфигурации, но не уверен, что они правильно размещены. Например, мой dispatcher.xml в папке web-inf, в то время как другие 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>
пружинно-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>
web.xml
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- Spring MVC -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Структура моего весеннего проекта выглядит
src
main
java
taxApp
controller
loginController.java
model
user.java
dao
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
Ваши варианты именования плохие; ни один из них не соответствует стандартам кодирования Java. Вы используете Spring 4; Я бы рекомендовал не смешивать XML и аннотации. Быть последовательным. Сообщения при запуске должны четко указывать, что загружается, а что нет. – duffymo