我试图更好地掌握 Spring 处理依赖注入的方式,但我遇到了一个(至少对我而言)非常奇怪的情况。
我有一个分层的应用程序,可以直观地描述如下:web - 业务 - 域模型 - dal ......每个层都在单独的模块中。
我正在使用注释来注入我的类中需要的东西,并使事情变得更加复杂,我在业务层中使用了 cucumber,这意味着我需要在测试时显式地注入一个测试 bean,而不是在不测试时显式地注入真正的 bean。
一切都编译得很好,甚至运行和注入 ( @Inject
) 一切都很好......直到我将此行添加到 web 层的 context.xml 中:
<context:component-scan base-package="com.foo.bar.baz.bleep.bll" />
...这就是我的问题开始的地方。添加该行后,编译成功完成,但是当我在 Tomcat 中运行它时,出现此错误:
Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sampleAppServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.foo.bar.baz.bleep.dal.SampleAppDao com.foo.bar.baz.bleep.bll.SampleAppServiceImpl.sampleAppDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.foo.bar.baz.bleep.dal.SampleAppDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.inject.Inject()}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4797)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5291)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:662)
突然间,容器中缺少 dao(?)。
我想在 web 层中使用业务层的业务服务,但是一旦我尝试将 bll 上的扫描添加到 web 层,业务层就会中断。是什么赋予了?
我web-context.xml
在 web 层
<context:component-scan base-package="com.foo.bar.baz.bleep.bll" />
<context:component-scan base-package="com.foo.bar.baz.bleep.domainmodel" />
<context:component-scan base-package="com.foo.bar.baz.bleep.web" />
我cucumber.xml
在业务层
<context:component-scan base-package="com.foo.bar.baz.bleep.bll" />
<context:component-scan base-package="com.foo.bar.baz.bleep.domainmodel" />
<import resource="classpath:business-context.xml" />
我的两个business-context.xml
测试business-context.xml
<bean id="sampleAppDao" class="com.foo.bar.baz.bleep.dal.inMemorySampleAppDaoImpl"></bean>
真实的business-context.xml
<bean id="sampleAppDao" class="com.foo.bar.baz.bleep.dal.sampleAppDaoImpl"></bean>
我的实现都用适当的组件注释进行了注释(即web-servlet 上的@Controller、业务服务上的@Service 和dao 上的@Repository)所有模块都对pom.xml 中的其他模块具有必要的依赖关系。
我有两个问题:
我究竟做错了什么?显然 =)
我把这个自动布线业务搞错了吗?我在想我只会扫描每个模块特别需要的包,而不是所有东西(
context:component-scan base-package="com.foo" />
),就像我在许多教程中看到的那样。有一点需要注意:我之前在其他项目中尝试过扫描所有内容,但遇到了同样的问题,这就是为什么我开始扫描每个模块所需的内容。
请告诉我如何使这项工作!
更新 1
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Display Name Here</display-name>
<!-- Spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/web-context.xml</param-value>
</context-param>
<!-- Vaadin servlet -->
<servlet>
<servlet-name>Coconut Scooter</servlet-name>
<servlet-class>ru.xpoft.vaadin.SpringVaadinServlet</servlet-class>
<init-param>
<description>Vaadin UI to display</description>
<param-name>beanName</param-name>
<param-value>mainUI</param-value>
</init-param>
<init-param>
<description>Application widgetset</description>
<param-name>widgetset</param-name>
<param-value>com.foo.bar.baz.bleep.common.widgetset.AppWidgetSet</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Coconut Scooter</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Coconut Scooter</servlet-name>
<url-pattern>/VAADIN/*</url-pattern>
</servlet-mapping>
<context-param>
<description>Vaadin production mode</description>
<param-name>productionMode</param-name>
<param-value>false</param-value>
</context-param>
</web-app>
网络上下文.xml
<context:component-scan base-package="com.foo.bar.baz.bleep"
use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
黄瓜.xml
<context:component-scan base-package="com.foo.bar.baz.bleep">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<import resource="classpath:business-context.xml" />
更新 2
正如 M. Deinum 的回答中的评论一样,当既不使用 Spring MVC 也不使用 dispatcherservlet 时,您可以安全地将所有上下文文件加载到 ContextLoaderListener 中,以便将您的 bean 加载到单个 ApplicationContext 中。这可以通过简单地使用扫描所有包的组件扫描来完成。请参阅下面更新的上下文文件。
网络上下文.xml
<context:component-scan base-package="com.foo.bar.baz.bleep" />
黄瓜.xml
<context:component-scan base-package="com.foo.bar.baz.bleep" />
<import resource="classpath:business-context.xml" />