使用 tomcat embedded 作为数据源而不是 Hikari?

Use tomcat embbed as a datasource instand of Hikari?

我正在尝试使用 Spring Boot + Spring Data JPA + thymleaf 创建一个 CRUD 应用程序。

当我 运行 应用程序正常但我无法在浏览器中打开它。我注意到在日志中选择的 ApplicationContext 不是 AnnotationConfigEmbeddedWebApplicationContext。

这是我的 pom

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>com.mycompany</groupId>
<artifactId>Springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>Springboot</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <!--scope>provided</scope-->
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

这是我的 application.properties

server.port = 8081
spring.datasource.url = jdbc:sqlserver://local;hosts:1433;databaseName=Graduate6045
spring.datasource.username = sa
spring.datasource.password = P@ssw0rd
spring.datasource.driver = com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.SQLServerDialect
logging.level.org.hibernate.SQL = debug
spring.jpa.hibernate.ddl-auto = create

这是我的日志

  .   ____          _            __ _ _
 /\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.2.RELEASE)

2018-06-11 12:03:53.598  INFO 6276 --- [           main] c.m.S.SpringbootApplicationTests         : Starting SpringbootApplicationTests on LPTADO76 with PID 6276 (started by S2026045 in D:\New folder\java\projects\Springboot)
2018-06-11 12:03:53.614  INFO 6276 --- [           main] c.m.S.SpringbootApplicationTests         : No active profile set, falling back to default profiles: default
2018-06-11 12:03:53.677  INFO 6276 --- [           main] o.s.w.c.s.GenericWebApplicationContext   : Refreshing org.springframework.web.context.support.GenericWebApplicationContext@12c8e7b: startup date [Mon Jun 11 12:03:53 GMT+02:00 2018]; root of context hierarchy
2018-06-11 12:03:55.431  INFO 6276 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$a8b18764] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-06-11 12:03:55.884  INFO 6276 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2018-06-11 12:03:57.032  INFO 6276 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2018-06-11 12:03:57.110  INFO 6276 --- [           main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2018-06-11 12:03:57.141  INFO 6276 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
 name: default
 ...]
2018-06-11 12:03:57.297  INFO 6276 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.2.17.Final}
2018-06-11 12:03:57.297  INFO 6276 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2018-06-11 12:03:57.390  INFO 6276 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2018-06-11 12:03:57.609  INFO 6276 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.SQLServerDialect
2018-06-11 12:03:58.374 DEBUG 6276 --- [           main] org.hibernate.SQL                        : drop table hibernate_sequence
2018-06-11 12:03:58.390 DEBUG 6276 --- [           main] org.hibernate.SQL                        : drop table users
2018-06-11 12:03:58.390 DEBUG 6276 --- [           main] org.hibernate.SQL                        : create table hibernate_sequence (next_val numeric(19,0))
2018-06-11 12:03:58.390 DEBUG 6276 --- [           main] org.hibernate.SQL                        : insert into hibernate_sequence values ( 1 )
2018-06-11 12:03:58.390 DEBUG 6276 --- [           main] org.hibernate.SQL                        : create table users (id int not null, address varchar(255), email varchar(255), username varchar(255), password varchar(255), primary key (id))
2018-06-11 12:03:58.405  INFO 6276 --- [           main] o.h.t.schema.internal.SchemaCreatorImpl  : HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@14beff9'
2018-06-11 12:03:58.405  INFO 6276 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2018-06-11 12:03:58.876  INFO 6276 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-06-11 12:03:59.736  INFO 6276 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.web.context.support.GenericWebApplicationContext@12c8e7b: startup date [Mon Jun 11 12:03:53 GMT+02:00 2018]; root of context hierarchy
2018-06-11 12:03:59.799  WARN 6276 --- [           main] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2018-06-11 12:03:59.861  INFO 6276 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/],methods=[GET]}" onto public java.lang.String com.mycompany.Springboot.controller.Controller.hello()
2018-06-11 12:03:59.861  INFO 6276 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-06-11 12:03:59.861  INFO 6276 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-06-11 12:03:59.924  INFO 6276 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-06-11 12:03:59.924  INFO 6276 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-06-11 12:04:00.033  WARN 6276 --- [           main] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
2018-06-11 12:04:00.485  INFO 6276 --- [           main] c.m.S.SpringbootApplicationTests         : Started SpringbootApplicationTests in 7.339 seconds (JVM running for 9.125)
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 8.499 s - in com.mycompany.Springboot.SpringbootApplicationTests
2018-06-11 12:04:00.738  INFO 6276 --- [       Thread-2] o.s.w.c.s.GenericWebApplicationContext   : Closing org.springframework.web.context.support.GenericWebApplicationContext@12c8e7b: startup date [Mon Jun 11 12:03:53 GMT+02:00 2018]; root of context hierarchy
2018-06-11 12:04:00.738  INFO 6276 --- [       Thread-2] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2018-06-11 12:04:00.738  INFO 6276 --- [       Thread-2] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2018-06-11 12:04:00.753  INFO 6276 --- [       Thread-2] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

根据 Spring boot's datasource 你的属性名称是错误的,使用 driver-class-name:

    spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver

或者您可以使用 spring.datasource.type 属性

覆盖

You can bypass that algorithm completely and specify the connection pool to use by setting the spring.datasource.type property

spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource

实现相同结果的其他方式。 (注意:反之亦然。我正在使用 spring boot 1.5.4.RELEASE 版本并希望使用 hikari 而不是 tomcat-jdbc)

 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <exclusions>
        <exclusion>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-jdbc</artifactId>
</dependency>