springBoot 项目排除数据库启动方式

目录
  • springBoot 排除数据库启动
    • 1. 场景
    • 2. 配置
  • Springboot不经过数据库直接启动
    • 问题
    • 错误
    • 原因
    • 解决

springBoot 排除数据库启动

1. 场景

在家里运行项目,运行springBoot的时候报数据库连接不了,公司的数据库家里不能连接。

2. 配置

2.1 保留之前的properties 配置,不删除;

2.2 在启动类中添加

exclude = {DataSourceAutoConfiguration.class}
package cn.cncommdata.file;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
 * 启动类
 */
@SpringBootApplication(scanBasePackages = "cn.cncommdata", exclude = {DataSourceAutoConfiguration.class})
@EnableFeignClients(basePackages = {"cn.cncommdata", "cc.iooc"})
@MapperScan("cn.cncommdata.file.dao")
public class FileApplication {
    /**
     * main
     *
     * @param args args
     */
    public static void main(String[] args) {
        SpringApplication.run(FileApplication.class, args);
    }
    /**
     * 没什么用,就是不想让checkstyle报错
     */
    public void init() {
    }
}

Springboot不经过数据库直接启动

问题

之前开发工程中,有一个单独注册在nacos上服务的项目(不需要访问数据库)需要启动,但是启动会报错。

错误

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method ‘dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourcePropertiesDataSourceBeanCreationException: Failed to determine a suitable driver class

原因

SpringBoot项目默认会访问数据库,因为此时没有写数据库连接,所以出现这个错误。

解决

在启动类上加上一个注解,在容器加载的时候默认排除数据库连接即可。

