web-fragment使用
文章目录
一、简介
servlet3.0引入了web fragment,它可以在jar包中引入web配置。这里对其进行介绍。
二、web-fragment
2.1 作用
web fragment可以在jar包中,定义web-fragment.xml配置文件,内可配置servlet、filter、listener等组件。
2.2 web-fragment.xml定义
web-fragment.xml配置文件(内可配置servlet、filter、listener等组件)定义示例如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-fragment 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-fragment_3_0.xsd"
version="3.0">
<name>fragment_1</name>
</web-fragment>
2.3 web-fragment.xml存放位置
web-fragment.xml需要放在jar包文件内的META-INF目录下,如在intellij idea中开发,放在 项目/resources/META-INF/目录下。
2.4 web项目配置读取顺序
web项目中引入含有web fragment的jar包,那么加载顺序为:先是web.xml,再是web-fragment.xml。
当web项目引入多个含有web fragment的jar包时,也可以自定义各个jar包间的web-fragment.xml加载顺序。方式有两种,绝对顺序和相对顺序。
2.4.1 绝对顺序
在web.xml中使用标签absolute-ordering进行绝对顺序配置,示例如下:
<?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_3_0.xsd"
id="WebApp_ID" version