【发布时间】:2019-08-28 09:50:36
【问题描述】:
我有一个不是 Web 应用程序的 Spring Boot 应用程序。在这个应用程序中,我在以下 bean 的帮助下配置了嵌入式 tomcat。
@豆 public TomcatServletWebServerFactory tomcatFactory() {
return new TomcatServletWebServerFactory() {
protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
tomcat.enableNaming();
return super.getTomcatWebServer(tomcat);
}
protected void postProcessContext(Context context) {
ContextResource contextResource = new ContextResource();
contextResource.setName("jdbc/BPMDB");
contextResource.setType(DataSource.class.getName());
contextResource.setProperty("driverClassName", env.getProperty("bpm.db.driverClassName"));
contextResource.setProperty("url", env.getProperty("bpm.db.url"));
contextResource.setProperty("username", env.getProperty("bpm.db.username"));
contextResource.setProperty("password", env.getProperty("bpm.db.password"));
context.getNamingResources().addResource(contextResource);
}
};
}
我如何为这个嵌入式 tomcat 做连接池。我正在使用 spring boot 2.x,它说 hikaricp 是默认的连接池,但是如何将它设置到这个嵌入式 tomcat 中。 这是否需要设置诸如 spring.datasource.hikari.initial-size=15 之类的属性 spring.datasource.hikari.max-wait=20000
但同样,boot 将如何知道以及我将如何知道这些属性已被使用。
谢谢。
【问题讨论】:
-
放弃这个bean方法,你基本上就完成了。将属性从
bpm.db重命名为spring.datasource就完成了。 -
@M.Deinum 那么嵌入式服务器将在没有这个 bean 的情况下运行吗?
-
当然会,我为什么还要提到这个。我强烈建议阅读 Spring Boot 参考指南和/或 Spring.io 网站中的一些指南/教程来学习 Spring Boot。
-
HikariCP 的可用性取决于依赖关系,希望您使用过这两个启动器 spring-boot-starter-jdbc 或 spring-boot-starter-data-jpa 中的任何一个。除非您定义自己的数据源 bean,否则始终自动配置。
-
@NirajJha 是的,我正在使用 spring-boot-starter-data-jpa。这会启用与 hikari 的连接池吗?
标签: spring-boot java-8 hikaricp embedded-tomcat