How to auto wire spring data repository in another spring based project (without spring data context) - spring

I have two spring based projects, project-a with spring data and project-b simple spring based application with out spring data, project a is referenced in project b. In project b application context, i couldn't able to scan the spring data repositories present in project a using the annotation <jpa:repositories base-package="com.projecta"></jpa:repositories> it throws me below error.
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.3.v20160428-59c81c5): org.eclipse.persistence.exceptions.DatabaseException
[junit] Internal Exception: java.sql.SQLRecoverableException: IO Error: Connect identifier was empty.
Let me know if you have ever come across this.

Related

How to use both reactive and non reactive MongoDB template on Webflux Application

Currently, I'm developing a springboot webflux app for my project, where I use reactive-mongo-template and other reactive implementations. Now I have a legacy spring boot application that is implemented by WebMvc ( here i dont use the real springboot app. just using the core services implemented with mongoDB configurations) and it's using (normal) mongo-template for its implemented services. In my webflux app I'm trying to use the old services defined in the legacy application.
For that i tried importing the legacy app using component scan (com.a.b.cservice.core.configuration) and got some error. The problem is it wont detect the application correctly and the non-reactive mongo template.
Does the spring support only 1 mongo connection? These are the error logs i got;
[main] WARN o.s.b.w.r.c.AnnotationConfigReactiveWebServerApplicationContext - [] - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mapperDAOSpringConfigurations': Unsatisfied dependency expressed through field 'mongoTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoTemplate' defined in class path resource
[com/a/b/cservice/core/configuration/MongoConfigurations.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.core.MongoTemplate]: Factory method 'mongoTemplate' threw exception; nested exception is java.lang.NoSuchMethodError: com.mongodb.connection.ClusterSettings.getDescription()Ljava/lang/String;
***************************
APPLICATION FAILED TO START
***************************
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
com.mongodb.connection.DefaultClusterFactory.createCluster(DefaultClusterFactory.java:182)
The following method did not exist:
com.mongodb.connection.ClusterSettings.getDescription()Ljava/lang/String;
The method's class, com.mongodb.connection.ClusterSettings, is available from the following locations:
jar:file:/C:/Users/abc/.m2/repository/org/mongodb/mongodb-driver-core/4.0.5/mongodb-driver-core-4.0.5.jar!/com/mongodb/connection/ClusterSettings.class
jar:file:/C:/Users/abc/.m2/repository/org/mongodb/mongo-java-driver/3.12.7/mongo-java-driver-3.12.7.jar!/com/mongodb/connection/ClusterSettings.class
The class hierarchy was loaded from the following locations:
com.mongodb.connection.ClusterSettings: file:/C:/Users/abc/.m2/repository/org/mongodb/mongodb-driver-core/4.0.5/mongodb-driver-core-4.0.5.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of com.mongodb.connection.ClusterSettings
Are there any workaround to work the legacy app with the webflux application with mongoDb? Or do i need to keep it as a seperate micro-service and call the relevent endpoints?
after a few works and searches, I found the issue is not related to Webflux. purely a mongo client collision between legacy application and webflux drivers. so I exclude the drivers from legacy and imported the spring-boot-starter-data-mongodb along with the reactive spring-boot-starter-data-mongodb-reactive version. (both are using the same version 2.3.5.RELEASE).
ps: found the same issue on here Spring boot 2.0.5.RELEASE and mongo 4.0 connection issues

Spring Boot application cannot join Coherence cluster on server: Join request was aborted

I have developed a small REST API application with Spring Boot after another sample application with classic Spring XML configuration. It imports context from a library with a Coherence cache set up.
When I developed it locally, running the Spring Boot app with Intellij Idea runner, there were no issues, the application worked fine and the endpoint worked connecting to that cache.
When I deployed the application to the Unix server, I am getting the following error during the application startup:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'coreSearchSync' defined in class path resource [core-search-core-context.xml]: Cannot resolve reference to bean 'corePersistenceManager' while setting bean property 'corePersistenceManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'corePersistenceManager': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ' coreSearchResultCache' defined in class path resource [core-search-coherence-context.xml]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.tangosol.net.NamedCache]: Factory method 'getCache' threw exception; nested exception is (Wrapped: Failed to start Service "Management" (ServiceState=SERVICE_STOPPED)) java.lang.RuntimeException: Join request was aborted
The application on the server is using the application.properties but they are the same as in the application.yaml locally.
I am not sure where to continue the investigation...
The problem was solved by passing JVM arguments to the script to start the application:
-Dtangosol.coherence.clusterport=<port> -Dtangosol.coherence.site=<site> -Dtangosol.coherence.clusteraddress=<cluster_ip_ddress> -Dtangosol.coherence.cluster=<cluster_name>
It appears that these properties are not applied from the application properties because starting the coherence cluster works on the system level and/or starts before the properties are initialized. Or those properties are not visible to the coherence.

