2016-05-29 4 views
1

Я пытаюсь изучить весну MVC и создал сервис отдыха, но я получаю проблему с автопостановкой бобов.не удалось автоувеличивать весну

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

Вместо '@Autowired userDaoImpl' использовать' @Autowired userDao'. Программа для интерфейса не конкретных классов. –

ответ

0

Все ваши daoimpl как

@repository общественного класса userDaoImpl реализует userDAO {}

Autowire интерфейс, а маркировки конкретного класса. Пойдите с java config для весеннего приложения, намного легче понять.

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 
} 

и увидеть результат?

0

У Р, имеющий одну инъекцию сеттера в бобе и хотите autowire у вас сделать что-то вроде этого

//Autowired annotation on variable/setters is equivalent to autowire="byType" 
    @Autowired 
    private Employee employee; 

    @Autowired 
    public void setEmployee(Employee emp){ 
     this.employee=emp; 
    } 
Смежные вопросы