微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Spring r2dbc的存储库绑定问题

如何解决Spring r2dbc的存储库绑定问题

我尝试将Spring R2DBC与postgres驱动程序一起使用。我有以下存储库:

package fr.misc.database.repos.computed;

import fr.misc.database.entities.computed.OrdreDeTravailPivotComputedEntity;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;

public interface OrderRepository extends ReactiveCrudRepository<OrderEntity,String> {

}

我尝试了几种类似的注释:

package fr.misc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories;

@EnableJpaRepositories(basePackages = {"fr.misc.database.repos.common"})
@EntityScan("fr.misc")
@ComponentScan("fr.misc")
@SpringBootApplication
@EnableConfigurationProperties
@EnableR2dbcRepositories("fr.misc.database.repos.computed")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

使用这样的连接工厂:

package fr.misc;

import io.r2dbc.spi.ConnectionFactories;
import io.r2dbc.spi.ConnectionFactory;
import io.r2dbc.spi.ConnectionFactoryOptions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.r2dbc.config.AbstractR2dbcConfiguration;

@Configuration
public class DatabaseConfiguration extends AbstractR2dbcConfiguration {

    @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;

    @Override
    public ConnectionFactory connectionFactory() {
//      return new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder().host(host)
//              .port(port).database(database).username(username).password(password).build());
        return ConnectionFactories.get(ConnectionFactoryOptions.builder()
                .option(DRIVER,POSTGREsql_DRIVER)
                .option(DATABASE,database)
                .option(HOST,host)
                .option(PORT,port)
                .option(PASSWORD,password)
                .option(USER,username)
                .build());
    }
}

和application.yml:

  data:
    postgres:
      host: locahost
      port: 5432
      database: postgres
      username: postgres
      password: postgres

但是我总是遇到以下异常:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'orderConverter' defined in file [../converters/OrderConverter.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDeFinitionException: No qualifying bean of type 'fr.misc.database.repos.computed.OrderRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:798)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:228)
    at org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.autowireConstructor(AbstractAutowireCapablebeanfactory.java:1358)
    at org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.createBeanInstance(AbstractAutowireCapablebeanfactory.java:1204)
    at org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.doCreateBean(AbstractAutowireCapablebeanfactory.java:557)
    at org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.createBean(AbstractAutowireCapablebeanfactory.java:517)
    at org.springframework.beans.factory.support.Abstractbeanfactory.lambda$doGetBean$0(Abstractbeanfactory.java:323)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.Abstractbeanfactory.doGetBean(Abstractbeanfactory.java:321)
    at org.springframework.beans.factory.support.Abstractbeanfactory.getBean(Abstractbeanfactory.java:202)
    at org.springframework.beans.factory.support.DefaultListablebeanfactory.preInstantiateSingletons(DefaultListablebeanfactory.java:879)
    at org.springframework.context.support.AbstractApplicationContext.finishbeanfactoryInitialization(AbstractApplicationContext.java:878)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:126)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
    ... 63 more
Caused by: org.springframework.beans.factory.NoSuchBeanDeFinitionException: No qualifying bean of type 'ffr.misc.database.repos.computed.OrderRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.support.DefaultListablebeanfactory.raiseNoMatchingBeanFound(DefaultListablebeanfactory.java:1695)
    at org.springframework.beans.factory.support.DefaultListablebeanfactory.doResolveDependency(DefaultListablebeanfactory.java:1253)
    at org.springframework.beans.factory.support.DefaultListablebeanfactory.resolveDependency(DefaultListablebeanfactory.java:1207)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:885)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:789)
    ... 81 more

下面是我的OrderConverter构造函数

@Slf4j
@Component
public class OrderConverter {

    private final OrderComputedRepository   orderComputedRepository;

    @Autowired
    public OrderConverter(OrderComputedRepository orderComputedRepository) {
        this.orderComputedRepository = orderComputedRepository;
    }

您知道为什么未绑定我的存储库bean吗?

感谢所有问候

艾德里安

解决方法

错误提示:

没有类型为'ffr.misc.database.repos.computed.OrderRepository'的合格Bean

好像在包的开头有一个附加的“ f”。 因此,我不知道您共享的代码是否正确,但至少与您的注释不匹配

@ EnableR2dbcRepositories(“ fr.misc.database.repos.computed”)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。