java - 无法 Autowiring 数据源

标签 java postgresql spring-boot

我有一些简单的应用程序,但我对这个类有问题(它只是一部分)

import javax.sql.DataSource;

@Repository
@Transactional
public class AppUserDAO extends JdbcDaoSupport {

    @Autowired
    public AppUserDAO(DataSource dataSource) {
        this.setDataSource(dataSource);
    }

这是 build.gradle:

plugins {
    id 'org.springframework.boot' version '2.2.5.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {

    compile group: 'org.springframework', name: 'spring-jdbc', version: '5.2.4.RELEASE'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    testImplementation 'org.springframework.security:spring-security-test'
}

test {
    useJUnitPlatform()
}

当我启动我的应用程序时,我会得到下一个:

Description:

Parameter 0 of constructor in com.example.demo.dao.AppUserDAO required a bean of type 'javax.sql.DataSource' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)

The following candidates were found but could not be injected:
    - Bean method 'dataSource' in 'JndiDataSourceAutoConfiguration' not loaded because @ConditionalOnProperty (spring.datasource.jndi-name) did not find property 'jndi-name'
    - Bean method 'dataSource' in 'XADataSourceAutoConfiguration' not loaded because @ConditionalOnClass did not find required class 'javax.transaction.TransactionManager'


Action:

Consider revisiting the entries above or defining a bean of type 'javax.sql.DataSource' in your configuration.

我是 Spring 新手,不明白我应该做什么。

附:另外,这是我的数据库应用程序属性(如果需要的话)。我不知道为什么,但是 postgresql.Driver 用红色下划线作为错误。

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=12345

p.p.s。我阅读了其他一些有类似问题的主题,但它对我没有帮助。

最佳答案

build.gradle 缺少两个依赖项:

  • org.springframework.boot:spring-boot-starter-jdbc - 获取 TransactionManager、DataSource 等
  • org.postgresql:postgresql - 加载 PostgreSQL 驱动程序

关于java - 无法 Autowiring 数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60535102/

相关文章:

java - 以编程方式选择 selectManyCheckbox 组件

java - Spring Boot 中预定义的 bean

gradle - 如何在使用gradle的多模块系统中使用sonar + jacoco生成用于集成测试的代码覆盖率报告

java - 使用 JPA 跳过有关 INSERT 的专栏

Postgresql Ubuntu 的奇怪行为(版本不兼容?)

postgresql - Postgres : How to verify which certificate is being used for SSL

java - 当 Spring 属性无效时提供更好的日志记录

java.util.regex.PatternSyntaxException : null (in java. util.regex.Pattern)错误

java - 使用 Guava Collections2 转换方法作为 Apache CollectionUtil.forAllDo

Java序列化和处理对象