2014-11-25 63 views
0

我在自動裝配Spring MVC 4時遇到了很大的問題,而且我已經花了很多時間在它上面了。找到很多解決方案,但沒有什麼幫助Spring MVC autowiring bean throws nestedexception

我有控制器:

@Controller 
public class PrintedBookController { 
    @Autowired 
    PrintedBookService pbookService; // interface 

    @RequestMapping(value = "/pbook", method = RequestMethod.GET) 
    public ModelAndView pbook() { 
     return new ModelAndView("pbook", "command", new PrintedBookDTO()); 
    } 
... 
} 

也有:

PrintedBookService // this is interface 

PrintedBookServiceImpl // this is implementation of PrintedBookService 

在printedbookserviceimpl是:

@Service 
@Transactional 
public class PrintedBookServiceImpl implements PrintedBookService { 

    @Autowired 
    private PrintedBookDAO pbookDao; 

    @Autowired 
    private BookDAO bookDao; 

    @Autowired 
    private LoanDAO loanDao; 


    public void setPrintedBookDao(PrintedBookDAO pbookDao) { 
     this.pbookDao = pbookDao; 
    } 
    .... 
} 

在PrintedBookServiceImpl的DAOS是接口

釷ËDAO實現看起來就像這樣:

public class PrintedBookDAOImpl implements PrintedBookDAO, GenericDAO<PrintedBook> { 

    @PersistenceContext(unitName = "pbook-unit", type = PersistenceContextType.EXTENDED) 
    private EntityManager em; 
    .... 
} 

我有三個模塊庫LIB(DAOS)圖書館服務(服務)庫網(Spring MVC的)。 圖書館MVC有控制器的xml:

<beans xmlns="http://www.springframework.org/schema/beans" 
    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-3.0.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <context:component-scan base-package="cz.fi.muni.pa165.library.web" /> 
    <context:component-scan base-package="cz.fi.muni.pa165.service" /> 
    <context:component-scan base-package="cz.fi.muni.pa165.dao" /> 


    <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> 

和web.xml

<web-app id="WebApp_ID" version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 

    <display-name>Spring MVC Application</display-name> 

    <servlet> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <servlet-class> 
         org.springframework.web.servlet.DispatcherServlet 
       </servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value> 
    </context-param> 

    <listener> 
     <listener-class> 
         org.springframework.web.context.ContextLoaderListener 
       </listener-class> 
    </listener> 

</web-app> 

當我運行web(上tomcat8)它讓我異常:

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: cz.fi.muni.pa165.service.PrintedBookServiceImpl cz.fi.muni.pa165.library.web.PrintedBookController.pbookService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'printedBookServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cz.fi.muni.pa165.dao.PrintedBookDAO cz.fi.muni.pa165.service.PrintedBookServiceImpl.pbookDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [cz.fi.muni.pa165.dao.PrintedBookDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'printedBookServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cz.fi.muni.pa165.dao.PrintedBookDAO cz.fi.muni.pa165.service.PrintedBookServiceImpl.pbookDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [cz.fi.muni.pa165.dao.PrintedBookDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 

也得到這個:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pbookDao': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'pbook-unit' is defined 

該項目在GitHub上https://github.com/Cospel/ProjLibrary 任何想法如何解決這個問題?

+0

您可以在github上找到ProjLibrary以獲取更多實施細節。 https://github.com/Cospel/ProjLibrary – Cospel 2014-11-25 16:07:13

回答

0

嘗試在PrintedBookDAOImpl上添加@Component註釋,Spring在上下文中找不到PrintedBookDAO類型的任何bean。

查看跟蹤這一部分:

No qualifying bean of type [cz.fi.muni.pa165.dao.PrintedBookDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.

+0

已添加,但仍然得到異常,現在異常與pbook-unit。(entitymanager)沒有定義[javax.persistence.EntityManagerFactory]類型的限定bean – Cospel 2014-11-25 15:27:58

0

正如讓說,你需要添加@Component您要自動裝配任何類。對於服務,Spring通過使用@Service標籤而不是@Component標籤來提供一些優化。同樣,對於DAO層,Spring提供了一個優化的@Repository註釋。使用這些註釋來啓用這些類進行組件掃描。那麼,你甚至不需要

setPrintedBookDAO() 

的方法,因爲春天會照顧你的自動裝配。

+0

已添加,但仍然得到異常,現在pbook-unit異常(entitymanager)沒有定義[javax.persistence.EntityManagerFactory]類型的合格bean – Cospel 2014-11-25 15:27:18