SpringBoot项目启动就停止,数据源启动就关闭、多数据源配置问题


最近在跑一个项目的时候碰到了一些问题。所以来记录一下解决方案。

1. 数据源启动就关闭 INFO com.alibaba.druid.pool.DruidDataSource- {dataSource-1} closed

解决方案:

修改 build.gradle 配置文件

//    runtime("org.apache.tomcat.embed:tomcat-embed-jasper")
    compile("org.apache.tomcat.embed:tomcat-embed-jasper")

注意:这里还有一个原因,可能是由于配置文件中某一个配置缺失导致的。不一定是Druid相关的配置。也不一定是数据库相关的配置。还需要仔细检查!

日志详情:


Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022/04/29-11:47:20 [main] ERROR org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter- 

***************************
APPLICATION FAILED TO START
***************************

2022/04/29-11:47:20 [main] INFO  org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext- Closing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@5491f68b: startup date [Fri Apr 29 11:45:47 CST 2022]; root of context hierarchy
2022/04/29-11:47:20 [main] INFO  org.springframework.context.support.DefaultLifecycleProcessor- Stopping beans in phase 2147483647
2022/04/29-11:47:20 [main] INFO  org.springframework.context.support.DefaultLifecycleProcessor- Stopping beans in phase 20
2022/04/29-11:47:20 [main] INFO  org.springframework.jmx.export.annotation.AnnotationMBeanExporter- Unregistering JMX-exposed beans on shutdown
2022/04/29-11:47:20 [main] INFO  org.springframework.jmx.export.annotation.AnnotationMBeanExporter- Unregistering JMX-exposed beans
2022/04/29-11:47:20 [main] INFO  org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor- Shutting down ExecutorService 'asyncServiceExecutor'
2022/04/29-11:47:20 [main] DEBUG com.augurit.agcloud.opus.common.sc.scc.runtime.kernal.engine.OpusSccCommandFactory- 清除SpringContextHolder中的ApplicationContext:org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@5491f68b: startup date [Fri Apr 29 11:45:47 CST 2022]; root of context hierarchy
2022/04/29-11:47:20 [main] INFO  com.augurit.agcloud.bpm.common.timeCalculate.timer.TimeCalculateTimerShutdown- ----------系统关闭,关闭定时器线程池--------
2022/04/29-11:47:20 [main] INFO  org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler- Shutting down ExecutorService
2022/04/29-11:47:20 [main] INFO  org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor- Shutting down ExecutorService 'downloadExecutor'
2022/04/29-11:47:20 [main] INFO  org.flowable.job.service.impl.asyncexecutor.AbstractAsyncExecutor- Shutting down the async job executor [org.flowable.spring.job.service.SpringAsyncExecutor].
2022/04/29-11:47:20 [flowable-reset-expired-jobs] INFO  org.flowable.job.service.impl.asyncexecutor.ResetExpiredJobsRunnable- stopped resetting expired jobs
2022/04/29-11:47:20 [flowable-acquire-timer-jobs] INFO  org.flowable.job.service.impl.asyncexecutor.AcquireTimerJobsRunnable- stopped async job due acquisition
2022/04/29-11:47:20 [flowable-acquire-async-jobs] INFO  org.flowable.job.service.impl.asyncexecutor.AcquireAsyncJobsDueRunnable- stopped async job due acquisition
2022/04/29-11:47:20 [main] INFO  org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor- Shutting down ExecutorService 'dataExchangeThreadPool'
2022/04/29-11:47:20 [main] INFO  com.alibaba.druid.pool.DruidDataSource- {dataSource-1} closed
Disconnected from the target VM, address: '127.0.0.1:63538', transport: 'socket'

Process finished with exit code 1

多数据源未指定@Primary的问题

简述1:required a single bean, but 2 were found
简述2:At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
要求一个单独的bean

解决方案:

