我在自動裝配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 任何想法如何解決這個問題?
您可以在github上找到ProjLibrary以獲取更多實施細節。 https://github.com/Cospel/ProjLibrary – Cospel 2014-11-25 16:07:13