【问题标题】:Spring MVC autowiring bean throws nestedexceptionSpring MVC 自动装配 bean 抛出嵌套异常
【发布时间】:2014-11-25 14:29:22
【问题描述】:

我在 Spring MVC 4 中的自动装配方面遇到了很大的问题,我已经花了很多时间在这上面。找到了许多解决方案,但没有任何帮助。

我有控制器:

@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;
    ....
}

我有三个模块 library-lib(daos) library-service(services) library-web(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 任何想法如何解决这个问题?

【问题讨论】:

标签: java spring spring-mvc jakarta-ee autowired


【解决方案1】:

尝试在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.

【讨论】:

  • 已添加但仍然出现异常,现在 pbook-unit 出现异常。(entitymanager) 未定义 [javax.persistence.EntityManagerFactory] ​​类型的限定 bean
【解决方案2】:

正如 Jean 所说,您需要将 @Component 添加到您希望自动装配的任何类中。对于服务,Spring 通过使用@Service 标签而不是@Component 标签提供了一些优化。同样,对于 DAO 层,Spring 提供了优化的 @Repository 注解。使用这些注释来启用这些类进行组件扫描。那么,你甚至不应该需要

setPrintedBookDAO() 

方法,因为 Spring 会为您处理自动装配。

【讨论】:

  • 已添加但仍然出现异常,现在 pbook-unit 出现异常。(entitymanager) 未定义 [javax.persistence.EntityManagerFactory] ​​类型的限定 bean
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-09
  • 2015-11-16
  • 1970-01-01
  • 2016-06-23
  • 1970-01-01
  • 2013-05-09
  • 1970-01-01
相关资源
最近更新 更多