У меня большие проблемы с autowiring весной 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;
}
....
}
в DAOS в PrintedBookServiceImpl интерфейсы
Th реализации электронных даосские выглядеть следующим образом:
public class PrintedBookDAOImpl implements PrintedBookDAO, GenericDAO<PrintedBook> {
@PersistenceContext(unitName = "pbook-unit", type = PersistenceContextType.EXTENDED)
private EntityManager em;
....
}
У меня есть три модуля библиотека Пб (DAOS) библиотека-сервис (услуги) библиотека-сеть (весна 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>
Когда я запустить веб (на 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 Любые идеи, как решить эту проблему?
Вы можете найти ProjLibrary на github для получения более подробной информации о реализации. https://github.com/Cospel/ProjLibrary – Cospel