【来也匆匆,去也匆匆,在此留下您的脚印吧,转发点赞评论;

您的认可是我最大的动力,感谢您的支持】

Spring Boot理念就是零配置编程,但是如果绝对需要使用XML的配置,我们建议您仍旧从一个@Configuration类开始,你可以使用@ImportResouce注解加载XML配置文件,我拿一个例子来进行讲解:

这个例子的大体步骤如下:

(1)新建一个工程;

(2)在App.java类编写HelloService2;

(3)在App.java类无法扫描的包下编写HelloService;

(4)编写application-bean.xml注入HelloService;

(5)编写ConfigClass注入配置文件application-bean.xml;

(6)编写App.java启动类进行测试;

(7)其它说明

(1)新建一个工程;

我们在前几节的例子已经写到hello2了,我们取一个新的名称为spring-boot-hello3,这里没有什么难点,不过多介绍,还有难处的可以查看之前的例子,当然这里加入spring-boot相应的web支持;

不懂的参考:

spring boot起步之Hello World【从零开始学Spring Boot】:

http://412887952-qq-com.iteye.com/blog/2291500

(2)在App.java类编写HelloService2;

首先我们这里有几个包:com.kfit,org.kfit,我们这里打算把App.java启动类放到com.kfit中,根据Spring Boot扫描(根包到子包的原则),我们把HelloService2写在Spring Boot可以扫描的位置,HelloService写在Spring Boot无法扫描到的位置,那么我们使用配置文件bean的方式进行引入,具体代码如下:

com.kfit.service.HelloService2:

package com.kfit.service;

import org.springframework.stereotype.Service;

@Service

publicclass HelloService2 {

/**

* 启动的时候观察控制台是否打印此信息;

*/

public HelloService2() {

System.out.println("HelloService2.HelloService2()");

System.out.println("HelloService2.HelloService2()");

System.out.println("HelloService2.HelloService2()");

}

}

(3)在App.java类无法扫描的包下编写HelloService;

注意这个类是写在Spring Boot无法自动扫描的位置,正常启动之后,如果引入HelloService的话肯定会报异常的,因为它根本没有被注入成功,具体代码如下:

org.kfit.service.HelloService:

package org.kfit.service;

import org.springframework.stereotype.Service;

@Service

publicclass HelloService {

/**

* 启动的时候观察控制台是否打印此信息;

*/

public HelloService() {

System.out.println("HelloService.HelloService()");

System.out.println("org.kfit.service.HelloService.HelloService()");

System.out.println("HelloService.HelloService()");

}

}

(4)编写application-bean.xml注入HelloService;

在src/main/resouces下编写配置文件application-bean.xml文件:

<?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">

<!-- 注入spring boot无法扫描到的bean. -->

<bean id="helloService" class="org.kfit.service.HelloService"></bean>

</beans>

(5)编写ConfigClass注入配置文件application-bean.xml;

在com.kfit.config包下编写类ConfigClass,这个确保能被Spring Boot可以扫描到,不然一切都付之东流了,具体代码如下:

com.kfit.config.ConfigClass:

package com.kfit.config;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.ImportResource;

/**

* classpath路径:locations={"classpath:application-bean1.xml","classpath:application-bean2.xml"}

* file路径: locations = {"file:d:/test/application-bean1.xml"};

*/

@Configuration

@ImportResource(locations={"classpath:application-bean.xml"})

//@ImportResource(locations={"file:d:/test/application-bean1.xml"})

publicclass ConfigClass {

}

(6)编写App.java启动类进行测试;

这个类Spring Boot正常的启动代码:

com.kfit.App:

package com.kfit;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

/**

*

*

* 大家也许会看到有些demo使用了3个注解: @Configuration;

*

* @EnableAutoConfiguration

* @ComponentScan

*

*   其实:@SpringBootApplication申明让spring boot自动给程序进行必要的配置,

*

* 等价于以默认属性使用@Configuration,

*  @EnableAutoConfiguration和@ComponentScan

*

* 所以大家不要被一些文档误导了,让自己很迷茫了,希望本文章对您有所启发;

*

* @author Angel(QQ:412887952)

* @version v.0.1

*/

@SpringBootApplication

public class App {

public static void main(String[] args) {

SpringApplication.run(App.class, args);

}

}

在App.java 右键 Run As  Java Application观察控制台输出可以看到:

HelloService2.HelloService2()

HelloService2.HelloService2()

HelloService2.HelloService2()

HelloService.HelloService()

org.kfit.service.HelloService.HelloService()

HelloService.HelloService()

说明我们引入编写的代码生效了,如果你不相信的话,可以把ConfigClass的注解去掉,测试下,是不是打印信息就少了HelloService的部分,是的话就对了。

(7)其它说明

ImportResouce有两种常用的引入方式:classpath和file,具体查看如下的例子:

classpath路径:locations={"classpath:application-bean1.xml",

"classpath:application-bean2.xml"

}

file路径:

locations = {"file:d:/test/application-bean1.xml"};

Spring Boot 系列博客】

)前言【从零开始学Spring Boot】 :

http://412887952-qq-com.iteye.com/blog/2291496

)spring boot起步之Hello World【从零开始学Spring Boot】:

http://412887952-qq-com.iteye.com/blog/2291500

)Spring Boot返回json数据【从零开始学Spring Boot】

http://412887952-qq-com.iteye.com/blog/2291508

(15)Spring Boot使用Druid和监控配置【从零开始学Spring Boot】

