<context:component-scan />
,但是当我运行我的应用程序时,控制器无法找到,除非我还将<context:component-scan />
添加到dispatcher-servlet.xml中。我在两个文件中都使用相同的base-package,因此问题不在于此。我感到困惑,因为我认为applicationContext.xml是dispatcher-servlet.xml的父级。难道在applicationContext.xml中放置
<context:component-scan />
就不够吗?web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
编辑:我还在dispatcher-servlet.xml中使用了mvc:annotation-driven,这应该可以识别控制器(我想?)。
编辑2:这里是配置文件。 我从applicationContext.xml中删除了一堆Spring Security和OAuth设置(出于安全原因和它们可能不相关的考虑)。
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:sec="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context" xmlns:oauth="http://www.springframework.org/schema/security/oauth2"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd
http://www.springframework.org/schema/security/oauth2 http://www.springframework.org/schema/security/spring-security-oauth2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:component-scan base-package="bar.foo"/>
<context:property-placeholder location="classpath:my.properties" />
<bean class="bar.foo.ServicesConfig" />
</beans>
调度器Servlet配置文件.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:component-scan base-package="bar.foo.controller" />
<mvc:annotation-driven/>
<mvc:default-servlet-handler />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
<property name="order" value="2" />
</bean>
<bean id="contentViewResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
</map>
</property>
<property name="defaultViews">
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
</property>
<property name="order" value="1" />
</bean>
</beans>
编辑 3:好的,这很有趣。 我的服务和dao类位于一个不同的项目(JAR)中,我从Web项目引用它们。 我正在使用基于Java的配置,并从applicationContext.xml进行引用:
EDIT 3: 好的,这很有意思。我的服务和DAO类在一个不同的项目(JAR)中,我从 Web 项目中引用它们。我正在使用基于 Java 的配置,并从 applicationContext.xml 进行引用:
<bean class="bar.foo.config.ServicesConfig" />
所以,这意味着我的Web项目(其中包含applicationContext.xml)中只有Controller注释。回顾一下,从我的applicationContext.xml中删除context:component-scan不应该有任何影响,因为除了@Controller之外,没有任何注释(修复为编辑:还有一些@Autowired注释)。但是,当我从applicationContext.xml中删除context:component-scan时,它会说找不到我的Service类的Controllers(从dispatcher servlet scan中找到)。引用ServicesConfig不应该足够吗?这是ServicesConfig类的参考资料-它有自己的组件扫描用于Services,这些Services与applicationContext.xml正在扫描的内容不同。@Configuration
@ComponentScan({ "some.other.package", "another.package" })
@ImportResource({ "classpath:commonBeans.xml" })
@PropertySource({ "classpath:services.properties",
"classpath:misc.properties" })
public class ServicesConfig {
// Bean definitions //
}
解决方案:
当我从根上下文中删除context:component-scan时,控制器无法获取自动装配的服务bean。这是因为根上下文引用了我的服务基于Java的配置Bean,但我没有设置根上下文来扫描组件。因此,当我将组件扫描添加到根上下文(applicationContext.xml)中时,一切都可以正常工作。这是我现在拥有的内容:
applicationContext.xml:
<bean class="bar.foo.config.ServicesConfig" />
<context:component-scan base-package="bar.foo.config" />
调度器servlet.xml:
<context:component-scan base-package="bar.foo.controller" />
我已经设置了Web上下文来捕获控制器(Controller)、自动装配(Autowired)和控制器包中的任何其他注释 - 我不确定这是否是最佳实践。