How to autowire a class from a maven dependency? - spring

I currently have a dependency project added in from pom.xml and that has all the config and everything included already. Is it possible to autowire a class from a jar dependency maven?

You can use scanBasePackage in your application.
#SpringBootApplication(scanBasePackages = {"com.myrootpackage"})

Related

javax.enterprise.inject.UnsatisfiedResolutionException:Unsatisfied dependency for type com.test.model.TestService and qualifiers [#Default] in Quarkus

I have created Quarkus project and added a SpringBoot project dependency in build.gradle file, dependency downloaded successfully.
Now when I am trying to inject a class which is available in dependancy, the build is getting failed.
Note: The Class which I am trying to inject does not have #ApplicationScope annotation.
getting below error:
Caused by: javax.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type com.test.model.TestService and qualifiers [#Default]
Look here:
https://quarkus.io/guides/cdi-reference
See the section 1 in the document above about creating the bean archive.
What worked for me is adding references to the dependencies in my application.properties.
quarkus.index-dependency.[name].group-id=[group]
quarkus.index-dependency.[name].artifact-id=[artifact]
For instance, in my project I injected some OpenTracing beans, ended up with:
quarkus.index-dependency.opentracing.group-id=io.opentracing
quarkus.index-dependency.opentracing.artifact-id=opentracing-api

Specify gradle configuration or customize dependencies in bootJar task?

Our application is using EclipseLink. For production artifacts we use static weaving, we have a Gradle task that builds a separate jar that should be included in the Spring Boot fat jar. During development we are not using weaving so we don't have this artifact.
What we would like to do is customize the classpath in the bootJar task so that we include the weaved artifact and exclude the source of the un-weaved module. Prior to 2.0.x of the Spring Boot Gradle plugin this was achieved by specifying a customConfiguration in a task of type bootRepackage, like this:
task singleJar(type: BootRepackage) {
customConfiguration = "weavedRuntime"
}
But this option seems to be missing in the 2.0.x version. Is there any way of overriding the configuration in the new version?
Alternatively we need to modify the classpath, but just for the bootJar task. The normal runtime classpath shoukd include the un-weaved module, but the bootJar classpath should include the weaved artifact and exclude the un-weaved module. Any suggestions on how to do this?

How to exclude spring configuration files with #Configuration for dependency on other project

I have a project say A[In Maven] which has dependency on project B[In Maven]. Now project B got some spring #Configuration[db configuration ] files which gets up when i build and deploy my project A but due to some config properties which I don't have in my project A it shows some error. So how can I exclude that configuration files in project A while using other dependency in project B.
You can add excludeFilters in your component scan to skip the class
#ComponentScan(value = {'your.package.here'}, excludeFilters = #Filter(ConfigurationToIgnore.class))

Spring Boot Gradle plugin - Multi Module fat jar

I have gradle multi-project that looks as follows:
-Project
|
|- common
|- moduleA
|- moduleB
moduleA is Spring Boot app and its gradle build file uses SpringBootGradle plugin. This module depends on common nodule.
moduleB is also Spring Boot app and its gradle build file uses SpringBootGradle plugin. This module depends on moduleA.
After I build my Project with gradle build I expect that moduleA.jar and moduleB.jar exist and they do.
But when I try to run moduleB.jar I get an exception (Spring Boot initialization exception). As I found moduleB.jar contains lib directory with all dependencies from moduleA as well as moduleA.jar (which is packaged with dependencies also).
So what I need is to find a way to add moduleA plain jar as a dependency to my moduleB during the build.
You need to configure module A so that it can be used as a dependency:
bootRepackage {
classifier = 'exec'
}
This will mean that you end up with two jars. One without a classifier that can be used as a dependency, and one with a classifier that is the executable fat jar with dependencies nested inside it.
For Spring Boot 2.3 use
bootJar {
classifier = 'boot' // or whatever
}
The fat jars (for module A and B) will have the name *-boot.jar and module B will include only module-a.jar
See here

Issue with maven + surefire-plugin and Spring #Autowired

I have been breaking my head in getting Spring #Autowired to work inside maven tests. When I run the JUnit tests inside IntellJ (did not try Eclipse) it works. But when I run mvn clean install, the JUnit tests fail with the following error
testApp(com.sample.spring.AppTest): Error creating bean with name 'com.sample.spring.AppTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.sample.spring.AppB com.sample.spring.AppTest.appB; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.sample.spring.AppB] 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)}
I have created a self contained sample project that exhibits this behaviour consistently. I am using Spring 3.1.1. I am sure someone faced the same issue and cracked it. Looking for some pointers regarding this issue.
This is a build path issue:
Working in eclipse I changed your ContextConfiguration to :
#ContextConfiguration(locations = "classpath:applicationContext.xml")
and it runs both with standard eclipse runner and maven(maven test or maven install).
Make sure you have:
src/main/java
src/test/java and
src/test/resources
declared as source folders in your buildpath

Resources