How to avoid spring application failure due to BeanCreationException caused by third party package

I am using a third-party package in my spring boot app. This one to be specific. This package has a method that creates a bean when the application starts. The bean is only created successfully if this package is able to talk to nats service. In most cases, this all works fine. The remote server is available, the bean is created, my spring boot apps boots up correctly.
But there are some edge cases when remote nats servers might not be up. This causes my spring boot app to fail to start as the above function throws BeanCreationException
How can I avoid this i.e avoid my spring app boot failure due to this exception when the nats server is not up?
Note: It is fine for my spring app/business logic to be up and running if this bean (connections to nats service) is not available.
Specific Exception :
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'natsConnection' defined in class path resource [io/nats/spring/boot/autoconfigure/NatsAutoConfiguration.class]:
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.nats.client.Connection]:
Factory method 'natsConnection' threw exception; nested exception is java.io.IOException: Unable to connect to NATS server.\n\tat org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:655)\n\tat org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:635)\n\tat org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1336)\n\tat org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1176)\n\tat org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556)\n\tat org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516)\n\tat org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324)\n\tat org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)\n\tat org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)\n\tat org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)\n\tat org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897)\n\tat org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879)\n\tat
The bean natsConnection is created by auto-configuration and annotated with #ConditionalOnMissingBean. Such beans are instantiated after your own beans.
You can create your own bean of this type, io.nats.client.Connection, and register it as natsConnection. This bean should have no own logic. When called for the first time, it should create a worker instance via NatsAutoConfiguration.natsConnection(...) and then delegate all calls to it.
Because of #ConditionalOnMissingBean the default bean from the NATS library will not be instantiated and only your bean will be created.

Issue with spring cloud task mutiple datasources

I configured two data sources in my app following the guide of spring example:https://github.com/spring-cloud/spring-cloud-task/blob/master/spring-cloud-task-samples/multiple-datasources.
The spring boot version I use is :2.0.0.RELEASE
The spring.cloud.task.version I use is :1.2.2.RELEASE.
This application works fine in my local computer, But when deploy to the to AWS, I got the following error with the defination of class:CustomTaskConfigurer.java.
which defined the same as here:https://github.com/spring-cloud/spring-cloud-task/blob/master/spring-cloud-task-samples/multiple-datasources/src/main/java/io/spring/configuration/CustomTaskConfigurer.java
The error message is like below:
exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cloud.task.configuration.SimpleTaskConfiguration': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customTaskConfigurer' defined in file [/home/vcap/app/BOOT-INF/classes/com/xxx/configuration/CustomTaskConfigurer.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.xxx.configuration.CustomTaskConfigurer$$EnhancerBySpringCGLIB$$bc80cd46]: Constructor threw exception; nested exception is java.lang.IllegalStateException: Unable to create a TaskExecutionDao.
The root cause for this error is that when I developed the application locally I configure a local datasource bean for postgresql like below:
#Bean
#Primary
#ConfigurationProperties("spring.datasource")
public HikariDataSource sourceDataSource() {
return DataSourceBuilder.create().type(HikariDataSource.class).build();
}
This bean read the properties in the application.properties file which identify the username and password url for the local postgres.
When This application deloyed to cloud,It will connect to the cloud data base instead of the local databases, which means the url,username,and password are no longer correct.
After add the configuration for cloud, this error disppeared.
But this exception stack trace only tell you that it is unable to create taskExecutionDao, It is really hard for the user to fix the issue when see such a error message
If it is a multiple datasource issue, you can try marking it as #Primary. Providing a better stack trace is helpful.

Spring data jpa repository integration test using JNDI datasource

We are using JNDI datasource for spring data JPA repository datasource.
While writing integration tests, tried to create the JNDI datasource as below.
But still the
SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
DriverAdapterCPDS cpds = new DriverAdapterCPDS();
cpds.setDriver("com.mysql.jdbc.Driver");
cpds.setUrl("jdbc:mysql://localhost:3306/test?autoReconnect=true");
cpds.setUser("");
cpds.setPassword("");
SharedPoolDataSource dataSource = new SharedPoolDataSource();
dataSource.setConnectionPoolDataSource(cpds);
dataSource.setMaxActive(10);
dataSource.setMaxWait(50);
builder.bind("jdbc/testDB", dataSource);
builder.activate();
Getting the below exception when running the maven JUnit Tests
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: At least one JPA metamodel must be present!
Caused by: java.lang.IllegalArgumentException: At least one JPA metamodel must be present!

Resources