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

目录
  • 使用applicationContext.xml配置文件
  • applicationContext 加载配置文件
    • 案例
    • 多文件的加载方法

使用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"});

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • SpringBoot如何使用ApplicationContext获取bean对象

    目录 使用ApplicationContext获取bean对象 SpringBoot Bean注入的深入研究 下面代码可正常运行 下面代码不能正常运行 比较 解决方案 应用 使用ApplicationContext获取bean对象 编写一个ApplicationContextFactory工厂类 public class ApplicationContextFactory{ private static ApplicationContext applicationContext = null;

  • SpringBoot获取ApplicationContext的3种方式

    ApplicationContext是什么? 简单来说就是Spring中的容器,可以用来获取容器中的各种bean组件,注册监听事件,加载资源文件等功能. Application Context获取的几种方式 1 直接使用Autowired注入 @Component public class Book1 { @Autowired private ApplicationContext applicationContext; public void show (){ System.out.printl

  • Springboot如何获取上下文ApplicationContext

    目录 Springboot获取上下文ApplicationContext springboot的应用上下文 ServletWebServerApplicationContext 扩展的功能 AnnotationConfigServletWebServerApplicationContext Springboot获取上下文ApplicationContext 在项目中遇到了一个场景,就是通过获得上下文然后获取特定的bean.在此遇到了不小的坑,故留下这个篇文章,做个记录. import org.s

  • springboot ApplicationContextInitializer的三种使用方法小结

    目录 ApplicationContextInitializer的三种使用方法 概述 1.使用spring.factories方式 2.application.properties添加配置方式 3.直接通过add方法 ApplicationContextInitializer都干了些什么 ApplicationContextInitializer的三种使用方法 概述 ApplicationContextInitializer是在springboot启动过程(refresh方法前)调用,主要是在A

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

    目录 使用applicationContext.xml配置文件 applicationContext 加载配置文件 案例 多文件的加载方法 使用applicationContext.xml配置文件 SpringBoot默认是通过Java代码进行依赖注入,但也为xml形式的依赖注入提供了入口,就是@ImportResource注解. 我们可以在SpringBoot的启动类上添加这个注解并在注解的locations属性中指定xml配置文件.(可以使用一个文件集合也可以只引入主配置文件然后在主配置文件

  • 详解spring applicationContext.xml 配置文件

    applicationContext.xml 文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http

  • Spring中xml配置文件的基础使用方式详解

    目录 1. xml配置文件的读取 1.1 通过类路径读取配置文件 1.2 通过文件系统绝对路径读取配置文件 1.3使用BeanFactory接口读取配置文件 2.带参构造对象的创建(constructor-arg标签) 3.使用另一个类中的方法创建对象,并放到Spring容器中 4.调用另一个类中的静态方法创建对象,并放到Spring容器中 5.对象的生命周期 6.单例多例的测试 1. xml配置文件的读取 目录结构 applicationContext.xml配置文件 <?xml versio

  • Spring主配置文件(applicationContext.xml) 导入约束详解

    eclipse导入Spring配置文件约束  Windows-Preference-XML-XMLCatalog 点 Add 选File System 下spring的解压包下的schema文件夹,选beans,然后选择spring对应的版本的xsd文件 选择指定xsd文件,再Key的路径后面添加"/spring-beans-4.2.xsd"点ok 创建applicationContext.xml   写根元素 <beans></beans> Add导入XSI,

  • spring-boot读取props和yml配置文件的方法

    最近微框架spring-boot很火,笔者也跟风学习了一下,废话不多说,现给出一个读取配置文件的例子. 首先,需要在pom文件中依赖以下jar包 <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <d

  • Spring根据XML配置文件 p名称空间注入属性的实例

    要生成对象并通过名称空间注入属性的类 代码如下: package com.swift; public class User { private String userName; public void setUserName(String userName) { this.userName = userName; } public String fun() { return "User's fun is ready."+this.userName; } } XML配置文件写法如下: &

  • Spring根据XML配置文件注入属性的方法

    方法一使用setter方法 package com.swift; public class Book { private String bookName; public void setBook(String bookName) { this.bookName = bookName; } @Override public String toString() { return "Book [book=" + bookName + "]"; } } 在Spring框架中

  • spring如何实现两个xml配置文件间的互调

    这篇文章主要介绍了spring如何实现两个xml配置文件间的互调,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 首先建两个测试类 package soundsystem; public class Dog { private String Cry; private Cat Cat; public void setCry(String cry) { Cry = cry; } public void setCat(soundsystem.Cat c

  • spring是如何解析xml配置文件中的占位符

    前言 我们在配置Spring Xml配置文件的时候,可以在文件路径字符串中加入 ${} 占位符,Spring会自动帮我们解析占位符,这么神奇的操作Spring是怎么帮我们完成的呢?这篇文章我们就来一步步揭秘. 1.示例 ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(); applicationContext.setConfigLocation("${java.versi

  • springboot多模块多环境配置文件问题(动态配置生产和开发环境)

    第一种情况: spring.profiles.active=环境变量 配置两个环境的,可根据实际需要增加环境模式(开发环境dev,测试环境test,回归坏境retu,预生产环境pre,生产环境prod,等等) dev代表开发环境: prod代表生产环境 pom.xml里面配置profiles: <profiles> <profile> <id>dev</id> <activation> <!-- 默认激活--> <activeB

随机推荐