Java 使用Spring Reactive(R2DBC)连接MSSQL,创建bean时出错
我目前正在尝试与Microsoft SQL Server建立数据库连接。不幸的是,我不明白为什么它不起作用。不幸的是,错误信息不能给我准确的信息 初级班Java 使用Spring Reactive(R2DBC)连接MSSQL,创建bean时出错,java,spring,reactive-programming,spring-reactive,Java,Spring,Reactive Programming,Spring Reactive,我目前正在尝试与Microsoft SQL Server建立数据库连接。不幸的是,我不明白为什么它不起作用。不幸的是,错误信息不能给我准确的信息 初级班 @ComponentScan({ "com.example.web", "com.example.repository", "com.example.config", "com.example.domain" }) @SpringBootApplication public class R2Dbc3Application { publ
@ComponentScan({ "com.example.web", "com.example.repository", "com.example.config", "com.example.domain" })
@SpringBootApplication
public class R2Dbc3Application {
public static void main(String[] args) {
SpringApplication.run(R2Dbc3Application.class, args);
}
}
数据库配置:
@Configuration
@EnableR2dbcRepositories("com.example.repository")
public class DatabaseConfiguration extends AbstractR2dbcConfiguration {
private final Logger log = LoggerFactory.getLogger(DatabaseConfiguration.class);
@Value("${spring.data.mssql.host}")
private String host;
@Value("${spring.data.mssql.database}")
private String database;
@Value("${spring.data.mssql.username}")
private String username;
@Value("${spring.data.mssql.password}")
private String password;
@Bean
@Override
public MssqlConnectionFactory connectionFactory() {
System.out.println("Connecting to database" + host);
return new MssqlConnectionFactory(MssqlConnectionConfiguration.builder()
.host(host)
.port(1453)
.database(database)
.username(username)
.password(password)
.build());
}
}
数据库初始值设定项:
@Component
public class DatabaseInitializer {
private final Logger log = LoggerFactory.getLogger(DatabaseInitializer.class);
@Autowired
PersonRepository personRepository;
public DatabaseInitializer(PersonRepository personRepository) {
this.personRepository = personRepository;
}
@PostConstruct
public void init() {
log.info("Initializing database if necessary");
personRepository.findAll().count().subscribe(count -> {
if (count == 0) {
log.info("Database is empty, inserting sample data");
createPerson("Josh", "Long", "Pivotal");
createPerson("Julien", "Dubois", "Microsoft");
} else {
log.info("Database is already initialized");
}
});
}
private void createPerson(String firstName, String lastName, String company) {
Person person = new Person();
person.setFirstName(firstName);
person.setLastName(lastName);
person.setCompany(company);
personRepository.save(person).log().subscribe();
}
}
域:
导入org.springframework.data.annotation.Id;
导入org.springframework.data.relational.core.mapping.Table
@表(“人”)
公共阶层人士{
@Id
private Long id;
private String firstName;
private String lastName;
private String company;
// getter und setter
存储库:
@Repository
public interface PersonRepository extends ReactiveCrudRepository<Person, Long> {
}
请为DatabaseClient添加一个bean,如下所示
@Autowired
private ConnectionFactory connectionFactory;
@Bean
public DatabaseClient databaseClient() {
return create(connectionFactory);
}
为了减少类的数量,您可以删除DatabaseConfiguration类并将下面的添加到application.properties中,Spring将负责创建connectionFactory
server.port=
spring.r2dbc.url=r2dbc:mssql:/<host>/<db-name>
spring.r2dbc.username=
spring.r2dbc.password=
spring.r2dbc.initialization-mode=always
spring.main.web-application-type=reactive
server.port=
spring.r2dbc.url=r2dbc:mssql://
spring.r2dbc.username=
spring.r2dbc.password=
spring.r2dbc.initialization mode=始终
spring.main.web应用程序类型=被动
除非有异常的目录结构或外部包,否则不需要进行componentScan。另外,能否提供完整的堆栈跟踪,同时显示“未能自检类[io.r2dbc.mssql.MSSQLSconnectionFactory]”异常?
2020-05-25 10:23:35.193 INFO 22532 --- [ restartedMain] com.example.app.R2Dbc3Application : Starting R2Dbc3Application on 64LP0Z2 with PID 22532 (C:\tools\Spring-Tool-Suite\R2DBC3\target\classes started by kemal.sacirovic in C:\tools\Spring-Tool-Suite\R2DBC3)
2020-05-25 10:23:35.195 INFO 22532 --- [ restartedMain] com.example.app.R2Dbc3Application : No active profile set, falling back to default profiles: default
2020-05-25 10:23:35.236 INFO 22532 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2020-05-25 10:23:35.237 INFO 22532 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2020-05-25 10:23:35.555 INFO 22532 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data R2DBC repositories in DEFAULT mode.
2020-05-25 10:23:35.588 INFO 22532 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 30ms. Found 1 R2DBC repository interfaces.
Connecting to databasedbtbln01
2020-05-25 10:23:35.930 WARN 22532 --- [ restartedMain] onfigReactiveWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'personController' defined in file [C:\tools\Spring-Tool-Suite\R2DBC3\target\classes\com\example\web\PersonController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personRepository': Cannot resolve reference to bean 'databaseClient' while setting bean property 'databaseClient'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'databaseClient' defined in class path resource [com/example/config/DatabaseConfiguration.class]: Unsatisfied dependency expressed through method 'databaseClient' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'reactiveDataAccessStrategy' defined in class path resource [com/example/config/DatabaseConfiguration.class]: Unsatisfied dependency expressed through method 'reactiveDataAccessStrategy' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'r2dbcMappingContext' defined in class path resource [com/example/config/DatabaseConfiguration.class]: Unsatisfied dependency expressed through method 'r2dbcMappingContext' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'r2dbcCustomConversions' defined in class path resource [com/example/config/DatabaseConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.r2dbc.convert.R2dbcCustomConversions]: Factory method 'r2dbcCustomConversions' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'connectionFactory' defined in class path resource [com/example/config/DatabaseConfiguration.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [io.r2dbc.mssql.MssqlConnectionFactory] from ClassLoader [sun.misc.Launcher$AppClassLoader@764c12b6]
2020-05-25 10:23:35.947 INFO 22532 --- [ restartedMain] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-05-25 10:23:35.956 ERROR 22532 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'personController' defined in file [C:\tools\Spring-Tool-Suite\R2DBC3\target\classes\com\example\web\PersonController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personRepository': Cannot resolve reference to bean 'databaseClient' while setting bean property 'databaseClient'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'databaseClient' defined in class path resource [com/example/config/DatabaseConfiguration.class]: Unsatisfied dependency expressed through method 'databaseClient' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'reactiveDataAccessStrategy' defined in class path resource [com/example/config/DatabaseConfiguration.class]: Unsatisfied dependency expressed through method 'reactiveDataAccessStrategy' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'r2dbcMappingContext' defined in class path resource [com/example/config/DatabaseConfiguration.class]: Unsatisfied dependency expressed through method 'r2dbcMappingContext' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'r2dbcCustomConversions' defined in class path resource [com/example/config/DatabaseConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.r2dbc.convert.R2dbcCustomConversions]: Factory method 'r2dbcCustomConversions' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'connectionFactory' defined in class path resource [com/example/config/DatabaseConfiguration.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [io.r2dbc.mssql.MssqlConnectionFactory] from ClassLoader [sun.misc.Launcher$AppClassLoader@764c12b6]
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:798) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:228) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1358) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1204) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:882) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.refresh(ReactiveWebServerApplicationContext.java:66) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at com.example.app.R2Dbc3Application.main(R2Dbc3Application.java:13) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_212]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_212]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_212]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_212]
@Autowired
private ConnectionFactory connectionFactory;
@Bean
public DatabaseClient databaseClient() {
return create(connectionFactory);
}
server.port=
spring.r2dbc.url=r2dbc:mssql:/<host>/<db-name>
spring.r2dbc.username=
spring.r2dbc.password=
spring.r2dbc.initialization-mode=always
spring.main.web-application-type=reactive