@Configuration
public class DruidConfig {
    @Primary	//选择一个数据源,加上@Primary注解
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean(initMethod = "init", destroyMethod = "close")
    public DruidDataSource druidDataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setProxyFilters(Lists.newArrayList(statFilter()));
        return druidDataSource;
    }
    // 此处省略后面的配置
}

日志详情:

SpringBoot启动Logo
2022/04/29-16:02:33 [main] INFO  org.springframework.data.repository.config.RepositoryConfigurationDelegate- Multiple Spring Data modules found, entering strict repository configuration mode!
2022/04/29-16:02:34 [main] INFO  org.springframework.data.repository.config.RepositoryConfigurationDelegate- Multiple Spring Data modules found, entering strict repository configuration mode!
2022/04/29-16:02:41 [main] INFO  org.apache.coyote.http11.Http11NioProtocol- Initializing ProtocolHandler ["http-nio-8083"]
2022/04/29-16:02:41 [main] INFO  org.apache.catalina.core.StandardService- Starting service [Tomcat]
2022/04/29-16:02:41 [main] INFO  org.apache.catalina.core.StandardEngine- Starting Servlet Engine: Apache Tomcat/8.5.31
2022/04/29-16:02:41 [localhost-startStop-1] INFO  org.apache.catalina.core.AprLifecycleListener- The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [此处省略]
这里省略一堆文件找不到,但不影响运行的log
2022/04/29-16:02:42 [localhost-startStop-1] INFO  org.apache.jasper.servlet.TldScanner- At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2022/04/29-16:02:42 [localhost-startStop-1] INFO  org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/aplanmis-front]- Initializing Spring embedded WebApplicationContext
2022/04/29-16:02:46 [main] INFO  org.apache.catalina.core.StandardService- Stopping service [Tomcat]
Disconnected from the target VM, address: '127.0.0.1:65277', transport: 'socket'

Process finished with exit code 1

参考:【多数据源配置时出现的一些问题】
https://blog.csdn.net/weixin_42074377/article/details/83615895

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot项目配置多数据源,可以使用Spring Boot提供的多数据源支持。以下是一个简单的示例: 首先在application.properties中配置数据源的相关信息: ``` # 数据源1 spring.datasource.url=jdbc:mysql://localhost/test1 spring.datasource.username=root spring.datasource.password=password1 spring.datasource.driver-class-name=com.mysql.jdbc.Driver # 数据源2 spring.datasource.secondary.url=jdbc:mysql://localhost/test2 spring.datasource.secondary.username=root spring.datasource.secondary.password=password2 spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver ``` 然后定义两个数据源配置类: ```java @Configuration @Primary @ConfigurationProperties("spring.datasource") public class DataSource1Config extends HikariDataSource { } @Configuration @ConfigurationProperties("spring.datasource.secondary") public class DataSource2Config extends HikariDataSource { } ``` 在这个示例中,我们使用了HikariCP作为连接池,分别定义了两个数据源配置类。其中,@Primary注解表示默认使用第一个数据源。 接下来,定义两个JdbcTemplate的实例: ```java @Bean(name = "jdbcTemplate1") public JdbcTemplate jdbcTemplate1(@Qualifier("dataSource1") DataSource dataSource) { return new JdbcTemplate(dataSource); } @Bean(name = "jdbcTemplate2") public JdbcTemplate jdbcTemplate2(@Qualifier("dataSource2") DataSource dataSource) { return new JdbcTemplate(dataSource); } ``` 在这个示例中,我们使用了@Qualifier注解来指定要使用的数据源。 最后,在需要访问不同数据源的地方注入相应的JdbcTemplate即可,例如: ```java @Autowired @Qualifier("jdbcTemplate1") private JdbcTemplate jdbcTemplate1; @Autowired @Qualifier("jdbcTemplate2") private JdbcTemplate jdbcTemplate2; ``` 在实际使用中,我们可以根据需要自由切换数据源,以访问不同的数据库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Huathy-雨落江南,浮生若梦

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

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

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

打赏作者

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

抵扣说明:

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

余额充值