SpringBoot理念就是零配置编程,但是如果绝对需要使用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)新建一个工程;

参照我前面的例子

(2)在App.java类包路径下编写TestService;

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

package me.shijunjie.service;

import org.springframework.stereotype.Service;

@Service
public class TestService { public TestService() {
System.out.println("me.shijunjie.service");
System.out.println("TestService");
} }

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

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

package com.test.service;

import org.springframework.stereotype.Service;

@Service
public class TestService2 { public TestService2() {
System.out.println("com.test.service");
System.out.println("TestService2");
} }

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

在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" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean id="testService2" class="com.test.service.TestService2"></bean>
</beans>

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

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

package me.shijunjie.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource; @Configuration
@ImportResource(locations={"classpath:application-bean.xml"})
public class ConfigClass { }

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

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

package me.shijunjie;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}

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

(7)其它说明

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

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

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

[二十一]SpringBoot 之 导入xml配置的更多相关文章

  1. 玩转Spring Boot 自定义配置、导入XML配置与外部化配置

    玩转Spring Boot 自定义配置.导入XML配置与外部化配置       在这里我会全面介绍在Spring Boot里面如何自定义配置,更改Spring Boot默认的配置,以及介绍各配置的优先 ...

  2. 2015年12月10日 spring初级知识讲解(二)最小化Spring XML配置 注解

    序,随着Spring容器管理Bean数量增加,XML文件会越来越大,而且纯手工配置XML很繁琐,Spring和JAVA都提供了一些注解方式用以简化XML配置. 目录 一.自动装配(autowiring ...

  3. 003-SpringBoot导入xml配置

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

  4. 模拟Springboot一:(零xml配置搭建SSM项目)

    在spring官网文档中无论是spring的基础文档,还是spring-mvc文档都推荐我们使用javaconfig的方式来搭建项目 间接说明 (优点:javaconfig配置>xml配置) 其 ...

  5. SpringBoot基础学习(二) SpringBoot全局配置文件及配置文件属性值注入

    全局配置文件 全局配置文件能够对一些默认配置值进行修改.SpringBoot 使用一个名为 application.properties 或者 application.yaml的文件作为全局配置文件, ...

  6. geotrellis使用(二十一)自动导入数据

    目录 前言 整体介绍 前台界面 后台控制 总结 一.前言        之前Geotrellis数据导入集群采用的是命令行的方式,即通过命令行提交spark任务来ingest数据,待数据导入完毕再启动 ...

  7. (31)Spring Boot导入XML配置【从零开始学Spring Boot】

    [来也匆匆,去也匆匆,在此留下您的脚印吧,转发点赞评论: 您的认可是我最大的动力,感谢您的支持] Spring Boot理念就是零配置编程,但是如果绝对需要使用XML的配置,我们建议您仍旧从一个@Co ...

  8. cocos2d-x的初步学习二十一之iosandroid跨平台环境配置

    这篇文章中,我们将来构建下跨平台开发的环境配置,我自己也是参考了别人了文章,折腾了几个小时,尤其是android的配置相对麻烦些.... 参考自子龙山人:http://www.cnblogs.com/ ...

  9. spring框架学习(二)使用注解代替xml配置

    注解 1.使用注解配置spring 1)开启使用注解代理配置文件 <?xml version="1.0" encoding="UTF-8"?> &l ...

随机推荐

  1. easyui -validatebox 验证框加载

    问题: easyui验证狂框有时会验证输入字符的位数,或者验证有效字符组合 解决: 使用easyui的验证框,继承验证框,指定输入框为验证框即可 $(function(){ $.extend($.fn ...

  2. CF543E Listening to Music

    题面 空间只有$64\text{MB}$!!! 题解 (据说正解是毒瘤分块套分块) 按照权值从大到小排序,对所有能够覆盖到它的区间的左端点打个标记 按照值域建一棵主席树就可以了 区间查询最大值,用$m ...

  3. MySQL授权root

    1. 改表法. 可能是你的帐号不允许从远程登陆,只能在localhost.这个时候只要在localhost的那台电脑,登入mysql后,更改 "mysql" 数据库里的 " ...

  4. cocos2dx2.0 帧动画的创建和播放过程 深入分析

    一.帧动画的创建过程帧动画的实现有四个不可或缺的类,如下:1.CCSpriteFrame:精灵帧信息.存储帧动画的每一帧的纹理基本信息. class CC_DLL CCSpriteFrame : pu ...

  5. 强化学习读书笔记 - 09 - on-policy预测的近似方法

    强化学习读书笔记 - 09 - on-policy预测的近似方法 参照 Reinforcement Learning: An Introduction, Richard S. Sutton and A ...

  6. Iterable/Iterator傻傻分不清

    区别可迭代对象和迭代器 1.判断是否可以迭代 from collections import Iterabledef fid(times): n = 0 a , b = 0,1 while n < ...

  7. Python数据结构 将列表作为栈和队列使用

    列表作为栈使用 Python列表方法使得列表作为堆栈非常容易,最后一个插入,最先取出(“后进先出”).要添加一个元素到堆栈的顶端,使用 append() .要从堆栈顶部取出一个元素,使用 pop()  ...

  8. OpenLDAP编译安装及配置

    原文发表于cu:2016-06-20 参考文档: 原理:http://seanlook.com/2015/01/15/openldap_introduction/ 官方文档: http://www.o ...

  9. XSS 注入检查点

    如果你有个论坛,一般你会很注意用户发帖的注入问题,往往这个地方不会被注入,因为开发特别照顾.原则上XSS都是用户输入的,但是许多边角还是容易忽略.枚举一些检查点. 分页 分页通用组件获取url,修改p ...

  10. centos 7.2 安装apache,mysql,php5.6

    安装Apache.PHP.Mysql.连接Mysql数据库的包: yum -y install httpd yum -y install php yum -y install php-fpm yum  ...