springboot 使用传统xml配置,多数据源配置

本文介绍了如何在SpringBoot项目中使用传统XML配置实现多数据源。通过@ImportResource注解引入XML配置文件,包括spring-extend.xml和spring-hibernate-lis.xml,配合application.properties中的配置,实现数据库的灵活切换。文中还提到了TestBeanLis.java和TestController.java作为测试和业务逻辑的入口。
摘要由CSDN通过智能技术生成

1、什么是springboot

SpringBoot,就是一个javaweb的开发框架,和SpringMVC类似,对比其他javaweb框架的好处,官方说是简化开发,约定大于配置, you can “just run”,能迅速的开发web应用,几行代码开发一个http接口。
Spring Boot 基于 Spring 开发,Spirng Boot 本身并不提供 Spring 框架的核心特性以及扩展功能,只是用于快速、敏捷地开发新一代基于 Spring 框架的应用程序。也就是说,它并不是用来替代 Spring 的解决方案,而是和 Spring 框架紧密结合用于提升 Spring 开发者体验的工具。Spring Boot 以约定大于配置的核心思想,默认帮我们进行了很多设置,多数 Spring Boot 应用只需要很少的 Spring 配置。同时它集成了大量常用的组件(例如 Redis、MongoDB、Jpa、RabbitMQ、Quartz ,MyBatis等等),Spring Boot 应用中这些组件可以零配置的开箱即用。

我们需要使用springboot常用的组件只要按照在application.xml或yml写规定的格式的配置并且引入相应的集成jar包,在创建springboot项目时 也会有可视化的选择界面,之后在项目中就可以直接使用。
如果我们想不启用springboot约定组件,如不使用springboot自带的数据源组件,可以通过exclude排除
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
这样springboot在启动时则会排除里面的数据源配置properties属

2、springboot 使用传统xml配置

项目背景:
项目上通过springboot框架写的自助机服务,数据库用的springboot的JPA。
现在项目想在连接其他数据库,当然通过注解的形式肯定更简单,资料也更多。
这里学习使用传统xml的配置。其实核心配置使用xml配置对项目维护性也有好处。
业务层代码使用注解是方便很多,组件用xml引入,也就引入一遍。使用xml可读性很高。

2.1 启动类增加 @ImportResource注解引入xml

package com.bsoft.selfServiceMachine;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ComponentScan(basePackages ={
   "com.base", "com.bsoft"})
@ServletComponentScan(basePackages ={
   "com.base","com.bsoft"})
//springboot xml扩展 使用xml引入组件 如创建多个数据源
@ImportResource(locations = {
   "classpath:spring/spring-extend.xml"})
public class SelfServiceMachineApplication {
   

    public static void main(String[] args) {
   
        SpringApplication.run(SelfServiceMachineApplication.class, args);
    }

}

2.2 spring-extend.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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

   <!--  location里的properties可以使用引入springboot 默认的 application.properties,实现配置统一管理-->
    <!-- 并且加载是也会根据spring.profiles.active 多环境配置去读取相应的 dev,prod文件 -->
    <context:property-placeholder location="classpath*:application.properties"/>

    <import resource="spring-hibernate-lis.xml"/>



</beans>

2.3 spring-hibernate-lis.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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/bea
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值