SpringBoot中加载XML配置
开篇
在SpringBoot中我们通常都是基于注解来开发的,实话说其实这个功能比较鸡肋,但是,SpringBoot中还是能做到的。所以用不用是一回事,会不会又是另外一回事。
涛锅锅在个人能力能掌握的范围之内,一般是会得越多越好,都是细小的积累,发生质的改变,所以今天和小伙伴们一起分享一下。
实践
1.首先我们新建一个SpringBoot Project ,工程名为 xml

2.添加web依赖,点击Finish完成构建

3.我们新建一个类 SayHello 不做任何配置
package org.taoguoguo;
/**
* @author powersi
* @description SayHello
* @website https://www.cnblogs.com/doondo
* @create 2020-09-02 13:23
*/
public class SayHello {
public String sayHello(){
return "hello xml";
}
}
4.然后在项目的resources目录下,新建一个bean.xml,配置 Say Hello 的实体Bean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="sayHello" class="org.taoguoguo.SayHello" />
</beans>
5.在工程中创建WebMvcConfig,并声明为一个配置类,通过配置类加载 xml 配置文件
package org.taoguoguo;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
/**
* @author powersi
* @description taoguoguo
* @website https://www.cnblogs.com/doondo
* @create 2020-09-02 13:25
*/
@ImportResource(locations = "classpath:bean.xml")
@Configuration
public class WebMvcConfig {
}
6.单元测试
package org.taoguoguo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class XmlApplicationTests {
@Autowired
SayHello sayHello;
@Test
void contextLoads() {
System.out.println(sayHello.sayHello());
}
}
运行测试方法 成功读取到xml中的配置Bean

解读
当我们实践完以后我们看一下 ImportResource 这个注解,实质上里面是一个BeanDefinitionReader的接口,而在Spring中这个接口的作用就是读取xml

总结
另外@ImportResource 这个注解实质上是在包spring-context中的,所以即使项目不是SpringBoot也能使用,当我们使用Java纯配置SSM时,同理可用
好了,今天的分享就到这,喜欢的朋友给个支持点个赞,也可通过关注或留言联系涛锅锅,原创不易,谢谢大家!
SpringBoot中加载XML配置的更多相关文章
- Spring中加载xml配置文件的六种方式
Spring中加载xml配置文件的六种方式 博客分类: Spring&EJB XMLSpringWebBeanBlog 因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装 ...
- Web项目中加载Spring配置的常用方法
1.web.xml中添加配置 <web-app> <context-param> <param-name>contextConfigLoc ...
- java中加载xml文件方法
this.getclass().getclassloader().getresourceasstream(String file); 可以加载文件,比如xml.
- jquery IE中加载xml
$.ajax({ url: 'xml/myXML.xml', dataType: ($.browser.msie) ? "text" : "xml", time ...
- Spring中加载xml配置文件的常用的几种方式
https://blog.csdn.net/qq877507054/article/details/62432062
- Spring中加载配置文件的方式
原文:http://blog.csdn.net/snowjlz/article/details/8158560 Spring 中加载XML配置文件的方式,好像有3种, XML是最常见的Spring 应 ...
- spring加载xml的六种方式
因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装入系统,这就需要利用Spring去动态加载某一位置下的配置文件,所以就总结了下Spring中加载xml配置文件的方式,我总结的有6 ...
- web.xml的加载过程配置详解
一:web.xml加载过程 简单说一下,web.xml的加载过程.当我们启动一个WEB项目容器时,容器包括(JBoss,Tomcat等).首先会去读取web.xml配置文件里的配置,当这一步骤没有 ...
- Spring中加载ApplicationContext.xml文件的方式
Spring中加载ApplicationContext.xml文件的方式 原文:http://blog.csdn.net/snowjlz/article/details/8158560 1.利用Cla ...
随机推荐
- 精讲RestTemplate第1篇-在Spring或非Spring环境下如何使用
一.什么是 RestTemplate? RestTemplate是执行HTTP请求的同步阻塞式的客户端,它在HTTP客户端库(例如JDK HttpURLConnection,Apache HttpCo ...
- ElasticSearch实战系列六: Logstash快速入门和实战
前言 本文主要介绍的是ELK日志系统中的Logstash快速入门和实战 ELK介绍 ELK是三个开源软件的缩写,分别表示:Elasticsearch , Logstash, Kibana , 它们都是 ...
- 正确的使用HttpClient
快捷的网络请求,多用HttpClient 但是常规的写法会一大片的TIME_OUT 比如这样的例子 static async Task<string> TestHttpClient(str ...
- Python Selenium 搭建Web UI自动化
Python搭建UI自动化环境 下载Python3 Python官网 PyCharm 环境配置 安装Python 勾选Add Python to PATH,一直下一步. 验证:CMD输入Python ...
- Python分析「我们为什么这么穷」
- 微信小程序多列选择器
wxml <picker mode="multiSelector" bindchange="bindMultiPickerChange2" bindcol ...
- Django中的ORM如何通过数据库中的表格信息自动化生成Model 模型类?
Inspectdb Django项目通过绑定的数据库中的相应表格直接自动化生成Model 模型类 python manage.py inspectdb Django 中的 ORM 可以实现对象关系映射 ...
- JS学习第三天
运算符: 赋值运算符 “=” ,将“=”右边的值赋值给左边 比较运算符 “> < >= <= == != ===”, ==比较两边内容是否一致 ...
- 2020-05-08:mycat部署数据库集群的时候 遇到了哪些坑
福哥答案2020-05-08:答案仅供参考,来自群员 使用activity时,连接mycat设置进去的序列化的流程变量,反序列化会报错这个类型字段类型是blob类型,mycat对这种类型处理时有点问题
- RNN以及LSTM简介
转载地址 https://blog.csdn.net/zhaojc1995/article/details/80572098 本文部分参考和摘录了以下文章,在此由衷感谢以下作者的分享! https:/ ...