web.xml - Where is the right place for spring configuration files? -


why there must <init-param> in <servlet> node <param-name> "contextconfiglocation" ? because if comment <init-param> group project not load?

<?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 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> 

what right place spring configuration files ?

you can load spring configuration files either through <context-param> or <init-param> of servlet class. but, there difference explained below:

if loading using <context-param>, dependencies (spring beans) available whole application (for servlets). used load common dependencies across whole application (like services, daos, etc..).

but, if loading using <init-param> of dispatcherservlet, dependencies (spring beans) available dispatcherservlet child context. so, if have multiple entry end points application (like restlet or apache cxfservlet etc..), can maintain child contexts 1 per each servlet (entry point). each child context owns/loads specific dependencies respective web tier.

you can @ below notes taken spring's webapplicationcontext api:

like generic application contexts, web application contexts hierarchical. there single root context per application, while each servlet in application (including dispatcher servlet in mvc framework) has own child context.

you can here , here


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -