【问题标题】:Cant autowire DataSource无法自动装配数据源
【发布时间】:2020-06-17 11:30:28
【问题描述】:

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

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.

我是春天的新手,不明白我应该做什么。

附言另外,这是我的数据库应用程序属性(如果需要)。我不知道为什么,但是 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.我读了一些其他有类似问题的主题,但对我没有帮助。

【问题讨论】:

  • 你在使用 SpringBoot 吗?
  • 是的,我使用 spring initilizer 中的模板,将 build.gradle 添加到问题中
  • 我无法读取 gradle 构建文件,因为我刚刚使用了 maven。但最重要的是,我可以理解依赖项是在依赖项下定义的。因此,您必须添加 Postgres 驱动程序的依赖项。没有驱动程序,它无法连接到数据库。在构建文件中添加 Postgres SQL 驱动程序并重新构建。确保包含正确的版本。
  • thx,添加了数据库驱动程序,但现在有一些其他问题.. 想我会解决它。再次感谢您。

标签: java postgresql spring-boot


【解决方案1】:

build.gradle 缺少两个依赖项:

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

【讨论】:

    猜你喜欢
    • 2018-11-01
    • 2018-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-28
    • 2018-09-03
    • 1970-01-01
    • 2014-04-14
    相关资源
    最近更新 更多