导航:首页 > 源码编译 > springboot加载配置文件源码

springboot加载配置文件源码

发布时间:2024-11-13 12:18:55

‘壹’ Springboot自定义xml文件解析

有时候,要通过自定义XML配置文件来实现一些特定的功能。这里通过例子来说明。

首先,看部分spring加载bean文件的源码
spring-beans-5.0.6.RELEASE.jar!/org/springframework/beans/factory/xml/PluggableSchemaResolver.class :

spring-beans-5.0.6.RELEASE.jar!/org/springframework/beans/factory/xml/.class :

可以看出,spring在加载xml文件的时候,会默认读取配置文件 META-INF/spring.schemas 和 META-INF/spring.handlers 。这样,我们就可以在这两个文件添加我们自定义的xml文件格式和xml文件解析处理器。

新建一个Springboot工程,pom如下。
SelfDefineXmlTrial/pom.xml :

然后,新建一个用于测试controller。
com.lfqy.springboot.selfdefxml.controller.SelfDefXmlController :

最后,创建一个Springboot的启动类。
com.lfqy.springboot.selfdefxml.SelfDefXmlApplication :

运行启动之后,浏览器访问 http://localhost:8080/selfdefxml/hello 效果如下:

修改前面提到的配置文件 META-INF/spring.schemas 、 META-INF/spring.handlers ,添加xml格式说明。
META-INF/spring.schemas :

META-INF/spring.handlers :

添加xml格式说明配置文件。
META-INF/selfdef.xsd :

添加自定义xml格式处理器类。
com.lfqy.springboot.selfdefxml.selxmlparse.UserNamespaceHandler :

新增xml格式解析类。
com.lfqy.springboot.selfdefxml.selxmlparse.UserBeanDefinitionParser :

新增自定义xml对应的bean类。
com.lfqy.springboot.selfdefxml.beans.User :

添加自定义xml配置文件读取的相关逻辑。
com.lfqy.springboot.selfdefxml.SelfDefXmlApplication :

到这里,编码就完成了,工程的目录结构如下。

运行之后,控制台输出如下:

这里,通过实现一个启动时自动初始化的一个servlet来实现。

com.lfqy.springboot.selfdefxml.servlet.StartupServlet :

在启动时加载servlet,为了方便区分,这里新写一个启动类。
com.lfqy.springboot.selfdefxml.

到这里,编码已经完成,工程的目录结构如下:

运行之后,控制台输出如下:

阅读全文

与springboot加载配置文件源码相关的资料

热点内容
免费阅读程序员那么可爱 浏览:980
外网哄睡助眠解压 浏览:818
如何反编译amxx 浏览:735
rsa加密太耗时 浏览:237
数控车ug编程步骤 浏览:6
如何精准推广app 浏览:175
高中数学教材pdf人教版 浏览:152
什么app能看明星演唱会 浏览:493
ug刀柄放到哪个文件夹 浏览:322
eclipse编写php 浏览:673
php抓取网页内容的代码 浏览:868
什么是萌鸭app 浏览:861
变量的数字如何变化python 浏览:794
整数压缩 浏览:993
最优停止策略问题算法 浏览:716
pdf图片背景 浏览:766
app的图标有什么风格 浏览:28
python代码运行编译器 浏览:936
魔鬼训练程序员 浏览:686
php上传大文件失败 浏览:602