[英]repository binding problem with 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")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.