春天配置文件的正确位置在哪里?
问题描述:
为什么必须有<init-param>
在<servlet>
节点与<param-name>
“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>
答
什么是spring配置文件的地方?
您可以通过servlet类的<context-param>
或<init-param>
加载弹簧配置文件。但是,有如下所述的差异:
如果使用<context-param>
加载,则依赖项(spring bean)将可用于整个应用程序(对于所有servlet)。这将用于加载整个应用程序的通用依赖关系(如服务,DAO等)。
但是,如果使用<init-param>
的DispatcherServlet
进行加载,则依赖项(spring beans)仅作为子上下文可用于DispatcherServlet
。因此,如果您的应用程序有多个入口端点(如Restlet或Apache CXFServlet等),则可以为每个servlet(入口点)维护一个子上下文。每个子上下文都拥有/加载相应Web层的特定依赖关系。
你可以看一下从Spring的WebApplicationContext
API采取了以下事项:
像通用的应用程序环境中,Web应用程序上下文是 层次。每个应用程序有一个根上下文,而应用程序中的每个servlet(包括 MVC框架中的调度程序servlet)都有其自己的子上下文。