When I add Actuator dependency it gives UnsatisfiedDependencyException - java

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

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

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

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

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

Spring add references to different projects

Newbie question: I have different projects that im doing in STS, all opened. For example i have myproject_mvc and myproject_datamodel.
In myproject_mvc are the views and controllers.
In myproject_datamodel are the class of the Model like: Person, Car, etc.
the project_datamodel is used in other projects too. So i want to add a reference and use it like if that class where in myproject_mvc.
I added doing right click over myproject_mvc Build Path/Configure Build Path/Projects and i added the porject there, but when i run the application i recieve this error message:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0': Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: solvian_datamodel/Vehicle
Maybe i have to register that dependency in pom.xml or other file that i dont know.
You need to configure Web Deployment Assembly in STS.
Try following:
Go to myproject_mvc's properties -> Deployment Assembly.
Click Add button.
Add dependency projects.
Hope this helps.

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