OAUTH2 transaction manager grails mongodb - spring

I am working on an app that uses a mongodb, is built in grails, and is using oauth2 as an authentication service. I have the services and controller built out (I think) and am trying to run my server to manually test connectivity with google. I am running into an "Error creating bean with name transactionManagerPostProcessor..." that I can't solve. I know I need to either install a plug-in that takes care of the "transactions" for me, or create a bean in resources.groovy.
I have done a lot of researching and reading up on this, but I have been at a dead end for the last couple days trying to get this implemented. Can someone tell me how to solve this problem, create the correct bean, or install the correct plug-in? Partial stack trace below, with link to full stack trace:
|Loading Grails 2.3.4
|Configuring classpath
.
|Environment set to development
.................................
|Packaging Grails application
............................................
|Running Grails application
Configuring Spring Security Core ...
... finished configuring Spring Security Core
Error |
2014-01-16 09:30:52,840 [localhost-startStop-1] ERROR context.GrailsContextLoader - Error initializing the application: Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoTransactionManager': Cannot resolve reference to bean 'mongoDatastore' while setting bean property 'datastore'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoDatastore': Cannot resolve reference to bean 'mongoMappingContext' while setting bean property 'mappingContext'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoMappingContext': FactoryBean threw exception on object creation; nested exception is java.lang.NoClassDefFoundError: org/springframework/format/datetime/DateFormatterRegistrar
Full stacktrace can be found at: http://pastebin.com/5mW5uwtu
I have tried adding
static transaction = 'mongo'
to the service that calls google to authenticate the user. This is a solution I found on a similiar question on stackoverflow. I have also fiddled with different plugins and dependencies in my build-config. Not sure what else there is to try.
Thank you!

To work around this issue with some plugins, I've added the following to the beans block in resources.groovy
// some plugins need a transaction manager,
// although the mongo one isn't properly transactional
// and could produce unexpected results in some cases.
springConfig.addAlias('transactionManager', 'mongoTransactionManager')
However, the specific error you're seeing relates to the Spring class DateFormatterRegistrar not being found. Are you using an up-to-date version of Grails (e.g. 2.3+) and the mongo plugin? Have you added the mongo plugin as a compile plugin dependency in BuildConfig?:
compile ":mongodb:1.3.3"

Related

How to run all JUnit tests in a Spring Boot Application?

I am trying to generate a code coverage report of my Spring Boot application but am facing some issues with running all the existing unit tests at once.
Currently, I have quite a number of test classes (~20+) and when run individually on IntelliJ, they work fine with no issues or exceptions. However, when I create a JUnit configuration that builds and runs from "All in directory", 70-80% of my test classes fail and throws the same exception IllegalStateException: Failed to load ApplicationContext.
The stack trace seem to suggest some issues with the dependency injection. It follows this pattern:
java.lang.IllegalStateException: Failed to load ApplicationContext ...
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'foo': Injection of resource dependencies
failed; org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'bar': FactoryBean threw exception on object
creation org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'foobar': Initialization of bean failed;
I'm not sure if this has something to do with the #SpringBootTest annotation which is repeated throughout all the test classes (I'm thinking maybe it's spinning up the application / context multiple times resulting in this issue?)
I think perhaps I'm doing it wrongly or have configured some annotations wrongly? How can I run all the JUnit test classes on IntelliJ?
P.S Sorry for the lack of context as it's not a personal project and I have to hide some information, and also I'm very new to SpringBoot and JUnit.

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

Upgrade Spring 4.3.3 to Spring 5.0.1 Getting Errors

Can some one help me on this as I am getting this error and tried all the possible ways from my end and also tried checking with different options.
My application runs without error in Spring 4.3.3 where as when I upgrade to Spring 5.0.1 it gives the following error:
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'controllerDependencyBean': Unsatisfied dependency expressed through field 'signUpService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'signUpServiceImpl': Unsatisfied dependency expressed through field 'notifyRepository'; nested exception is org.springframework.beans.factory.BeanCreationException.
All the versions and decencies are cross checked and all are correct and everything is fine, but still I get the above error. I am using Spring Security.

java version "1.8.0_121" Requested bean is currently in creation: Is there an unresolvable circular reference?

When I build and deploy my spring based Application with
java version "1.8.0_112" it compiles and deploys fine.
Also, if i compile with java version "1.8.0_121", and deploy with
java version "1.8.0_112" that works too.
But when I compile and deploy the same Application with
java version "1.8.0_121"
it gives me an error for one service:
Error creating bean with name 'namesServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private a.b.c.backend.services.account.PersonService a.b.c.backend.services.serviceimpl.NamesServiceImpl.personService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'CISExpressionHandler' defined in URL
[jar:file:/usr/local/tomcat7-7/webapps/ServicesApp/WEB-INF/lib/E2Services.jar!/META-INF/spring/applicationContext-security.xml]: Cannot resolve reference to bean 'CISPermissionEvaluator' while setting bean property 'permissionEvaluator'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'CISPermissionEvaluator': Requested bean is currently in creation: Is there an unresolvable circular reference?
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private a.b.c.backend.services.ciscase.CaseService a.b.c.backend.services.security.permissionEvaluators.CaseOwnerPermission.caseService; nested exception is org.springframework.beans.factory.BeanCreationException
One explanation, I could find was:
The exception may or may not occur depends on the creation order of beans. We load them with config something like below in web.xml
(/usr/local/tomcat7-7/webapps/ServicesApp/WEB-INF/web.xml)
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
</context-param>
The XML files will be loaded by XmlWebApplicationContext class and the loading order of files are not guaranteed.
It just loads files from the file system. The problem is here.
There's no problem if the class loads the application context file first, because your beans are already created when they are used for the construction injection of Spring Security. But, if it loads the Spring Security context file first, the circular reference problem occurs, because Spring tries to use your beans in the constructor injection before they had been created.
How to solve the problem?
Force the loading order of the xml files. loading the security context xml file at the end of the application context file by using
<import resource="applicationContext-security.xml">.
Now how this got introduced with JDK version change? I don't have an explanation for that
ref: Splitting applicationContext to multiple files
Q: How do i get the order of beans at deployment ?

Liferay Spring MVC

Has anyone experienced this error?
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.liferay.portal.spring.aop.ServiceBeanAutoProxyCreator#0' defined in class path resource [META-INF/base-spring.xml]: Initialization of bean failed; nested exception is java.lang.IncompatibleClassChangeError: org/springframework/core/LocalVariableTableParameterNameDiscoverer$ParameterNameDiscoveringVisitor
Im using Liferay 6.2 with Spring 4
I wouldn't recommend mixing up other versions of Spring with Liferay than the one supported itself. You can check version of given external plugin by looking it this xml file
https://github.com/liferay/liferay-portal/blob/6.2.x/lib/versions.xml
When you grep this page for 'Spring' occurs, you see 3.0.7 version, which is right one to use with 6.2.X Liferay.

Resources