Following the Getting Started with R2DBC video, I convert some of the repositories to reactive in an existing Spring Boot application with PostgreSQL as the database. The application works before the conversion. After I try to start up the application, I get the following error:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'albumReactResource' defined in file [/app/classes/com/mycompany/myapp/web/rest/react/AlbumReactResource.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'albumReactRepository' defined in class path resource [com/mycompany/myapp/config/ReactDatabaseConfig.class]: Unsatisfied dependency expressed through method 'albumReactRepository' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'r2dbcRepositoryFactory' defined in class path resource [com/mycompany/myapp/config/ReactDatabaseConfig.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [org.springframework.data.r2dbc.repository.support.R2dbcRepositoryFactory] from ClassLoader [sun.misc.Launcher$AppClassLoader#4e0e2f2a]
...
gallery-app_1 | Caused by: java.lang.ClassNotFoundException: org.springframework.data.repository.query.QueryMethodEvaluationContextProvider
gallery-app_1 | at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
gallery-app_1 | at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
gallery-app_1 | at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
gallery-app_1 | at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
gallery-app_1 | ... 51 common frames omitted
The QueryMethodEvaluationContextProvider class should be there. I guess that the database configuration can't co-exist with the existing database and Spring Data configuration.
The Spring Boot version is 2.0.5.RELEASE. And related dependencies are the followings:
compile "org.springframework.boot:spring-boot-starter-webflux"
compile "org.springframework.data:spring-data-jdbc:1.0.0.r2dbc-SNAPSHOT"
compile "io.r2dbc:r2dbc-postgresql:1.0.0.M5"
Here is the database configuration which is the same as the code for the video:
Configuration
public class ReactDatabaseConfig {
#Bean
PostgresqlConnectionFactory connectionFactory(){
return new PostgresqlConnectionFactory(
PostgresqlConnectionConfiguration.builder()
.host("localhost")
.database("gallery")
.username("postgres")
.password("")
.build()
);
}
#Bean
DatabaseClient databaseClient(ConnectionFactory connectionFactory){
return DatabaseClient.builder()
.connectionFactory(connectionFactory)
.build();
}
#Bean
R2dbcRepositoryFactory r2dbcRepositoryFactory(DatabaseClient client){
RelationalMappingContext context = new RelationalMappingContext();
context.afterPropertiesSet();
return new R2dbcRepositoryFactory(client, context);
}
#Bean
AlbumReactRepository albumReactRepository(R2dbcRepositoryFactory factory){
return factory.getRepository(AlbumReactRepository.class); <-- exceptions occurs
}
...
}
Some related dependency information as the followings:
+--- org.springframework.data:spring-data-jdbc:1.0.0.r2dbc-SNAPSHOT
| +--- org.springframework.data:spring-data-commons:2.1.1.BUILD-SNAPSHOT -> 2.0.10.RELEASE (*)
...
+--- org.springframework.security:spring-security-data -> 5.0.8.RELEASE
| +--- javax.xml.bind:jaxb-api:2.3.0
| +--- org.springframework.data:spring-data-commons:2.0.10.RELEASE (*)
...
| +--- org.springframework.data:spring-data-jpa:2.0.10.RELEASE
| | +--- org.springframework.data:spring-data-commons:2.0.10.RELEASE
How to fix this problem?
Make sure AlbumReactRepository is extending
org.springframework.data.repository.reactive.ReactiveCrudRepository
and not
org.springframework.data.repository.CrudRepository
Related
I am trying to use the ConfigurableMongoDbMessageStore as a message-store in my spring-integration aggregator component. For some reason the following exception is thrown:
Caused by: org.springframework.data.mapping.model.MappingInstantiationException: Failed to instantiate org.springframework.messaging.support.GenericMessage using constructor NO_CONSTRUCTOR with arguments
at org.springframework.data.convert.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:67)
at org.springframework.data.convert.ClassGeneratingEntityInstantiator.createInstance(ClassGeneratingEntityInstantiator.java:84)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:272)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:245)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readValue(MappingMongoConverter.java:1491)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$MongoDbPropertyValueProvider.getPropertyValue(MappingMongoConverter.java:1389)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$AssociationAwareMongoDbPropertyValueProvider.getPropertyValue(MappingMongoConverter.java:1438)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$AssociationAwareMongoDbPropertyValueProvider.getPropertyValue(MappingMongoConverter.java:1401)
at org.springframework.data.mapping.model.PersistentEntityParameterValueProvider.getParameterValue(PersistentEntityParameterValueProvider.java:71)
at org.springframework.data.mapping.model.SpELExpressionParameterValueProvider.getParameterValue(SpELExpressionParameterValueProvider.java:49)
at org.springframework.data.convert.ClassGeneratingEntityInstantiator$EntityInstantiatorAdapter.extractInvocationArguments(ClassGeneratingEntityInstantiator.java:250)
at org.springframework.data.convert.ClassGeneratingEntityInstantiator$EntityInstantiatorAdapter.createInstance(ClassGeneratingEntityInstantiator.java:223)
at org.springframework.data.convert.ClassGeneratingEntityInstantiator.createInstance(ClassGeneratingEntityInstantiator.java:84)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:272)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:245)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:194)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:190)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:78)
at org.springframework.data.mongodb.core.MongoTemplate$ReadDocumentCallback.doWith(MongoTemplate.java:3017)
at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:2673)
at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2404)
at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2387)
at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:823)
at org.springframework.data.mongodb.core.MongoTemplate.findOne(MongoTemplate.java:772)
at org.springframework.integration.mongodb.store.ConfigurableMongoDbMessageStore.getMessageGroup(ConfigurableMongoDbMessageStore.java:115)
at org.springframework.integration.mongodb.store.ConfigurableMongoDbMessageStore.addMessageToGroup(ConfigurableMongoDbMessageStore.java:138)
at org.springframework.integration.aggregator.AbstractCorrelatingMessageHandler.store(AbstractCorrelatingMessageHandler.java:757)
at org.springframework.integration.aggregator.AbstractCorrelatingMessageHandler.handleMessageInternal(AbstractCorrelatingMessageHandler.java:479)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:162)
... 83 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.messaging.support.GenericMessage]: No default constructor found; nested exception is java.lang.NoSuchMethodException: org.springframework.messaging.support.GenericMessage.<init>()
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:129)
at org.springframework.data.convert.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:64)
... 111 common frames omitted
Caused by: java.lang.NoSuchMethodException: org.springframework.messaging.support.GenericMessage.<init>()
at java.base/java.lang.Class.getConstructor0(Class.java:3350)
at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2554)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:122)
... 112 common frames omitted
The bean is created and used as follows:
In SpringIntegrationBeans.java
#Autowired
private MongoTemplate mongoTemplate;
#Bean(name = "configurableMongoDbMessageStore")
public ConfigurableMongoDbMessageStore configurableMongoDbMessageStore() {
return new ConfigurableMongoDbMessageStore(mongoTemplate);
}
In spring-integration.xml
<int:aggregator id="myAggregator"
ref="testingAggregator"
message-store="configurableMongoDbMessageStore"/>
I am using the following spring-integration-mongodb package:
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-mongodb</artifactId>
<version>5.1.2.RELEASE</version>
</dependency>
I found similar q&a in Spring Integration Aggregator with MongoDbMessageStore: Failed to instantiate GenericMessage: No default constructor found but that seems to be applicable for MongoDbMessageStore rather than ConfigurableMongoDbMessageStore
I would appreciate any help/advice.
The mongo template needs a suitably configured converter; inject the db factory instead and the store will create an appropriate template.
#Autowired
private MongoDbFactory dbFactory;
#Bean(name = "configurableMongoDbMessageStore")
public ConfigurableMongoDbMessageStore configurableMongoDbMessageStore() {
return new ConfigurableMongoDbMessageStore(dbFactory);
}
I have a spring boot application where I need to store image into a local folder and its details like id,name and path in db.I done this with no problem. Now I want to edit the name in the folder as well as in db. I succeeded in renaming the file in the folder and i want to rename in db as well. I have written code for this but i got error like unsatisfied dependency. In my controller
#PutMapping("/updateFile")
public String updateFile(#RequestParam String oldFileName,#RequestParam String newFileName,#RequestParam UUID imageId,#RequestParam ImageModel imageModel) throws IOException {
// UUID Id=UUID.fromString(imageId);
imgserv.updateImageData(imageId, imageModel);
return fileStorageService.updateFile(oldFileName, newFileName);
}
and in service implementation
#Override
public String updateImageData(UUID imageId, ImageModel imageModel) {
ImageModel imageData = imageRepo.findImageById(imageId);
if (imageId == null) {
return "Image Data Not Found";
}
imageData.setImageName(imageModel.getImageName());
imageRepo.save(imageData);
return "Image Details Updated Successfully!";
}
and my Repository class
#Repository("imageRepo")
public interface ImageRepo extends MongoRepository<ImageModel, UUID> {
ImageModel findImageById(UUID imageId);
}
and my error code is like
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'fileController': Unsatisfied dependency expressed through field 'imgserv'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'imgserv': Unsatisfied dependency expressed through field 'imageRepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'imageRepo': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property id found for type ImageModel!
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:586) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:372) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1341) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:572) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) ~[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at
I am trying to use Spring cloud config with zookeeper for configuration management.
My build.gradle looks like:
dependencies {
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.cloud:spring-cloud-starter-zookeeper-config')
compile('org.springframework.cloud:spring-cloud-config-server')
}
Here is my bootstrap.properties file:
spring.application.name = myapp
spring.cloud.zookeeper.connectString: localhost:2181
This is the application class:
#SpringBootApplication
#EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
But the application fails to launch and gives an exception:
Error starting Tomcat context.
Exception: org.springframework.beans.factory.BeanCreationException.
Message: Error creating bean with name 'servletEndpointRegistrar'
defined in class path resource
[org/springframework/boot/actuate/autoconfigure/endpoint/web/ServletEndpointManagementContextConfiguration.class]
: Bean instantiation via factory method failed;
nested exception is org.springframework.beans.BeanInstantiationException
: Failed to instantiate [org.springframework.boot.actuate.endpoint.web.ServletEndpointRegistrar]
: Factory method 'servletEndpointRegistrar' threw exception;
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'healthEndpoint'
defined in class path
resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]
: Bean instantiation via factory method failed;
nested exception is org.springframework.beans.BeanInstantiationException
: Failed to instantiate [org.springframework.boot.actuate.health.HealthEndpoint]
: Factory method 'healthEndpoint' threw exception;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException
: Error creating bean with name 'configServerHealthIndicator'
defined in class path resource
[org/springframework/cloud/config/server/config/EnvironmentRepositoryConfiguration.class]
: Unsatisfied dependency expressed through
method 'configServerHealthIndicator' parameter 0;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException
: Error creating bean with name 'org.springframework.cloud.config.server.config.CompositeConfiguration'
: Unsatisfied dependency expressed through method 'setEnvironmentRepos' parameter 0;
nested exception is org.springframework.beans.factory.BeanCreationException
: Error creating bean with name
'defaultEnvironmentRepository' defined in class path
resource [org/springframework/cloud/config/server/config/DefaultRepositoryConfiguration.class]
: Bean instantiation via factory method failed;
nested exception is org.springframework.beans.BeanInstantiationException
: Failed to instantiate
[org.springframework.cloud.config.server.environment.MultipleJGitEnvironmentRepository]
: Factory method 'defaultEnvironmentRepository' threw exception;
nested exception is java.lang.NullPointerException
What could be the reason behind this? Seems like it is searching for git repository even after specifying zookeeper.
I'm trying to install mongeez plugin and I get the following error,
I have included ,
plugins{
..
compile ':mongeez:0.2.3'
..
}
in the BuildConfig.groovy
Error creating bean with name 'grails.mongeez.MongeezController': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongeez': Cannot resolve reference to bean 'mongo' while setting bean property 'mongo'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'mongo' is defined
.....
.....
....
....
.....
.....
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongeez': Cannot resolve reference to bean 'mongo' while setting bean property 'mongo'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'mongo' is defined
I'm able to install the plugin by adding mongeez as a dependency instead of a plugin.
BuildConfig.groovy
dependencies {
......
.....
compile 'org.mongeez:mongeez:0.9.6'
.....
......
}
Contents of migrations folder(Change logs)
I have grails-app/migrations/test.js
grails-app/migrations/mongeez.xml
This is my mongeez.xml
<changeFiles>
<file path="test.js"/>
</changeFiles>
This is my test.js
db.movie.insert({
"name":"tutorials point"
});
I have written a groovy script as follows,
import com.mongodb.Mongo
import org.springframework.core.io.ClassPathResource
import org.mongeez.Mongeez
includeTargets << grailsScript("_GrailsInit")
target(updateMongo: "Update the mongo DB!") {
println "The script is about to run"
def host = 'localhost'
def port = 27017
def databaseName = 'reporting'
Mongeez mongeez = new Mongeez()
mongeez.setFile(new ClassPathResource("/migrations/mongeez.xml"))
mongeez.setMongo(new Mongo(host, port))
mongeez.setDbName(databaseName)
mongeez.process()
println "The script just ran"
}
setDefaultTarget(updateMongo)
When I run the above script both the print statements are getting executed.
A mongeez collection also got created in my reporting db but the contents of test.js(movie collection) is not reflected in my mongodb , i.e movie collection is not getting created.
Please suggest me if I'm missing something.
Got mongeez plugin working by replacing ClassPathResource with FileSystemResource.
Mongeez mongeez = new Mongeez()
mongeez.setFile(new FileSystemResource(path)) // Give the path to your mongeez.xml
mongeez.setMongo(new Mongo(properties.host, properties.port))
mongeez.setDbName(databaseName)
mongeez.process()
i dont know ahy i got this error when i wanna test my app using spring beans this the main test :
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext context=
new ClassPathXmlApplicationContext(new String[]
{"applicationContext.xml"});
IBanqueMetier metier=(IBanqueMetier) context.getBean("metier");
metier.addClient(new Client("C1", "AD1"));
metier.addClient(new Client("C2", "AD2"));
}
and this the 1rst error :
Exception in thread "main"org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'dao': Injection of persistence dependencies
failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Illegal attempt to map a non collection as a #OneToMany, #ManyToMany or #CollectionOfElements: org.gestion.bp.entities.Compte.employe
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:342)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
With Spring exceptions, it's important that you read the whole message up to the very end. I usually split the message into several lines to make it more readable:
Exception in thread "main"org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'dao': Injection of persistence dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'entityManagerFactory' defined in class path resource [applicationContext.xml]:
Invocation of init method failed;
nested exception is org.hibernate.AnnotationException:
Illegal attempt to map a non collection as a #OneToMany, #ManyToMany or #CollectionOfElements:
org.gestion.bp.entities.Compte.employe
As you can see, you made an error in the definition of the field org.gestion.bp.entities.Compte.employe