spring boot导入xml配置文件
来源:互联网 发布:初级数据分析工程师 编辑:程序博客网 时间:2024/05/21 18:44
【来也匆匆,去也匆匆,在此留下您的脚印吧,转发点赞评论;
您的认可是我最大的动力,感谢您的支持】
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)新建一个工程;
我们在前几节的例子已经写到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;
importorg.springframework.stereotype.Service;
@Service
public class 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;
importorg.springframework.stereotype.Service;
@Service
public class 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/beanshttp://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;
importorg.springframework.context.annotation.Configuration;
importorg.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"})
public class ConfigClass {
}
(6)编写App.java启动类进行测试;
这个类Spring Boot正常的启动代码:
com.kfit.App:
package com.kfit;
import org.springframework.boot.SpringApplication;
importorg.springframework.boot.autoconfigure.SpringBootApplication;
/**
*
*
* 大家也许会看到有些demo使用了3个注解:@Configuration;
*
*@EnableAutoConfiguration
* @ComponentScan
*
* 其实:@SpringBootApplication申明让spring boot自动给程序进行必要的配置,
*
* 等价于以默认属性使用@Configuration,
* @EnableAutoConfiguration和@ComponentScan
*
* 所以大家不要被一些文档误导了,让自己很迷茫了,希望本文章对您有所启发;
*
* @authorAngel(QQ:412887952)
* @version v.0.1
*/
@SpringBootApplication
public class App {
public static voidmain(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导入xml配置文件
- spring boot导入xml配置
- 31. Spring Boot导入XML配置【从零开始学Spring Boot】
- 31. Spring Boot导入XML配置【从零开始学Spring Boot】
- Spring Boot 学习31--Spring Boot导入XML配置
- Spring Boot导入XML配置【从零开始学Spring Boot】
- Spring Boot 学习22--导入XML配置
- spring-boot配置文件
- Spring Boot 配置文件模板
- Spring Boot属性配置文件
- spring boot 工程配置文件
- Spring boot 配置文件
- Spring boot读取配置文件
- spring boot配置文件
- spring boot (二) 配置文件
- spring-boot 配置文件
- spring Boot 配置文件详解
- Spring Boot之配置文件
- [HAOI2012]高速公路(road)
- 可变参数函数初步分析
- git使用简记-git创建本地仓库及增删改文件操作
- shell 压缩文件/文件夹
- 中国科技公司挺进非洲获成功:身影无处不在
- spring boot导入xml配置文件
- 验证码生成器
- 如何将已经加密的PDF文件进行第二次的更改
- Newtonsoft.Json
- java 中文编码问题
- 数据中心机房运维可视化平台
- 字典树模板
- tomcat下载安装以及在eclipse中的配置
- matlab 计算机视觉工具箱step含义