Не могу автопровода DataSource - PullRequest
1 голос
/ 05 марта 2020

У меня есть простое приложение, и у меня проблема с этим классом (это только часть)

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 и не могу понять, что я должен делать.

ps Кроме того, вот мои apllication.properties для базы данных (если его требуется). Я не знаю почему, но 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

pps Я прочитал несколько других тем с похожей проблемой, и это не помогло мне.

1 Ответ

2 голосов
/ 05 марта 2020

В build.gradle отсутствуют две зависимости:

  • org.springframework.boot: spring-boot-starter-jdb c - для получения TransactionManager, DataSource et c
  • орг. postgresql: postgresql - загрузить PostgreSQL драйвер
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...