Spring Boot and #ComponentScan between two jars - java

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

Related

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

Unable to deploy spring boot jar application

I'm developping a spring boot application and it works fine when I run the command:
mvn spring-boot:run
But when deploying jar file with the command:
java -jar target\jarfile.jar
I get the following error:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeResource': Unsatisfied dependency expressed through field 'hrServicesSilo'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'hrServicesSilo': Unsatisfied dependency expressed through field 'employeeService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeService': Unsatisfied dependency expressed through field 'employeeRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.intranet.si.model.hr.Employee
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) ~[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE]
What am I doing wrong?
I can see a repository so I assume you use spring-data. It is strange that it works in maven and not working after packaging so you can try the following:
Check that you use spring-maven-plugin to package your application.
Try to add #EntityScan("com.intranet.si.model.hr") (can also be com.intranet.si.model to recursivly catch all models in the package and sub-packages) to one of your configuration classes or to the application class (the one annotated with #SpringBootApplication). This will notify spring + hibernate where to scan for entities. Also make sure your entity is annotated with #Entity.
I guess that you already have #EnableJpaRespotories in one of your configuration files but make sure it is configured as well.

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

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webSecurityConfig' [duplicate]

This question already has answers here:
Autowiring :expected at least 1 bean which qualifies as autowire candidate for this dependency
(6 answers)
Closed 5 years ago.
I am having this error whenever I run my Spring MVC App. I have been trying to integrate Spring Security but have been unsuccessful at injecting my custom AuthenticationSuccessHandler and so I get the following error pertaining to the application context:
2017-11-07 16:05:03 ERROR ContextLoader:350 - Context initialization failed
org.springframework.beans.factory.UnsatisfiedDependencyException: \
Error creating bean with name 'webSecurityConfig': \
Unsatisfied dependency expressed through field 'auth'; \
nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: \
No qualifying bean of type \
'org.springframework.security.web.authentication.AuthenticationSuccessHandler' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
I have my project on GitHub: https://github.com/riveraadrian571/Staging-Management-System-Modifed-.git . I am using an oracle database and in pom.xml file am using a jdbc driver that I have in my local repository so you may want to change that. I am also using tomcat server to run the app. Thanks in advance.
In WebSecurityConfig you have
#Autowired
AuthenticationSuccessHandler auth;
Which is an Interface. You need to specify any specific implementation from standard:
All Known Implementing Classes:
ForwardAuthenticationSuccessHandler,
SavedRequestAwareAuthenticationSuccessHandler,
SimpleUrlAuthenticationSuccessHandler
#see Spring Docs
Or implement your custom.

Categories