spring web.xml applicationContext.xml常见配置

来源:互联网 发布:魔盒cms微信营销平台 编辑:程序博客网 时间:2024/05/18 03:28

web.xml可以说是web项目的入口,spring struts2,springMVC,包括一些日志框架log4j,权限管理框架shiro等都是通过web.xml作为入口的。

先看一个web.xml的例子

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"><display-name>fundamental</display-name><context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:applicationContext.xml</param-value></context-param><context-param><param-name>webAppRootKey</param-name><param-value>WORKDIR</param-value></context-param><context-param><param-name>log4jConfigLocation</param-name><param-value>classpath:log4j.properties</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><listener><listener-class>org.springframework.web.util.Log4jConfigListener</listener-class></listener><filter><filter-name>encodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param></filter><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping><welcome-file-list><welcome-file>Loader.jsp</welcome-file></welcome-file-list></web-app>
解析:

<context-param> + <listener> -> <filter> -> <servlet>

applicationContext.xml是spring管理bean的配置文件,例如datasource的配置,controller的配置,service的配置等。

在appicationContext.xml中可以引入*.properties进行配置,当然,properties是以key-value的形式存储的,引入的配置文件写法,如下:

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">        <property name="locations">            <list>                <value>classpath:config.properties</value>            </list>        </property>    </bean>
用的时候,通过$符号进行引用。例如:${key}就能拿到properties中的value值。


0 0
原创粉丝点击