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

 

【视频 & 交流平台】

à SpringBoot视频

http://study.163.com/course/introduction.htm?courseId=1004329008&utm_campaign=commission&utm_source=400000000155061&utm_medium=share

à SpringCloud视频

http://study.163.com/course/introduction.htm?courseId=1004638001&utm_campaign=commission&utm_source=400000000155061&utm_medium=share

à Spring Boot源码

https://gitee.com/happyangellxq520/spring-boot

à Spring Boot交流平台

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

 

 

 

 


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

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

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

publicclass HelloService2 {

      

       /**

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

        */

       publicHelloService2() {

              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

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文件:

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="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"})

publicclass 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 系列视频】

视频&交流平台:

à Spring Boot网易云课堂视频

http://study.163.com/course/introduction.htm?courseId=1004329008

à Spring Boot交流平台

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

 

网易云课堂视频最新更新

第十一章 Spring Boot 日志

1、spring boot日志—理论

2、Spring Boot日志-logback

3、Spring Boot日志-log4j2

第十二章 Spring Boot 知识点2

1、spring boot 服务配置和部署

2、Spring Boot 定制URL匹配规则

 

 

历史章节

 

第一章 快速开始

1、Spring Boot之Hello World

2、Spring Boot之Hello World访问404

 

第二章 Spring Boot之JSON

1、spring boot返回json数据

2、Spring Boot完美使用FastJson解析JSON数据

 

第三章 Spring Boot热部署

1、Spring Boot热部署(springloader)

2、springboot + devtools(热部署)

 

第四章 Spring Boot数据库

1、Spring Boot JPA/Hibernate/Spring Data概念

2、Spring Boot JPA-Hibernate

3、Spring Boot Spring Data JPA介绍

4、Spring Boot JdbcTemplate

5、Spring Boot集成MyBatis

 

第五章 web开发

1、全局异常捕捉

2、配置server信息

3、spring boot使用thymeleaf

4、Spring Boot 使用freemarker

5、Spring Boot添加JSP支持

 

第六章 定时任务

1、Spring Boot定时任务

2、Spring Boot 定时任务升级篇(动态修改cron参数)

3、Spring Boot 定时任务升级篇(动态添加修改删除定时任务)

4、Spring Boot 定时任务升级篇(集群/分布式下的定时任务说明)

5、Spring Boot Quartz介绍

6、Spring Boot Quartz在Java Project中使用

7、Spring Boot 集成Quartz普通使用

8、Spring Boot 集成Quartz升级版

9、Spring Boot 集成Quartz二次升级版

10、Spring Boot 集成Quartz-Job如何自动注入Spring容器托管的对象

 

第七章 Spring Boot MyBatis升级篇

1、Spring Boot MyBatis升级篇-注解

2、Spring Boot MyBatis升级篇-注解-自增ID

3、Spring Boot MyBatis升级篇-注解-增删改查

4、Spring Boot MyBatis升级篇-注解-分页查询

5、Spring Boot MyBatis升级篇-注解-分页PageHelper不生效

6、Spring Boot MyBatis升级篇-注解- mybatic insert异常:BindingException: Parameter 'name' not found

7、Spring Boot MyBatis升级篇-注解- #和$符号特别篇

8、Spring Boot MyBatis升级篇-注解-@Result

9、Spring Boot MyBatis升级篇-注解-动态SQL(if test)-方案一:<script>

10、Spring Boot MyBatis升级篇-注解-动态SQL(if test)-方案二:@Provider

11、Spring Boot MyBatis升级篇-注解-动态SQL-参数问题

12、Spring Boot MyBatis升级篇-注解-特别篇:@MapperScan和@Mapper

13、Spring Boot MyBatis升级篇-XML

14、Spring Boot MyBatis升级篇-XML-自增ID

15、Spring Boot MyBatis升级篇-XML-增删改查

16、Spring Boot MyBatis升级篇-XML-分页查询

17、Spring Boot MyBatis升级篇-XML-分页PageHelper不生效

18、Spring Boot MyBatis升级篇-XML-动态SQL(if test)

19、Spring Boot MyBatis升级篇-XML-注解-初尝试

20、Spring Boot MyBatis升级篇- pagehelper替换为pagehelper-spring-boot-starter

 

第八章 Spring Boot 知识点1

1、Spring Boot 拦截器HandlerInterceptor

2、Spring Boot启动加载数据CommandLineRunner

3、Spring Boot环境变量读取和属性对象的绑定

4、Spring Boot使用自定义的properties

5、Spring Boot使用自定义的properties

6、Spring Boot使用@SpringBootApplication

7、Spring Boot 监控和管理生产环境

 

第十章 Spring Boot 打包部署

1、Spring Boot打包部署((提供Linux的sh文件))

 

第十一章 Spring Boot 日志

1、spring boot日志—理论

2、Spring Boot日志-logback

 

3、Spring Boot日志-log4j2


  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悟纤

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值