自动启动Spring容器的配置

来源:互联网 发布:气象观测数据怎么画图 编辑:程序博客网 时间:2024/05/17 23:17
<?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/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"

id="WebApp_ID" version="2.4">


<!--1.从类路径下加载Spring配置文件,classpath关键字特指类路径下加载-->       //注意:类路径表示src目录


<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>


<!--2.负责启动Spring容器的监听器,它将引用1处的上下文参数获得Spring配置文件地址-->

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


</web-app>


首先,通过Web容器上下文参数指定Spring配置文件的地址,如1所示。多个配置文件可用逗号或空格分隔,建议采用逗号分隔的方式。在2处指定Spring所提供的ContextLoaderListener的Web容器监听器,该监听器在web容器启动时自动运行,它会根据contextConfigLoacation Web容器参数获取Spring配置文件,并启动Spring容器。注意需要将log4j.properties日志配置文件放置在类路径下,以便日志引擎自动生效

0 0