SpringBoot如何使用applicationContext.xml配置文件

作者:我是你亲爱的航哥 时间:2022-11-15 08:18:53 

使用applicationContext.xml配置文件

SpringBoot默认是通过Java代码进行依赖注入,但也为xml形式的依赖注入提供了入口,就是@ImportResource注解。

我们可以在SpringBoot的启动类上添加这个注解并在注解的locations属性中指定xml配置文件。(可以使用一个文件集合也可以只引入主配置文件然后在主配置文件中使用标签引入其他子配置文件,个人更喜欢第二中方式)。

这样容器在启动时配置在xml文件中的BeanDefination也可以被解析。 

applicationContext 加载配置文件

ApplicationContext 理解为spring容器的上下文,通过上下文操作容器中bean.

  • ClassPathXmlApplicationContext:加载classpath下的配置文件创建一个容器实例

  • FileSystemXmlApplicationContext: 加载文件系统中任意目录下的配置文件,创建一个容器实例

案例

/*方式一 :ClassPathXmlApplicationContext*/
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("spring.xml");
/*方式二 FileSystemXmlApplicationContext */
        //FileSystemXmlApplicationContext ioc= new FileSystemXmlApplicationContext("E://1804_2//20180827spring//config//spring.xml");
        User u = (User) ioc.getBean("user1");
        System.out.println(u);

多文件的加载方法

/*方式一*/
//ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("spring.xml,spring-mvc.xml");
/*方式二*/
//ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext(new String[]{"spring.xml,spring-mvc.xml"});
/*方式三*/
//ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("spring-*.xml");
/*方式四*/
//ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext(new String []{"classpath:spring-*.xml","mybatis.xml"});
/*方式五*/
//ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("classpath:*.xml");
/*方式六*/
//ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("classpath*:*.xml");
/*方式七*/
//ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext(new String []{"classpath:*.xml","classpath:springmvc/beans.xml"});

来源:https://blog.csdn.net/qq_28807077/article/details/105100249

标签:SpringBoot,applicationContext.xml,配置文件
0
投稿

猜你喜欢

  • 代码分析Android消息机制

    2023-07-26 09:44:44
  • Java Main 函数启动不退出的解决方案

    2022-03-24 14:25:03
  • SpringBoot整合Kafka工具类的详细代码

    2022-07-03 14:12:13
  • 浅谈Java线程并发知识点

    2021-10-20 13:11:13
  • SpringMVC中的Model对象用法说明

    2022-12-15 02:24:34
  • Android 中使用EditText 点击全选再次点击取消全选功能

    2023-09-08 00:08:44
  • Maven继承与聚合详解及作用介绍

    2023-03-08 00:14:36
  • Android自定义可标记日历效果

    2022-09-06 04:11:22
  • 详解C# Socket异步通信实例

    2022-08-27 14:27:22
  • Java全排列算法字典序下的下一个排列讲解

    2023-07-30 17:44:39
  • Android 回调详解及简单实例

    2023-03-06 10:49:35
  • Spring @Conditional通过条件控制bean注册过程

    2023-08-06 10:00:11
  • Android编程基于距离传感器控制手机屏幕熄灭的方法详解

    2022-02-20 10:49:05
  • Android 使用Picasso加载网络图片等比例缩放的实现方法

    2023-08-29 15:36:32
  • Java中list.contains()的用法及拓展

    2022-11-06 21:04:59
  • C++实现图书管理系统

    2023-11-03 03:00:35
  • Android中实现图文并茂的按钮实例代码

    2022-10-18 22:58:49
  • Java NIO实例UDP发送接收数据代码分享

    2022-12-03 01:30:37
  • Android 代码写控件代替XML简单实例

    2023-04-11 00:10:48
  • Android自定义Toast之WindowManager

    2022-10-27 09:13:10
  • asp之家 软件编程 m.aspxhome.com