为什么<servlet>节点中必须有<param-name>为contextConfigLocation的contextConfigLocation?因为如果注释所有的<init-param>组,那么项目将不会加载?
<?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" 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>Archetype Created Web Application</display-name>
<!-- Spring MVC dispatcher servlet -->
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Loads Spring configurations from files -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-mvc.xml,
/WEB-INF/spring-security.xml,
/WEB-INF/hibernate-context.xml
</param-value>
</context-param>
<!-- Spring Security filter -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>发布于 2016-11-13 19:33:43
放spring配置文件的正确位置是什么?
您可以通过servlet类的<context-param>或<init-param>加载spring配置文件。但是,这里有一个区别,解释如下:
如果使用<context-param>加载,那么依赖项(spring Bean)将可用于整个应用程序(用于所有servlet)。这将用于加载整个应用程序中的公共依赖项(如服务、DAOs等)。
但是,如果您使用DispatcherServlet的<init-param>进行加载,那么依赖项(spring Bean)将只能作为DispatcherServlet的子上下文使用。因此,如果您的应用程序有多个入口点(如Restlet或Apache等),则可以为每个CXFServlet维护一个子上下文(入口点)。每个子上下文拥有/加载相应web层的特定依赖项。
您可以查看以下摘自Spring的WebApplicationContext注释:
与通用应用程序上下文一样,web应用程序上下文也是分层的。每个应用程序都有一个根上下文,而应用程序中的每个servlet (包括MVC框架中的dispatcher servlet )都有自己的子上下文。
https://stackoverflow.com/questions/40572984
复制相似问题