5

我出现以下错误:

Exception: Error creating bean with name 'inventoryService' defined in URL [jar:file:/app.jar!/BOOT-INF/classes!/com/epi/services/inventory/items/InventoryService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'itemRepository': Cannot resolve reference to bean 'databaseClient' while setting bean property 'databaseClient'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'databaseClient' available


2019-06-18 18:38:41,409 INFO  [main] org.apache.juli.logging.DirectJDKLog: Stopping service [Tomcat]


WARNING: An illegal reflective access operation has occurred


WARNING: Illegal reflective access by org.apache.catalina.loader.WebappClassLoaderBase (jar:file:/app.jar!/BOOT-INF/lib/tomcat-embed-core-8.5.29.jar!/) to field java.lang.Thread.threadLocals


WARNING: Please consider reporting this to the maintainers of org.apache.catalina.loader.WebappClassLoaderBase


WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations


WARNING: All illegal access operations will be denied in a future release


2019-06-18 18:38:45,424 INFO  [main] org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener: 




Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.


2019-06-18 18:38:50,695 ERROR [main] org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter: 




***************************


APPLICATION FAILED TO START


***************************




Description:




Parameter 0 of constructor in com.epi.services.inventory.items.InventoryService required a bean named 'databaseClient' that could not be found.






Action:




Consider defining a bean named 'databaseClient' in your configuration.

我的应用程序具有以下类和依赖项:

主模块内部:

  <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-r2dbc</artifactId>
        </dependency>
        <dependency>
            <groupId>io.r2dbc</groupId>
            <artifactId>r2dbc-postgresql</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
        </dependency>
        <dependency>
            <groupId>myGroupId</groupId>
            <artifactId>myModule.dblib</artifactId>
            <version>${project.version}</version>
        </dependency>
       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectreactor</groupId>
            <artifactId>reactor-spring</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-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
        </dependency>
    </dependencies>

应用程序:

@SpringBootApplication(scanBasePackages = "com.pack")
@EntityScan("com.pack")
@EnableR2dbcRepositories
@Import(DatabaseConfiguration.class)
public class InventoryApplication {

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

}

服务:

@Service
@RequiredArgsConstructor
public class InventoryService {

    private final ItemRepository itemRepository;

    public Flux<ItemPojo> getAllItems() {
        return itemRepository.findAllItems()
                             .map(Item::toPojo);
    }
}

回购:

    @Repository
public interface ItemRepository extends ReactiveCrudRepository<Item, Long> {

    Flux<List<Item>> findByName(String name);

    @Query("select i from Item i")
    Flux<Item> findAllItems();

}

实体:

@Data
@Table(name = "items")
public class Item implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

    public static ItemPojo toPojo(final Item items) {
        return new ItemPojo(items.id, items.name);
    }
}

myModule.dblib:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-r2dbc</artifactId>
        </dependency>
        <dependency>
            <groupId>io.r2dbc</groupId>
            <artifactId>r2dbc-postgresql</artifactId>
        </dependency>
        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-r2dbc</artifactId>
        </dependency>
        <dependency>
            <groupId>io.r2dbc</groupId>
            <artifactId>r2dbc-postgresql</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectreactor</groupId>
            <artifactId>reactor-spring</artifactId>
        </dependency>
    </dependencies>

数据库配置:

@Configuration
@EnableR2dbcRepositories
public class DatabaseConfiguration {

    @Value("${spring.data.postgres.host}") private String host;
    @Value("${spring.data.postgres.port}") private int port;
    @Value("${spring.data.postgres.database}") private String database;
    @Value("${spring.data.postgres.username}") private String username;
    @Value("${spring.data.postgres.password}") private String password;

    @Bean
    public PostgresqlConnectionFactory connectionFactory() {
            return new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
                                                    .host(host)
                                                    .port(port)
                                                    .database(database)
                                                    .username(username)
                                                    .password(password)
                                                    .build());
        }
}

我错过了什么?

4

3 回答 3

9

我会无耻地插入我自己关于如何开始使用 R2DBC postgres 和 spring boot 的文章。

R2DBC 入门

我认为您的问题在于您如何启动数据库 connectionFactory。根据您需要扩展和覆盖的文档AbstractR2dbcConfiguration#connectionFactory

@Configuration
@EnableR2dbcRepositories
public class PostgresConfig extends AbstractR2dbcConfiguration {

    @Override
    @Bean
    public ConnectionFactory connectionFactory() {
        return new PostgresqlConnectionFactory(
                PostgresqlConnectionConfiguration.builder()
                .host("localhost")
                .port(5432)
                .username("postgres")
                .password("mysecretpassword")
                .database("myDatabase")
                .build());
    }
}

