2016-05-29 142 views
0

我正在開發Spring 4 MVC項目來學習spring rest框架。這是一個課程項目,我正在創建一個Web應用程序。我已經設法從各種教程在線編寫一些Spring代碼。我收到豆autowiring的問題。spring bean不能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 
+2

您的命名選擇不好;它們都不遵循Java編碼標準。你在使用Spring 4;我建議不要混合使用XML和註釋。始終如一。啓動時的信息應該明確什麼是加載和什麼不是。 – duffymo

回答

0

我沒有看到調度員servlet.xml中的任何地方你有進口彈簧module.xml。這就是你的bean沒有在sprint環境中加載的原因。

0

你不必在你的類路徑下面的類,

<bean id="userDAO" class="dao.impl.userDaoImpl"> 

它應該是什麼,

<bean id="userDAO" class="taxApp.dao.userDaoImpl"> 

這是因爲xml配置優先於Java配置,並且xml配置已損壞。

此外,如果您從xml中刪除了以上userDAO bean聲明,它將自動使用@Service("userDAO")進行選擇,它應該可以工作。

但是,你不應該不應該同時使用Java和xml配置,而不會理解Spring。而你的命名轉換不可能是java。