[二十一]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 ...
随机推荐
- OpenCV人脸识别-训练级联分类器
OpenCV中以及附带了训练好的人脸特征分类器,3.2版本的有三种: 分别是LBP,Haar,Hug 在Data目录下. 也可以训练自己的特征库,具体参照如下: 级联分类器训练 — OpenCV 2. ...
- spring 缓存机制
简介 Spring3.1开始引入了基于注释的缓存,其使用方法和原理类似于Spring对事务管理的支持.可以对容器中的任意的bean或bean的方法添加缓存. 配置Spring缓存 Spring缓存 ...
- 九、Django之ORM
一.ORM概述 用于实现面向对象编程语言里不同类型系统的数据之间的转换,换言之,就是用面向对象的方式去操作数据库的创建表以及增删改查等操作. 到目前为止,当我们的程序涉及到数据库相关操作时,一般操作流 ...
- LUA中点号和冒号的区别
Student = {}; Student.__index = Student; function Student:new(name, age) local temp = {}; setmetatab ...
- 提取验证码到winform上webbroswer和axwebbroswer
在网上只有webbroswer的代码,所以自己又修改了修改改成axwebbroswer的 public static class yanZhengMaHelp { //webbrowser验证码 pu ...
- IDEA 配置Junit4
Junit4 主要用来执行java程序的单元测试: 1 安装junit4插件 因为我安装过了,没有安装的再输入框搜索,然后安装就行 2 选择默认使用Junit4 3 红框中的test去掉,变为“$en ...
- qt linux系统下出现Qt5: Unknown module(s) in QT: serialport问题解决
需要单独安装这个模块, manjaro linux打开包管理器,搜索安装,就好了
- halcon中关于文本的创建以及写入
原文链接:http://blog.sina.com.cn/s/blog_61cc743001017nxr.html#FileName 1.open_file( : : FileName, FileTy ...
- Spark SQL、DataFrame和Dataset——转载
转载自: Spark SQL.DataFrame和Datase
- php序列化问题
序列化是将变量转换为可保存或传输的字符串的过程:反序列化就是在适当的时候把这个字符串再转化成原来的变量使用.这两个过程结合起来,可以轻松地存储和传输数据,使程序更具维护性. 1. serialize和 ...