官方文档

这种方法允许您使用标准 io.r2dbc.spi.ConnectionFactory 实例,容器使用 Spring 的 AbstractR2dbcConfiguration。

与直接注册 ConnectionFactory 实例相比,配置支持还具有额外的优势,即还为容器提供了 ExceptionTranslator 实现,该实现将 R2DBC 异常转换为 Spring 的可移植 DataAccessException 层次结构中的异常,用于使用 @Repository 注释注释的数据访问类。

Spring 的 DAO 支持特性中描述了这种层次结构和 @Repository 的使用。

AbstractR2dbcConfiguration 还注册了数据库交互和存储库实现所需的 DatabaseClient。

官方 R2DBC 文档

于 2019-06-18T20:29:00.440 回答
2

从 TDD 方法中,我尝试使以下 TestNG 方法起作用:

@Test
@Slf4j
@SpringBootTest
class PostgresSanityTesting extends AbstractTestNGSpringContextTests{
    @Autowired
    DatabaseClient databaseClient

    void sanityCheck() {
        assert databaseClient

    }

但要到达那里,还有一些工作......

首先,我没有看到任何有效的代码,除非我强制我的 gradle 子项目使用

ext['spring.version'] = '5.2.0.M2'

这实际上是在R2Dbc 文档中提到的。其他依赖项的一半来自 spring 实验和里程碑。所以“买家要小心”......(例如,我仍然没有看到 R2dbc 正确处理存储库 save(),以及一系列其他问题。)

至于即时编码问题:

如果设置正确,数据库客户端是一个配置为 AbstractR2dbcConfiguration 类的一部分的 @Bean,然后 TestNg 测试将通过。

但是我发现我仍然需要创建一个@Bean,并且我通过@ConfigurationProperties 注入了连接工厂参数。我将这些问题放在两个文件中,因为将 @Bean 与 AbstractR2dbcConfiguration 的子类放在一起对我不起作用。

这是我正在使用的代码,用 groovy 表示。

import io.r2dbc.spi.ConnectionFactory
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Profile
import org.springframework.data.r2dbc.config.AbstractR2dbcConfiguration
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories

@Configuration
class R2DbcConfiguration extends AbstractR2dbcConfiguration {

    private final ConnectionFactory connectionFactory

    R2DbcConfiguration( ConnectionFactory connectionFactory ) {
        this.connectionFactory = connectionFactory
    }
    @Override
    ConnectionFactory connectionFactory() {
        this.connectionFactory
    }
}

import io.r2dbc.postgresql.PostgresqlConnectionConfiguration
import io.r2dbc.postgresql.PostgresqlConnectionFactory
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Profile

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "spring.datasource")
class PostgresConnectionConfig{
    String database
    String username
    String password
    String host
    String port

    @Bean
    PostgresqlConnectionFactory connectionFactory() {
        final def dbPort = port as Integer
        PostgresqlConnectionConfiguration config = PostgresqlConnectionConfiguration.builder() //
                .host(host)
                .port(dbPort)
                .database(database)
                .username(username)
                .password(password).build()
        PostgresqlConnectionFactory candidate = new PostgresqlConnectionFactory(config)
        candidate
    }
}

和我的一部分 application.yml

spring:
  datasource:
    url: jdbc:postgresql://127.0.0.1:5432/postgres
    driver-class-name: org.postgresql.Driver
    driverClassName: org.postgresql.Driver
    database: postgres
    host: localhost
    port: 5432
    password: pokey0
    username: postgres

以及我的子项目的 build.gradle 的摘录

repositories {
    maven { url "https://repo.spring.io/libs-milestone" }
}

dependencies {
    implementation group: 'org.springframework.data', name: 'spring-data-releasetrain', version: 'Lovelace-RELEASE', ext: 'pom'
    implementation group: 'org.springframework.data', name: 'spring-data-r2dbc', version: '1.0.0.M2'
    implementation group: 'io.r2dbc', name: 'r2dbc-postgresql', version: '1.0.0.M7'
    implementation('org.springframework.boot:spring-boot-starter-webflux')
    implementation('io.projectreactor:reactor-test')
    implementation group: 'org.postgresql', name: 'postgresql', version: '42.2.5'
        implementation('org.springframework.boot:spring-boot-starter-actuator')
        testImplementation('org.springframework.boot:spring-boot-starter-test')
        implementation group: 'javax.persistence', name: 'javax.persistence-api', version: '2.2'
}
于 2019-07-04T17:53:56.907 回答
1

原来我需要扩展AbstractR2dbcConfiguration包含DatabaseClientbean 的数据库配置类。

于 2019-06-18T20:48:17.220 回答