我在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
printbookserviceimpl中的是:
@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 任何想法如何解决这个问题?
答案 0 :(得分:0)
尝试在@Component
上添加PrintedBookDAOImpl
注释,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.
答案 1 :(得分:0)
正如Jean所说,你需要将@Component添加到你想要自动装配的任何类中。对于服务,Spring通过使用@Service标记而不是@Component标记来提供一些优化。类似地,对于DAO层,Spring提供了优化的@Repository注释。使用这些注释可以为组件扫描启用这些类。那么,你甚至不需要
setPrintedBookDAO()
方法,因为Spring会照顾你的自动装配。