[二十一]SpringBoot 之 导入xml配置
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配置的更多相关文章
- 玩转Spring Boot 自定义配置、导入XML配置与外部化配置
玩转Spring Boot 自定义配置.导入XML配置与外部化配置 在这里我会全面介绍在Spring Boot里面如何自定义配置,更改Spring Boot默认的配置,以及介绍各配置的优先 ...
- 2015年12月10日 spring初级知识讲解(二)最小化Spring XML配置 注解
序,随着Spring容器管理Bean数量增加,XML文件会越来越大,而且纯手工配置XML很繁琐,Spring和JAVA都提供了一些注解方式用以简化XML配置. 目录 一.自动装配(autowiring ...
- 003-SpringBoot导入xml配置
SpringBoot理念就是零配置编程,但是如果绝对需要使用XML的配置,我们建议您仍旧从一个@Configuration类开始,你可以使用@ImportResouce注解加载XML配置文件,我拿一个 ...
- 模拟Springboot一:(零xml配置搭建SSM项目)
在spring官网文档中无论是spring的基础文档,还是spring-mvc文档都推荐我们使用javaconfig的方式来搭建项目 间接说明 (优点:javaconfig配置>xml配置) 其 ...
- SpringBoot基础学习(二) SpringBoot全局配置文件及配置文件属性值注入
全局配置文件 全局配置文件能够对一些默认配置值进行修改.SpringBoot 使用一个名为 application.properties 或者 application.yaml的文件作为全局配置文件, ...
- geotrellis使用(二十一)自动导入数据
目录 前言 整体介绍 前台界面 后台控制 总结 一.前言 之前Geotrellis数据导入集群采用的是命令行的方式,即通过命令行提交spark任务来ingest数据,待数据导入完毕再启动 ...
- (31)Spring Boot导入XML配置【从零开始学Spring Boot】
[来也匆匆,去也匆匆,在此留下您的脚印吧,转发点赞评论: 您的认可是我最大的动力,感谢您的支持] Spring Boot理念就是零配置编程,但是如果绝对需要使用XML的配置,我们建议您仍旧从一个@Co ...
- cocos2d-x的初步学习二十一之iosandroid跨平台环境配置
这篇文章中,我们将来构建下跨平台开发的环境配置,我自己也是参考了别人了文章,折腾了几个小时,尤其是android的配置相对麻烦些.... 参考自子龙山人:http://www.cnblogs.com/ ...
- spring框架学习(二)使用注解代替xml配置
注解 1.使用注解配置spring 1)开启使用注解代理配置文件 <?xml version="1.0" encoding="UTF-8"?> &l ...
随机推荐
- pager-taglib分页注意事项
必须先导包,尤其是 jsp 这种工具类和标签库的
- avaweb(三十二)——JDBC学习入门
一.JDBC相关概念介绍 1.1.数据库驱动 这里的驱动的概念和平时听到的那种驱动的概念是一样的,比如平时购买的声卡,网卡直接插到计算机上面是不能用的,必须要安装相应的驱动程序之后才能够使用声卡和网卡 ...
- 基于Cocos2d-x-1.0.1的飞机大战游戏开发实例(下)
在飞机大战游戏开发中遇到的问题和解决方法: 1.在添加菜单时,我要添加一个有背景的菜单,需要在菜单pMenu中添加一个图片精灵,结果编译过了但是运行出错,如下图: 查了很多资料,调试了很长时间,整个人 ...
- linux下汇编语言开发总结
汇编语言是直接对应系统指令集的低级语言,在语言越来越抽象的今天,汇编语言并不像高级语言那样使用广泛,仅仅在驱动程序,嵌入式系统等对性能要求苛刻的领域才能见到它们的身影.但是这并不表示汇编语言就已经没有 ...
- ubuntu apt-xxx
1. apt-get install xxx 2. dpkg -l ; list software already installed. 3. apt-cache dumpavail ; print ...
- Java泛型理解
Java泛型提供了编译时类型安全检测机制,该机制允许程序员在编译时检测到非法的类型.当需要使用某一种算法时,又无法具体算法的数据类型,或者想指定类型值的上限或下限,那么这时就需要Java泛型来大显身手 ...
- POJ-3122(二分算法)
//题意:这是一个分蛋糕的游戏, t个测试数据,输入n, f n代表的是n块蛋糕,蛋糕的高为1, f代表的是f个人朋友,然后输入每份蛋糕的半径 // 将n块蛋糕分成 f+1 份 每一份都是完成的一块蛋 ...
- TCP/IP三次握手四次挥手分析
流程图 全部11种状态 客户端独有的:(1)SYN_SENT (2)FIN_WAIT1 (3)FIN_WAIT2 (4)CLOSING (5)TIME_WAIT 服务器独有的:(1)LISTEN (2 ...
- RyuBook1.0案例一:Switching Hub项目源码分析
开发目标 实现一个带MAC地址学习功能的二层交换机 Openflow交换机与Openflow控制器安全通道建立步骤 switch and controller建立未加密TCP连接或者加密的TLS连接 ...
- Python基础知识-05-数据类型总结字典
python其他知识目录 1.一道题,选择商品的序号.程序员和用户各自面对的序号起始值 如有变量 googs = ['汽车','飞机','火箭'] 提示用户可供选择的商品: 0,汽车1,飞机2,火箭用 ...