2014-11-25 66 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