当前位置: 首页 > 知识库问答 >
问题:

在springboot中使用tomcat池数据源时,我没有得到类型为'javax.sql.datasource'的合格bean

海宁
2023-03-14

我试图仅通过在application.yml文件中指定如下属性,将池化tomcat datasource与spring boot一起使用:

spring:
  data:
    jpa:
      repositories:
        enabled: true
  datasource:
    tomcat:
      max-active: 50
      max-wait: 10000
      username: postgres
      password: root
      url: jdbc:postgresql://localhost:5432/proj
      driver-class-name: org.postgresql.Driver

我得到

由:org.springframework.boot.autoconfigure.jdbc.datasourceProperties$datasourceBeanCreationException引起:无法确定数据库类型none的嵌入式数据库驱动程序类。如果您想要一个嵌入式数据库,请在类路径上放置一个受支持的数据库。如果要从特定配置文件加载数据库设置,则可能需要激活它(当前没有配置文件处于活动状态)。

    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
        at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
        at org.springframework.beans.factory
.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)

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

<groupId>com.yyyy.xxxx</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>xxxx</name>
<description>xxxx project</description>

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

<properties>
    <kotlin.compiler.incremental>true</kotlin.compiler.incremental>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <jsoup.version>1.10.3</jsoup.version>
    <log4j.version>1.2.17</log4j.version>
</properties>

<organization>
    <name>SA Technologies</name>
    <url>www.satechnologies.com</url>
</organization>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-hateoas</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.6</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
    </dependency>
    <dependency>
        <groupId>org.jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <version>${jsoup.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate</artifactId>
        <version>3.5.4-Final</version>
        <type>pom</type>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.2</version><!--$NO-MVN-MAN-VER$ -->
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-core</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.196</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>${log4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.assertj</groupId>
        <artifactId>assertj-core</artifactId>
        <version>3.8.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-jdbc</artifactId>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>de.jpdigital</groupId>
            <artifactId>hibernate5-ddl-maven-plugin</artifactId>
            <version>1.0.1-hibernate-5.1.2.Final</version>
            <configuration>
                <dialects>
                    <param>POSTGRESQL9</param>
                    <param>SQLSERVER2012</param>
                </dialects>
                <packages>
                    <param>com.yyyy.xxxx.model</param>
                </packages>
                <outputDirectory>${project.basedir}/docs/sql</outputDirectory>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>gen-ddl</goal>
                    </goals>
                    <phase>process-classes</phase>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
@EnableJpaRepositories(basePackages = { "com.yyyy.xxxx.persistence.repositories" })
@EnableTransactionManagement
@EntityScan(basePackages = { "com.yyyy.xxxx.persistence.model" })
@Configuration
public class PersistenceConfig {

}

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoggingInterceptor());
    }
}

@SpringBootApplication(scanBasePackages = { "com.yyyy.xxxx" })
public class XxxxApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(XxxxApplication.class);
    }

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

有人能帮忙吗?

共有1个答案

淳于典
2023-03-14

这4个属性位于datasource下,而不是Tomcat下。

  username: postgres
  password: root
  url: jdbc:postgresql://localhost:5432/proj
  driver-class-name: org.postgresql.Driver

在https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/datasourceProperties.java中查看datasourceProperties

进一步参考:https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html#howto-use-yaml-for-external-properties

 类似资料:
  • 我很难理解为什么会发生这种错误。我正在将教程移植到最新版本的Spring、Hibernate和WildFly。我从命令行运行,使用Maven构建和测试应用程序。我得到以下错误: 2015年7月10日下午2:18:03 org.springframework.test.context。TestContextManager prepareTestInstance SEVERE:允许TestExecut

  • 当我尝试自动连接扩展CrudRepository的接口时,我遇到了这个错误。我有两个用于两个数据库的hibernate xml配置。整个堆栈是 unsatisfiedDependencyException:创建名为“Hello Controller”的bean时出错:通过字段“stock service”表示的不满足的依赖项;嵌套异常为org.springframework.beans.facto

  • 问题内容: 我正在尝试构建一个全新的Spring Framework 4.0项目,而没有所有神奇的东西,而只是简单地将它踢过去。 我在这里关注该教程:http : //spring.io/guides/tutorials/data/并取得了一些成功。我只是停留在这一点上。 当我运行此单元测试时,得到以下堆栈跟踪: 根据观察和研究,似乎是在告诉我有两个EntityManager类。第一个来自hibe

  • 在JUnit Run中将Spring Boot版本从2.1.3.RELEASE升级到2.5.12后,当我尝试“mvn包”时,我遇到了以下异常 还升级了相关依赖项版本: 下面是完整的堆栈跟踪 TransactionalDataConfiguration: 雪花配置:

  • 问题内容: 我无法开始使用。我已经按照本指南创建了一个简单的Spring Boot应用程序,但是使用了Hibernate和相关的配置。 build.gradle: 每当我尝试运行该应用程序时,都会得到以下结果: 我已经看到此错误(据报道,该错误已从1.10.7开始修复),但降级至 1.9.x 并没有帮助,我仍然遇到相同的错误。 有任何想法吗? 问题答案: 问题出在。从依赖项中删除它可以解决此问题。

  • 我使用Gson Library 2.8.0将JSON字符串转换为Java Hashmap,Maven作为构建系统,Tomcat作为WebApp运行我的代码。IDE:IntelliJ Idea 问题是每当我运行我的代码,执行停止和抛出类NotFound异常。 代码==>