Can't load context from within IntelliJ when including external jar - java

I'm working on a multi-module Spring Boot project that can use non-spring plugins. Plugins are loaded using java.util.ServiceLoader. They may use spring beans provided by a ContextBridge implementing org.springframework.context.ApplicationContextAware. The end result looks like this:
app-folder/
plugins/
myplugin.jar
myapplication.jar
launcher-script.sh
The application and the plugins work as intended when started using the shell script:
java -Dloader.path=.,plugins -jar myapplication.jar "$#"
However, I can't figure out how to configure IntelliJ to start the application AND find the plugins so I would be able to debug more comfortably.
I've tried the following approaches
Adding the -Dloader.path config to the launch configuration doesn't seem to work. I have tried both Appliaction and Spring Boot launch configurations. This didn't seem to do anything. It just loads the main application but no plugins.
Adding the plugins through gradle:
compile fileTree(dir: 'dist/plugins', include: '**/*.jar')
This does load the plugin but fails to load the context which I take from the error message I get:
s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myServiceImpl': Unsatisfied dependency expressed through method 'setMyService' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.myapp.MyService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
So I added bean dependencies to the code in order to force a certain order in which beans get initialized:
Data-Access Module
interface Myservice {
...
}
#Service
#Qualifier("MyServiceBean")
class MyServiceImpl implements MyService {
...
}
ContextBridge inside Plugin
#Component
#DependsOn("MyService)
public class ContextBridgeImpl implements ContextBridge, ApplicationContextAware {
...
}
Then I get:
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'MyService' available
What confuses me is the fact that everythings works when started from the console. Why not inside IntelliJ?
Edit
Screen shots showing launch configurations

Related

Occasional BeanCreationException in Java Spring Boot project built with Gradle

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

Java Spring Boot Circular Dependency on one environment

I have a spring boot application which runs well on my local, UAT, and Prod environments but when I deploy it in a staging environment, it gives the following error:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'Bean 1': Unsatisfied dependency expressed through field 'Class 1'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'Bean2 of type Class1': Bean with the name 'Bean2' has been injected into other beans [Bean 3] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.
I am not sure if it's related to the build process or what. I have checked the code, there are no circular dependencies. When I annotate Bean2 with #Lazy on Class3, it works well. But I am not able to figure out why it works on other environments and this annotation is needed only for the Staging.

Error while Autowiring Tracer in Service Spring Boot

I have autowired org.springframework.cloud.sleuth.Tracer in a service but getting the following error while building the project (mvn clean install). Although The applications runs through IDE and code works perfectly, but I need a way to remove the error while compiling the project.
Unsatisfied dependency expressed through constructor parameter 3;
nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type 'org.springframework.cloud.sleuth.Tracer'
available: expected at least 1 bean which qualifies as autowire
candidate. Dependency annotations: {}
*************************** APPLICATION FAILED TO START ***************************
Description:
Parameter 3 of constructor in
com.mmt.corporate.payments.PaymentService required a bean of type
'org.springframework.cloud.sleuth.Tracer' that could not be found.
Action:
Consider defining a bean of type
'org.springframework.cloud.sleuth.Tracer' in your configuration.
have included org.springframework.cloud.sleuth this way in pom.xml :
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
The Sleuth's Tracer was present in version 1.3.x. Starting from version 2.x there's no more Tracer from Sleuth. There's one from Brave. Can you please upgrade to the latest version?

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

Spring Boot and #ComponentScan between two jars

I have 2 projects.
One is a DAL project that does CRUD operations on a neo4j DB using spring neo4j APIs . This project is packaged as a jar and included in project #2.
Project #2 Is a Spring restful services project that uses spring boot to package and create an executable jar that runes on an embedded tomcat server.
When trying to run my executable jar that Spring boot has created for me I keep getting this exception.
expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
Based off of my reading if I am using #ComponentScan I can give the annotation directories to look in. So I give it the base dir for my services project. And I give it the base dir for my included DAL.jar but still no luck here is what the annotation looks like.
Extracted from comments:
Component scan declaration
#ComponentScan({"com.foo.dal.*","com.foo.notification.*"})
Stacktrace:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pushCommandsController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.teradata.dal.example.PushRepository com.teradata.notification.rest.controller.PushCommandsController.repository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.teradata.dal.example.PushRepository] found for dependency:
expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
UPDATE:
based off of #chrylis answer:
Made change to #ComponenetScan
#ComponentScan({"com.teradata.notification","com.teradata.dal"})
running in to:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
at java.lang.Thread.run(Thread.java:744)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration' is defined
MORE DETAIL ON THE DAL PROJECT AND THE SERVICE PROJECT:
DAL PROJECT:
Services Project:
The argument to #ComponentScan is a package name, and those strings aren't valid packages. Drop the .* from them; Spring scans subpackages automatically.
Had this same issue for a short while, then #EntityScan did the trick for me, just as adviced here - Spring Boot w/ JPA: move #Entity to different package.
Hope that helps

Categories