Occasional BeanCreationException in Java Spring Boot project built with Gradle - java

Sometimes I get a BeanCreationException when starting a Java Spring Boot single-JAR application built by Gradle. My investigations show that it depends on the JAR file and how it has been built by Gradle. Most often (but not always) I see the exception when I build the project on a Linux system. But I have never reproduced the issue with a JAR built on Windows or when running from IntelliJ Idea.
I tried to compare the content of a working JAR with the JAR throwing the exception - all the *.class files (including meta and resources) were binary equal, the only difference was in the order the files were stored in the JAR/ZIP archive. I also tried to unpack the failing JAR on Windows and just repack it into a new JAR file (using 7-zip) - the application started without any exceptions. This weird workaround solved the issue, but it's not something I'd like to do after each build on a linux machine.
The exception suggests to check circular references, so I tried replacing #Autowired properties with #Autowired bean constructors to help me to find the problem but that didn't help. The stacktrace does not mention any of my classes, so I don't know what bean could be responsible for the issue. And because of the fact the issue happens only sometimes and is solvable by repacking the JAR file, I'm not sure there are any circular references anyway.
Could you please help me? Any advice or suggestion is welcomed.
JDK: openjdk 8u262
Gradle version 5.6
Spring boot 2.3.1
Exception message:
BeanCreationException: Error creating bean with name 'webConfig':
Invocation of init method failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource
[org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]:
Unsatisfied dependency expressed through method 'requestMappingHandlerAdapter' parameter 0;
nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException:
Error creating bean with name 'mvcContentNegotiationManager':
Requested bean is currently in creation: Is there an unresolvable circular reference?

By default, Spring manages itself bean's lifecycle and then, arranges their initialization order during the startup.
Here a full example of usage #DependsOn annotation to order the bean instanciation.
You can also found more detail in the official Spring documentation

Related

SAP Hybris Assisted Service Module (ASM) - can't generate component