具体如图所示:

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • Spring Boot 项目启动自动执行方法的两种实现方式

    目录 实际应用场景: 第一种实现ApplicationRunner接口 第二种实现CommandLineRunner接口 对比: 注意: 实际应用场景: springboot项目启动成功后执行一段代码,如系统常量,配置.代码集等等初始化操作:执行多个方法时,执行顺序使用Order注解或Order接口来控制. Springboot给我们提供了两种方式 第一种实现ApplicationRunner接口 package org.mundo.demo.core; import org.springfra

  • 解决Springboot项目启动后自动创建多表关联的数据库与表的方案

    熬夜写完,尚有不足,但仍在努力学习与总结中,而您的点赞与关注,是对我最大的鼓励! 在一些本地化项目开发当中,存在这样一种需求,即开发完成的项目,在第一次部署启动时,需能自行构建系统需要的数据库及其对应的数据库表. 若要解决这类需求,其实现在已有不少开源框架都能实现自动生成数据库表,如mybatis plus.spring JPA等,但您是否有想过,若要自行构建一套更为复杂的表结构时,这种开源框架是否也能满足呢,若满足不了话,又该如何才能实现呢? 我在前面写过一篇 Activiti工作流学习笔记(

  • springboot 启动时初始化数据库的步骤

    问题描述 在spring-boot启动时,希望能执行相应的sql文件来初始化数据库. 使用配置文件初始化数据库 可以在spring-boot的配置文件application.yml中设置要初始化的sql文件.这是最简单的方法,只需要添加属性就可以实现. 首先设置spring.datasource.initialization-mode=always表示任何类型数据库都进行数据库初始化,默认情况下,spring-boot会自动加载data.sql或data-${platform}.sql文件来初始

  • springboot 启动如何排除某些bean的注入

    springboot 启动排除某些bean的注入 问题: 最近做项目的时候,需要引入其他的jar.然后还需要扫描这些jar里的某些bean.于是使用注解:@ComponentScan 这个注解直接指定包名就可以,它会去扫描这个包下所有的class,然后判断是否解析: @ComponentScan(basePackages = {"your.pkg","other.pkg"}) public class Application { } 其他的jar中定义了 redis

  • springBoot 项目排除数据库启动方式

    目录 springBoot 排除数据库启动 1. 场景 2. 配置 Springboot不经过数据库直接启动 问题 错误 原因 解决 springBoot 排除数据库启动 1. 场景 在家里运行项目,运行springBoot的时候报数据库连接不了,公司的数据库家里不能连接. 2. 配置 2.1 保留之前的properties 配置,不删除: 2.2 在启动类中添加 exclude = {DataSourceAutoConfiguration.class} package cn.cncommdat

  • springboot项目main函数启动的操作

    springboot项目main函数启动 在controller包下新建appController类 package controller; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBu

  • springboot的四种启动方式

    目录 环境准备 第一种:直接main方法启动TxDemo2Application 第二种:通过maven插件来启动 第三种打jar包来访问 第四种通过docker容器虚拟化运行 环境准备 创建工程 pom.xml内容 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="h

  • 浅谈springboot的三种启动方式

    有段时间没有写博客了,也在努力的从传统单机开发向分布式系统过度,所以再次做一些笔记,以方便日后查看. 直接进入正题吧,今天记录spring-boot项目的三种启动方式. spring-boot的启动方式主要有三种: 1. 运行带有main方法类 2. 通过命令行 java -jar 的方式 3. 通过spring-boot-plugin的方式 一.执行带有main方法类 这种方式很简单,我主要是通过idea的方式,进行执行.这种方式在启动的时候,会去自动加载classpath下的配置文件 (这里

  • Intellij IDEA实现SpringBoot项目多端口启动的两种方法

    前言 有时候使用springboot项目时遇到这样一种情况,用一个项目需要复制很多遍进行测试,除了端口号不同以外,没有任何不同.这时我们强大的Intellij IDEA就能替我们实现. 实现方法 第一种方法 1.点击图中Edit Configurations,如图 2.取消选中的Single instance only 3.启动项目,demo(9000),如图. 4.修改配置文件中的端口号为9001,启动项目,demo(9001),如图. 从下方可以看到demo项目分别以9000和9001启动了

  • springBoot项目如何实现启动多个实例

    springBoot项目启动多个实例 今天碰到一个需求是,将一个服务提供者启动两个实例,一个实例对外,一个实例对内,对内价格有折扣,两个实例通过指定不停的profile来区分,要求是不能改造为两个服务提供者,于是我就焦灼了,知道是配置不同的配置文件,但是却是不知道怎么同时启动两个实例.在网上找来找去,稀里糊涂的弄好了......... 下面附上具体的做法: 根据我的业务需求,我要在配置文件中配置一个折扣, 起名为: discount .然后根据启动不同的配置文件中从,controller中取得该

  • eclipse创建springboot项目的三种方式总结

    目录 方法一 安装STS插件 新建spring boot项目 项目启动 方法二 1.创建Maven项目 2.选择项目类型 3.选择项目 4.编写项目组和名称-finish即可 5.修改pom.xml文件 6.pom.xml中添加依赖 7.pom.xml中添加编译插件 8.基础包和类 9.创建resources文件夹和application.properties文件 10.App.java 11.HelloController.java 12.启动项目 13.访问项目(低版本可能无法访问,2版本可

  • 详解springboot的三种启动方式

    一:IDE 运行Application这个类的main方法 二:在springboot的应用的根目录下运行mvn spring-boot:run 三:使用mvn install 生成jar后运行 先到项目根目录 mvn install cd target java -jar xxxx.jar 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们.

  • Springboot项目对数据库用户名密码实现加密过程解析

    pom.xml文件中引入如下内容 <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>1.16</version> </dependency> 保存pom.xml文件,更新jar 将当前路径切换到jasypt包目录下: 使用如下命

  • springboot使用外置tomcat启动方式

    目录 使用外置tomcat启动 使用外置的tomcat启动注意事项 使用外置tomcat启动 打开pom文件,把打包格式设置为war <packaging>war</packaging> SpringBoot默认有内置的tomcat运行模块,可以在Application(继承SpringBootServletInitializer)中直接main启动. 如下配置可在外置tomcat容器中运行 <dependency> <groupId>org.springf

随机推荐