我尝试将 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 未绑定吗?
感谢所有和最好的问候
阿德里安