I've installed and tested ASM on my localhost successfully (we use Hybris 1811). Then I wanted to transfer it on our dev server (actually it was only changes in localextensions.xml and regenerated extensionsinfo.xml), so in extensionsinfo.xml of my storefront there is the addon registrated like this:
<requires-extension name="assistedservicestorefront "/>
But it doesn't generate files in views:
[DefaultCMSComponentRendererRegistry] Error processing component tag. currentComponent [AssistedServiceComponentModel (8796814312508#2)] exception:
File [/WEB-INF/views/responsive/cms/assistedservicecomponent.jsp] not found
I've imported the same .impex file as in the manual for installing ASM (from help.hybris.com) and as on the localhost (cms-content.impex to include component and Jsp).
Even though I run update in HAC, I tried to sync content catalog. I checked extensions in HAC, it contains same ASM extensions as on my local machine: assistedservicefacades, assistedserviceservices, assistedservicestorefront.
When I checked the backoffice, the component with that ID is really there and it's in online catalog.
How do I make those files to generate? Or should I somehow override them with my custom views? But I think it would be bad approach.
UPDATE: I also tried to add this entry to project.properties of my storefront:
sikob2cstorefront.additionalWebSpringConfigs.assistedservicestorefront=classpath\:/assistedservicestorefront/web/spring/assistedservicestorefront-web-spring.xml
but then I got the error loading beans:
Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultStorefrontTenantDefaultFilterChainList': Post-processing of FactoryBean's singleton object failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'AssistedservicestorefrontFilterListMergeDirective' defined in class path resource [assistedservicestorefront/web/spring/assistedservicestorefront-web-spring.xml]: Cannot resolve reference to bean 'assistedServiceFilter' while setting add; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'assistedServiceFilter' defined in class path resource [assistedservicestorefront/web/spring/assistedservicestorefront-web-spring.xml]: Cannot resolve reference to bean 'assistedServicePathRestrictionEvaluator' while setting bean property 'assistedServicePathRestrictionEvaluator'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'assistedServicePathRestrictionEvaluator' available
# Install assistedservicestorefront
# Replace Mystorefront with your storefront extension name
ant addoninstall -Daddonnames="assistedservicestorefront" -DaddonStorefront.yacceleratorstorefront="Mystorefront"
I think you have missed installing the assistedservicestorefront addon or if you've already installed then check whether below changes have taken place on your server.
addoninstall command makes below changes
Adds assistedservicestorefront into extensioninfo.xml of your storefront extension
like
<requires-extension name="assistedservicestorefront "/>
Generate a new project.properties file from project.properties.template inside assistedservicestorefront, which has following entries.
Replace yacceleratorstorefront with your storefront extension name in the below property.
#Specifies the location of the spring context file added automatically to the global platform application context.
assistedservicestorefront.application-context=assistedservicestorefront-spring.xml
yacceleratorstorefront.additionalWebSpringConfigs.assistedservicestorefront=classpath:/assistedservicestorefront/web/spring/assistedservicestorefront-web-spring.xml
assistedservicestorefront.javascript.paths.mobile=/responsive/common/js/assistedservicestorefront.js;/responsive/common/js/jquery.tablesorter.pager.js;/responsive/common/js/jquery.tablesorter.min.js;/responsive/common/js/Chart.min.js;/responsive/common/js/asm.storefinder.js
assistedservicestorefront.javascript.paths.responsive=/responsive/common/js/assistedservicestorefront.js;/responsive/common/js/jquery.tablesorter.pager.js;/responsive/common/js/jquery.tablesorter.min.js;/responsive/common/js/Chart.min.js;/responsive/common/js/asm.storefinder.js
assistedservicestorefront.css.paths.mobile=/responsive/common/css/assistedservicestorefront.css;/responsive/common/css/storeFinder.css;/responsive/common/css/customer360.css
assistedservicestorefront.css.paths.responsive=/responsive/common/css/assistedservicestorefront.css;/responsive/common/css/storeFinder.css;/responsive/common/css/customer360.css
assistedservicestorefront.redirect.customer_and_cart=/cart
assistedservicestorefront.redirect.customer_only=/my-account
assistedservicestorefront.redirect.error=/
assistedservicestorefront.redirect.order=/my-account/order/%s
assistedservicestorefront.deeplink.link=/assisted-service/emulate
cscokpit.assistedservice.deeplink=true
assistedservicestorefront.profile.cookie.name=profile.tracking.pause
#AIF AJAX call timeout in milliseconds
assistedservicestorefront.aif.timeout=7000
So if you don't want to run addoninstall command in each environment then you can manually do these two changes. You can refer to the generated project.properties file inside your addon and copy all those properties to your storefront's project.properties or local.properteis file and commit the changes.

When I add Actuator dependency it gives UnsatisfiedDependencyException

I have a springboot application. When I add spring-boot-starter-actuator dependency to my pom it gives UnsatisfiedDependencyException.
Without this dependency, application is running. When I put it, application not running.
Can anyone provide a solution for this?
Error:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'servoMetricCollector' defined in class path resource

initializing AWS with Springboot

I am trying to initialize and build a Springboot Application that uses AWS dependencies on Net Beans, however, I am having a problem building the project. After resolving the errors after creating the project I am getting an error when I try to build. The message I get is
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cloud.aws.context.support.io.ResourceLoaderBeanPostProcessor#0': Cannot resolve reference to bean 'amazonS3' while setting constructor argument;
I found some previous answers saying to put cloud.aws.stack.auto=false in my application.properties file, however, I seem to be getting the same problem after I clean and build again.
If you are building/ testing or running locally (as opposed to EC2) then you need to have a region property such as in application.properties assuming you use the default profile for build and test.
cloud.aws.region.static=us-east-1

Hibernate 3.6.10 NoSuchMethodError

I'm having the following issue with my hibernate 3.6.10 project:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testSessionFactory' defined in class path resource [db.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.OneToMany.orphanRemoval()Z
Reading some of the issues here on stack overflow, it said I was using the wrong persistence API. So I downloaded the matching hibernate (I've been using the Spring deployment) and copied out the hibernate-jpa-2.0-api-1.0.1.Final.jar which comes with the hibernate distrib, only to get the self-same message.
Is this telling me that OneToMany is not supported by Hibernate 3.6.10?
It tells your that you have a JPA 1.0 API jar somewhere in your classpath. Such a problem cannot be reliably solved by adding proper JPA 2.0 jar to the classpath, you need to find and remove the offending jar before.

Spring Configuration file error

I am trying to run a simple program using a spring configuration file. In the configuration I am creating a bean for the JMS template. When I run it from eclipse everything works perfect but if I try to run it from the command line I get the following error.
Error creatign bean with name JMSTEMPLATE definded in class path resource [config.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: javax/jms/JMSException
Does anyone know why this could be? I've double checked my class path and it is fine.
You are forgetting to include the jms.jar in your classpath.
Are you including JMS jars in your classpath? This error suggests that they are missing at runtime.

Categories