Holders.config is null during grails run-war - java

Recently did a log4j upgrade on grails 2.5.4 application. Following dependencies pertaining to v2.17.1 have been added:
log4j-api
log4j-core
log4j-1.2-api
log4j-slf4j-impl
I've excluded the following dependencies from the global level:
log4j
grails-plugin-log4j
jcl-over-slf4j
The application boots up without any issues on doing grails run-app. But when I try to run the war by grails run-war or deploy the war using tomcat, I keep getting:
Error creating bean with name 'grailsApplication' defined in ServletContext
...
Caused by: java.lang.ExceptionInInitializerError
...
Caused by: java.lang.NullPointerException: Cannot get property 'xyz' on null object
This null object is Holders.config
This issue occurs during static property initialization. The class looks like:
class Test {
public static Logger log = LoggerFactory.getLogger(this)
private static final String SOURCE_URL = Holders.config.xyz.abc
...
}
Another class had the same issue during the initialization of variable using Holders.config in static block.
Strangely, this issue is only for classes under grails-app/utils folder because I've another class under src/groovy where its variables are defined in a similar way but there's no issue with it during the war file execution.
There's no change in code except for the dependencies added/removed as mentioned above.
I also tried using applicationContext.getBean('grailsApplication).config.xyz.abc but it said Could not find ApplicationContext, configure Grails correctly first.
Can someone please suggest what could be the root cause or guide how I can fix or maybe initialize these static values.

Related

jhipster java tests failing - application properties

I have generated a jhipster monolithic app. I have created a class to connect with the AWS S3 and upload a file there. I defined the properties in .yml file. Here everything works fine.
When I am trying to run the provided tests, most of them are failing with the following error:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 's3AutoConfig': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'aws.endpoint.url' in value "${aws.endpoint.url}"
S3AutoConfig is the class which uses the properties.
I checked jhipster's documenation and several posts, like the one below:
Adding applicationproperties in Jhipster
which mention that you should provide the properties in the ApplicationProperties class (seems a bit redundant).
I defined the properties also in the java class, but the tests are still failing with the same error above.
How should I define the properties, so they are picked up by the tests? Is it necessary to provide them also in the java class as some posts suggest?
Your implementation cannot work because you are defining Aws class within ApplicationProperties which means that your AWS properties will be prefixed by application, so for example application.aws.endpoint.url which does not match your application*.yml structure and this is why you get this error.
You should extract Aws class and its inner classes to its own file (Aws.java) and use prefix "aws".
Also, it would probably better named as AwsProperties.
#ConfigurationProperties(prefix = "aws", ignoreUnknownFields = false)
public class Aws {
Then the second point about tests is that they are using a different classpath than main class so you should ensure that you define these properties also in src/test/resources/config/application.yml

Spring Boot 2 Upgrade not able to resolve class name as property

We have an application where one of our internal libraries has defined a bean like this
<bean id="myBean" class="${myBean.type}"/>
We have a Spring Cloud Config Server which feeds properties to this application on startup, which also contains the property myBean.type. This setup is currently working fine with no issues. I then made the following change to my pom
Earlier
<spring.boot.version>1.5.16.RELEASE</spring.boot.version>
<spring.cloud.version>Edgware.RELEASE</spring.cloud.version>
<spring.version>4.3.19.RELEASE</spring.version>
Now
<spring.boot.version>2.0.9.RELEASE</spring.boot.version>
<spring.version>5.0.13.RELEASE</spring.version>
<spring.cloud.version>Finchley.SR2</spring.cloud.version>
Then I started getting this error on startup
An attempt was made to call the method org.springframework.beans.factory.support.BeanDefinitionBuilder.addConstructorArg(Ljava/lang/Object;)Lorg/springframework/beans/factory/support/BeanDefinitionBuilder; but it does not exist. Its class, org.springframework.beans.factory.support.BeanDefinitionBuilder, is available from the following locations:
jar:file:/I:/Library/MavenRepository/org/springframework/spring-beans/5.0.13.RELEASE/spring-beans-5.0.13.RELEASE.jar!/org/springframework/beans/factory/support/BeanDefinitionBuilder.class
It was loaded from the following location:
file:/I:/Library/MavenRepository/org/springframework/spring-beans/5.0.13.RELEASE/spring-beans-5.0.13.RELEASE.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of org.springframework.beans.factory.support.BeanDefinitionBuilder
Looking at this, I excluded the artifact org.apache.cxf:cxf-api:jar:2.7.18:compile from all the jars that depended on it & upgraded cxf jars versions to 3.2.5. Now the startup is going ahead but it is giving me the following error:
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [${myBean.type}] for bean with name 'myBean' defined in class path resource [xyz.xml]; nested exception is java.lang.ClassNotFoundException: ${myBean.type}
I am not sure if removal of cxf-api is causing this issue, or upgrade of Spring Boot 2, or is it something else that is going wrong here!

ConflictingBeanDefinitionException even after deleting a class

I am developing a simple maven + spring application and i am getting the following error. It says two of my classes have a conflict. so i deleted the second class but i am still getting the same error. I tried restarting the server but it still says my class exists. Can somebody help?
Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.nibm.config.RootConfig]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'employeeController' for bean class [com.nibm.hibernate.controller.EmployeeController] conflicts with existing, non-compatible bean definition of same name and class [com.nibm.controller.EmployeeController]
I was able to solve a similar problem by using IntelliJ's function "Rebuild".
The reason was an orphan .class file after the corresponding .java file had already been deleted.
You get the exception because you have two spring beans of the same class.
This exception is thrown by
org.springframework.context.annotation.ClassPathBeanDefinitionScanner#isCompatible
And looking at that implementation it looks as if you create a bean of type EmployeeController in your RootConfig and additionally by ComponentScan.
To fix the problem remove the bean from the RootConfig or change your ComponentScan, so this bean is not found by it.
You can set a breakpoint in the constructor of EmployeeController. From the stack you can get more information about how and why the bean is created.
I faced the same problem and it was because class with same name exist at two locations as mentioned in the Exception itself which are conflicting and after removing one issue got fixed.
I was getting this same ConflictingBeanDefinitionException..."conflicts with existing, non-compatible bean definition of same name and class" when running JUnit tests with #RunWith(SpringRunner.class)/ #SpringBootTest from inside Intellij.
Execution via gradle:build of the same tests were running fine.
This began to happen after I had refactored the packaging of several #Components which lead me to believe something was holding a reference to the class under it's previous package name.
No amount of gradle build/clean would seem to clear it.
Doing a Build -> Rebuild Project in IntelliJ was what cleared this issue for me.

ambiguous mapping while deploying

I'm using Jenkins 1.6.20 (Git Client Plugin 1.18.0, Git Plugin 2.4.0) to get the Java application code from bitbucket.org and deploy it to Apache Tomcat 8.0.23.
The error appears while deploying and looks like:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/spr$
public java.util.List by.ipps.accounting.ws.PositionWS.getEmployeePost(java.lang.Long)
to {[/positionListJson/{id}],methods=[GET],params=[],headers=[],consumes=[],produces=[application/json],custom=[]}: There is already 'resourceWS' bean method
public by.ipps.accounting.model.Employee.EmployeePost by.ipps.accounting.ws.ResourceWS.getEmployeePost(java.lang.Long) mapped.
bla-bla-bla ... so many errors ...
Caused by: java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'positionWS' bean method
public java.util.List by.ipps.accounting.ws.PositionWS.getEmployeePost(java.lang.Long)
to {[/positionListJson/{id}],methods=[GET],params=[],headers=[],consumes=[],produces=[application/json],custom=[]}: There is already 'resourceWS' bean method
public by.ipps.accounting.model.Employee.EmployeePost by.ipps.accounting.ws.ResourceWS.getEmployeePost(java.lang.Long) mapped.
The problem is that the class (with annotation #Controller) PositionWS with method getEmployeePost was renamed to ResourceWS a week ago, so exists no more, so I should not get this error.
To fix this I have to create a blank PositionWS controller (with no methods in it), commit & push that to bitbucket (and delete (cus i really don't need it) later and commit & push).
It seems to be like a bug in any of the applications I use. I can't find out in which app there is a bug to report it. Tell me please, if anyone faced such problems.
The heart of the issue was in incorrect configuration of Jenkins, it was my fault.
When I was configuring Jenkins I set maven goal as "install", but it must be "clean install". According to this Jenkins never deleted old files and kept them, so got a lot of issues of different kinds and with different log messages.
Due to Jenkins working specialty it downloads project files and try to assemble it on path /var/lib/jenkins/jobs/<projectName>/workspace/target/.
So I've drop the data in this folder and afterwards set maven goal to "clean install" and that fixed the issue.

NoClassDefFoundError Using Hibernate

I'm using Spring 3.1.1.RELEASE and Hibernate 4.1.10.FINAL in a Building Block on Blackboard Learn and getting the following exception:
java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.cfg.PropertyContainer
This seems to be a classpath issue, and so I did some digging and added the suggested libraries from this post but the same error occurs.
So my classpath now contains the necessary dependencies, but I am still getting the exception. What are some additional points I can look at to identify and resolve this issue?
Edit: I've verified the jboss-logging JAR is in my classpath.
Edit: Requested Stack Trace: https://gist.github.com/whargrove/79cbc9c5bd65217e3da3
After restarting Tomcat and re-deploying my WAR the following exceptions are observed in the Tomcat logs:
java.security.AccessControlException: access denied ("java.util.PropertyPermission" "jboss.i18n.generate-proxies" "write")
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mySessionFactory' defined in ServletContext resource [/WEB-INF/config/spring.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.cfg.PropertyContainer
(Full stack trace available in gist link above.)
The message
java.lang.NoClassDefFoundError: Could not initialize class SomeClass
means that the JVM has already tried and failed to perform static initialization on the class SomeClass. There may well be another error which happened earlier on when the JVM tried to load the class the first time.
Taking a look at the source of the PropertyContainer class, the only static intialization is a static initializer that sets a system property, and a line that initializes a logger for the class. Setting a system property will should not cause a problem, so my guess is that the logger class used is missing from your classpath.
The logger class used is org.jboss.logging.Logger. A quick Google for this class suggested a jar named jboss-logging.jar. Try getting a copy of that and adding it to your classpath.
(Incidentally, if you can't find a previous error before the 'Could not initialize class' error, that may well be because the missing JAR affects logging. Logging is something applications tend to assume is always working and can be used everywhere. The exception you are seeing might have been thrown from within a finally block that tried to do some logging when the corresponding try block also tried to do some logging but threw an exception. An exception thrown from within a finally block replaces any exception that had previously been thrown.)
EDIT in response to the stacktrace: I can now see that I was wrong about setting a system property not being the problem! I don't know the first thing about Blackboard Learn, but it is possible that it or something else has tightened up the security in your application and hence caused the above problem.
It does however confirm my belief that the real cause of the problem was before the Could not initialize class message.

Categories

Resources