Spring MVC configuration file loading

Source: Internet
Author: User

There are two contexts in spring-"application context and Web application context", which correspond to Contextloaderlistener and Servletdispatcher, respectively, And can be used to configure Bean injection, assembly, and AOP.

The following information refers to the following blog post

Http://simone-folino.blogspot.com/2012/05/dispatcherservlet-vs.html

http://syntx.co/languages-frameworks/difference-between-loading-context-via-dispatcherservlet-and-contextloaderlistener/

Http://www.cnblogs.com/zemliu/p/3201112.html

1. Contextloaderlistener

Contextloaderlistener reads the configuration parameters by reading the contextconfiglocation parameter, in general it configures the middle tier of the spring project, the injection of service layer components, assembly, and AOP.

The automatic assembly mechanism corresponding to spring <context:component-scan> is the assembly of the following types of annotations

    • DAO: such as @Repository bean
    • Entity: such as @Entity bean
    • Service: such as @Service bean

(The above is from the quote post)

Here is a code snippet for loading Contextloaderlistener in a typical web. xml

    <!--Defining parameters-<Context-param><Param-name>contextconfiglocation</Param-name><Param-value>classpath*:spring/config/applicationcontext.xml</param-value> </context-param > <!-- definition Listener --> <listener< Span style= "color: #0000ff;" >> <listener-class >org.springframework.web.context.contextloaderlistener </listener-class > </listener                

and Applicationcontext.xml

<?XML version= "1.0" encoding= "UTF-8" standalone= "no"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:jee= "Http://www.springframework.org/schema/jee"Xmlns:tx= "Http://www.springframework.org/schema/tx"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:jaxrs= "Http://cxf.apache.org/jaxrs"Xmlns:cxf= "Http://cxf.apache.org/core"Xsi:schemalocation= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-3.0.xsd/http Cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd Http://cxf.apache.org/core http://cxf.apache.org/ Schemas/core.xsd Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-3.0.xsd Http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/ Spring-jee-3.0.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-3.0.xsd "><Context:annotation-config/><Tx:annotation-drivenProxy-target-class= "true"Transaction-manager= "TransactionManager"/> <context: Component-scan base-package= " Com.simonefolinoblogspot.dao "> <context:exclude-filter type Expression= "Org.springframework.stereotype.Controller" /> </context: Component-scan> ... </beans>  

You can see that the Contextloaderlistener configuration files are middle-tier, data-tier, DAO-like

2. Servletdispatcher

Servletdispatcher reads the configuration by reading the <servlet-name>-servlet.xml file, and if the file does not exist, it can be passed through the servlet tag

        <init-param>            <param-name>contextconfiglocation</param-name  <param-value>classpath*:spring/config/dispatcher-servlet.xml</param-value  </init-param>              

To specify the configuration file, which configures the injection, assembly, and AOP of Web tier components

    • Controllers
    • Viewresolvers
    • Localeresolvers
    • Themeresolvers

The following is a typical configuration of Web. xml

<Servlet><Servlet-name>addressbook</Servlet-name><Servlet-class>org.springframework.web.servlet.dispatcherservlet</Servlet-class><Init-param><Param-name>contextconfiglocation</param-name> Span style= "color: #0000ff;" ><param-value>/web-inf/spring/spring-mvc.xml </param-value> </init-param> Span style= "color: #0000ff;" ><load-on-startup>1 </load-on-startup ></servlet>  

Spring-mvc.xml

<?XML version= "1.0" encoding= "UTF-8" standalone= "no"?><BeansXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://www.springframework.org/schema/beans"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:tx= "Http://www.springframework.org/schema/tx"Xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http ://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http ://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd/http WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd "><Context:annotation-config/><Context:component-scanBase-package= "Com.simonefolinoblogspot.controller"><Context:include-filterType= "Annotation"Expression= "Org.springframework.stereotype.Controller"/></Context:component-scan><BeanId= "Viewresolver"Class= "Org.springframework.web.servlet.view.InternalResourceViewResolver"><PropertyName= "Viewclass"Value= "Org.springframework.web.servlet.view.JstlView" / > <property name = "prefix"  Value= "/ web-inf/pages/"/> < property name= "suffix " Value=". jsp "/>" Span style= "color: #0000ff;" ></bean> </< Span style= "color: #800000;" >beans>            

In my case, I added the @controller auto-registration comment to the Conroller class and introduced the AOP configuration into the Applicationcontext.xml, which was read by Contextloaderlistener.

    <resource/> 

As a result, the AOP method is not executed, and the workaround is to put Aopconfig.xml into the servletdispatcher configuration XML file.

And this manager has a default dispacherservlet referenced in the configuration file under Webapp/web-inf rest-servlet.xml

Spring MVC configuration file loading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.