http://412887952-qq-com.iteye.com/blog/2292362

)Spring Boot使用Druid(编程注入)【从零开始学Spring Boot】

http://412887952-qq-com.iteye.com/blogs/2292376

)Spring Boot普通类调用bean【从零开始学Spring Boot】:

http://412887952-qq-com.iteye.com/blog/2292388

更多查看博客:http://412887952-qq-com.iteye.com/blog

(31)Spring Boot导入XML配置【从零开始学Spring Boot】的更多相关文章

  1. 59. Spring Boot Validator校验【从零开始学Spring Boot】

    大纲: (1) 入门例子: (2) 国际化: (3) 在代码中添加错误信息: (1) 入门例子: Validator主要是校验用户提交的数据的合理性的,比如是否为空了,密码长度是否大于6位,是否是纯数 ...

  2. (6)Spring Boot datasource - mysql【从零开始学Spring Boot】

    在任何一个平台都逃离不了数据库的操作,那么在spring boot中怎么接入数据库呢? 很简单,我们需要在application.properties进行配置一下,application.proper ...

  3. (3)Spring Boot热部署【从零开始学Spring Boot】

    在编写代码的时候,你会发现我们只是简单把打印信息改变了下,就需要重新部署,如果是这样的编码方式,那么我们估计一天下来之后就真的是打几个Hello World之后就下班了.那么如何解决热部署的问题呢?那 ...

  4. 72.spring boot讨论群【从零开始学Spring Boot】

    [从零开始学习Spirng Boot-常见异常汇总] 如果您碰到什么问题,您可以加群进行探讨,在群里有加入的都是Spring Boot志同道合的朋友: Spring Boot QQ交流群:193341 ...

  5. (42)Spring Boot多数据源【从零开始学Spring Boot】

    我们在开发过程中可能需要用到多个数据源,我们有一个项目(MySQL)就是和别的项目(SQL Server)混合使用了.其中SQL Server是别的公司开发的,有些基本数据需要从他们平台进行调取,那么 ...

  6. (27)Spring Boot Junit单元测试【从零开始学Spring Boot】

    Junit这种老技术,现在又拿出来说,不为别的,某种程度上来说,更是为了要说明它在项目中的重要性. 那么先简单说一下为什么要写测试用例 1. 可以避免测试点的遗漏,为了更好的进行测试,可以提高测试效率 ...

  7. 84. Spring Boot集成MongoDB【从零开始学Spring Boot】

    至于MongoDB网上有很多相关的资料,所以在这里不进行过多的介绍,我们在这里主要是介绍下如何将mongodb与spring boot结合使用.本节大纲: (1) 准备工作: (2) 新建一个mave ...

  8. 86. Spring Boot集成ActiveMQ【从零开始学Spring Boot】

    在Spring Boot中集成ActiveMQ相对还是比较简单的,都不需要安装什么服务,默认使用内存的activeMQ,当然配合ActiveMQ Server会更好.在这里我们简单介绍怎么使用,本节主 ...

  9. 85. Spring Boot集成RabbitMQ【从零开始学Spring Boot】

    这一节我们介绍下Spring Boot整合RabbitMQ,对于RabbitMQ这里不过多的介绍,大家可以参考网络上的资源进行安装配置,本节重点是告诉大家如何在Spring Boot中使用Rabbit ...

随机推荐

  1. 卸载了 TortoiseGit,问题太多

    电脑里面同一时候安装TortoiseGit 和 TortoiseSVN,使用tortoiseGIT来跟踪git项目,有一个非常无语的问题,git status显示都是clean的.可是目录图标却始终显 ...

  2. 【Ubuntu】基本操作 (条目=11)

    定义 NAME 为要操作的对象名 定义 DIR 为文件所在的绝对路径 所有操作默认在普通用户下进行 所有软件包默认是指Debian包(deb包) 1.查看进程 top 2.强制结束进程 PID由top ...

  3. linux下获取按键响应事件【转】

    本文转载自:https://my.oschina.net/u/157503/blog/91548 1.问题 通过一个死循环将读取键盘对应的设备文件将触发键盘事件在屏幕上打印出来,按esc退出程序 代码 ...

  4. golang文件读写三种方式——bufio,ioutil和os.create

    package main import ( "bufio" "fmt" "io/ioutil" "os" ) func ...

  5. poj 3662 Telephone Lines(最短路+二分)

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6973   Accepted: 2554 D ...

  6. hdoj--1220--Cube(数学推导)

    Cube Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  7. hihocoder 1677 翻转字符串 splay

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个字符串S,小Hi希望对S进行K次翻转操作. 每次翻转小Hi会指定两个整数Li和Ri,表示要将S[Li..Ri]进行 ...

  8. LA3276

    费用流 这种棋盘模型大概都是网络流吧 首先我们知道棋子之间不会影响到达目标的步数,那么就好做了,枚举终点,然后就是最小权匹配了,因为就是寻找总和最小,然后费用流就行了. #include<bit ...

  9. 洛谷P1894 [USACO4.2]完美的牛栏The Perfect Stall(二分图)

    P1894 [USACO4.2]完美的牛栏The Perfect Stall 题目描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星 ...

  10. Kubernetes+Jenkins+Nexus+Gitlab进行CI/CD集成

    前面已经完成了 二进制部署Kubernetes集群,下面进行CI/CD集成. 一.流程说明 应用构建和发布流程说明: 1.用户向Gitlab提交代码,代码中必须包含Dockerfile: 2.将代码提 ...