Spring dependency injection in maven multi modules project - java

I'm building a multi modules maven project with 4 modules. For two of them (the one with the rest controller and the one with the core business logic) I need the power of the dependency injection. But is not working at all. Here my parent pom:
....
<groupId>com.example.taskexecutor</groupId>
<artifactId>taskexecutor</artifactId>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
....
<modules>
<module>taskexecutor-service</module>
<module>taskexecutor-core</module>
<module>taskexecutor-common</module>
<module>taskexecutor-rules</module>
</modules>
.....
Child pom service:
<parent>
<groupId>com.example.taskexecutor</groupId>
<artifactId>taskexecutor</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>taskexecutor-service</artifactId>
.....
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.example.taskexecutor</groupId>
<artifactId>taskexecutor-core</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
Child pom core (the one with the class that i can't inject into the service project)
....
<parent>
<groupId>com.example.taskexecutor</groupId>
<artifactId>taskexecutor</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>taskexecutor-core</artifactId>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
The package inside the two project (service and core) are the same: com.example.application (where is the main class with #SpringBootApplication in the service project), com.example.application.restcontroller (where is the controller in the service project), com.example.application.core (where is the #component that I can't inject in the core project).
Inside the core project I have, under the com.example.application.core a #Component that I would inject inside the class into the service project under the com.example.application.restcontroller package.
Within the same project and under the package of the main class, the dependency injection works absolutely fine. Between two different modules I can't achieve that (nullPointerException).
Any advice will be appreciated

Your description about the packages is hard to understand. But, it most likely is a Component scanning issue. Make sure that you are scanning all the packages. For instance, In the taskexecutor-service, you can do:
#ComponentScan(basePackages = {"com.example.taskexecutor", "com.example.application"})

Related

Spring Maven: Correct the classpath of you application

I am getting the following error when trying to run my spring project:
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
org.hibernate.cfg.annotations.EntityBinder.processComplementaryTableDefinitions(EntityBinder.java:1236)
The following method did not exist:
'javax.persistence.Index[] javax.persistence.Table.indexes()'
The calling method's class, org.hibernate.cfg.annotations.EntityBinder, was loaded from the following location:
jar:file:/C:/Users/bobal/.m2/repository/org/hibernate/hibernate-core/5.4.14.Final/hibernate-core-5.4.14.Final.jar!/org/hibernate/cfg/annotations/EntityBinder.class
The called method's class, javax.persistence.Table, is available from the following locations:
jar:file:/C:/Users/bobal/Documents/Java%20Projects/restService/lib/javax.persistence.jar!/javax/persistence/Table.class
jar:file:/C:/Users/bobal/.m2/repository/jakarta/persistence/jakarta.persistence-api/2.2.3/jakarta.persistence-api-2.2.3.jar!/javax/persistence/Table.class
jar:file:/C:/Users/bobal/.m2/repository/org/hibernate/javax/persistence/hibernate-jpa-2.1-api/1.0.0.Final/hibernate-jpa-2.1-api-1.0.0.Final.jar!/javax/persistence/Table.class
The called method's class hierarchy was loaded from the following locations:
javax.persistence.Table: file:/C:/Users/bobal/Documents/Java%20Projects/restService/lib/javax.persistence.jar
Action:
Correct the classpath of your application so that it contains compatible versions of the classes org.hibernate.cfg.annotations.EntityBinder and javax.persistence.Table
Process finished with exit code 1
I think that this error may be due to having conflicting versions of some dependencies, but I'm not very sure. This is my first ever Spring project.
Here is my UPDATED pom.xml file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>restService</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>restService</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<jdbc.version>8.2.2.jre11</jdbc.version>
<hibernate.version>5.4.14.Final</hibernate.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>17.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>${jdbc.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate-validator.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
If you find anything wrong please feel free to point it out and explain why. Trying to learn as much as I can.
You have multiple version of JPA libraries, that's why you are getting this error.
jakarta.persistence-api-2.2.3.jar and
hibernate-jpa-2.1-api-1.0.0.Final.jar
You can remove hibernate-jpa-2.1-api-1.0.0.Final.jar dependency because it is also present in jakarta.persistence-api-2.2.3.jar.

Axon: Setting up Distributed Command Bus

Edit: Found a solution, I will update this post
Edit 2: The problem was that I defined my Commands and Events inside the src folder of my first and second microservice. The first microservice shot a com.myApplication.PaymentManagementService.commands.ApproveOrderCommand-command, while the command handler in the second microservice expected a com.myApplication.OrderManagementService.commands.ApproveOrderCommand-command.
I fixed the redundancy by lifting the commands and events a few hierarchy levels into a common api-folder that both services can access. Now the second micoservice expects a com.myApplication.api.commands.ApproveOrderCommand, that the first microservice shoots.
That solution may sound trivial to some, but since this is not described anywhere in the documentation and I didn't expect the name of the path to be relevant in handling the commands and events, you have to figure that out first.
Original post:
I'm currently implementing a distributed command bus with spring cloud. I followed the below example exactly, but I'm getting the following issues in both of my (Client-)Microservices.
https://docs.axoniq.io/reference-guide/v/master/extensions/spring-cloud
1. Could not auto-wire. There is more than one Bean of "Registration" type.
Screenshot 1
2. Cannot find a bean with the qualifier "localSegment", even though I'm using Spring Auto Configuration.
Screenshot 2
I can start both the client-applications (this one and the other microservice) nevertheless.
But:
I can't send a command from the first Microservice to the second Microservice, the following error appears:
org.axonframework.commandhandling.NoHandlerForCommandException: No
node known to accept
[com.myApplication.PaymentManagementService.commands.CheckPaymentCommand]
Both microservices are registered to my eureka server.
In the first Microservice, I produce a command over a REST Endpoint, the Command fires outside of an Aggegrate (via commandGateway.send()).
In the second Microservice, I want to receive the command:
#Component
public class TestCommandHandler {
#CommandHandler
public void handle(ApproveOrderCommand command){
System.out.println("Command delivered!");
}
}
Is there anything wrong with my dependencies or application properties? Do I have to setup a saga for this to work?
One of my poms:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.myApplication</groupId>
<artifactId>PaymentManagementService</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>PaymentManagementService</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Axon -->
<dependency>
<groupId>org.axonframework</groupId>
<artifactId>axon-spring-boot-starter</artifactId>
<version>4.4.5</version>
<!--
<exclusions>
<exclusion>
<groupId>org.axonframework</groupId>
<artifactId>axon-server-connector</artifactId>
</exclusion>
</exclusions>
-->
</dependency>
<dependency>
<groupId>org.axonframework</groupId>
<artifactId>axon-spring-boot-autoconfigure</artifactId>
<version>4.4.5</version>
</dependency>
<!-- Spring Cloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.axonframework.extensions.springcloud</groupId>
<artifactId>axon-springcloud-spring-boot-starter</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
application.properties of Microservice 1
Properties_Microservice_1
application.properties of Microservice 2
Properties_Microservice_2
I'm glad that you found out the solution by yourself.
Hope that you discovered our sample project https://github.com/AxonFramework/extension-springcloud-sample . This is a good starting point.
It also contain some indication on dependency and version that your project should have.
We also have the refguide that is a must read https://docs.axoniq.io/reference-guide/extensions/spring-cloud
Looking at your pom file is not clear to me if you are or you are not using axon server. If not, I can suggest to uncomment the exclusion of axon-server-connector dependency.

Spring Dependency for ResponseEntityExceptionHandler

I am not be able to import correctly ResponseEntityExceptionHandler
class ControllerAdvice #Autowired()() extends ResponseEntityExceptionHandler{
What I am missing in my pom xml file?
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/>
</parent>
try Add it inside dependency instead of the parent. parent is a base pom of your project.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
</dependencies>
Hi try to add this dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
You have added the dependency of spring-boot-starter-parent which does not contain spring-web dependency. ResponseEntityExceptionHandler class comes from spring-web. So, you can add below dependency to the pom:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
or you can use spring-boot-starter-web artifact which includes spring-web by default.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.9.RELEASE</version>
</dependency>
For more info you can look at pom of spring-boot-starter-web and spring-boot-starter-parent you see the dependencies that those artifact includes.

Spring Boot - Camel - Maven: How to add my own parent Dependency along with Spring Boot Dependency?

Following is parent dependency
<parent>
<groupId>com.rabu.practor</groupId>
<artifactId>integrator</artifactId>
<version>1.2-SNAPSHOT</version>
</parent>
I want to make project as spring boot project without touching parent inclusion and i am not allowed to change parent.
Following are versions in use
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>2.24.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Following are camel dependency in current project:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-infinispan</artifactId>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jaxb</artifactId>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jms</artifactId>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-cdi</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-ftp</artifactId>
</dependency>
Could anyone please help how to achieve it in spring boot
You can get benefit of spring-boot dependency management by adding the spring-boot-dependencies artifact with scope=import.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.9.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Without the parent POM, you no longer will benefit from plugin management. You need to add the spring-boot-maven-plugin explicitly.
To use a different version for a certain dependency than the one managed by Boot, you need to declare it in the dependencyManagement section, before spring-boot-dependencies is declared.

NoSuchMethodError: org.apache.log4j.ConsoleAppender.<init>(Lorg/apache/log4j/Layout;)

I have a maven project with jdk: 1.8, spring-boot: 1.5.4.RELEASE, spring: 4.3.9.RELEASE, and some vendor dependencies. The project builds and runs but when I access resources used by vendor packages, I get following exception:
java.lang.NoSuchMethodError: org.apache.log4j.ConsoleAppender.<init>(Lorg/apache/log4j/Layout;)V
at bkLogPkg.SingletonLog.<init>(SingletonLog.java:19) ~[VendorComLib.jar:na]
at Vendor.ConnectionBasket.<init>(ConnectionBasket.java:31) ~[VendorServices.jar:na]
at Vendor.ConnectionBasketInterface.Loader(ConnectionBasketInterface.java:450) ~[VendorServices.jar:na]
at Vendor.ConnectionBasketInterface.<init>(ConnectionBasketInterface.java:251) ~[VendorServices.jar:na]
...
It's trying to call method from Vendor jar file to log4j 1.2.14.jar file, but it's unable to do it. I have added vendor dependencies in pom.xml file.
pom.xml:
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
<groupId>com.sample</groupId>
<artifactId>angular2-spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>angular2-spring</name>
<description>Angular 2 application with Spring 4</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<org.slf4j-version>1.2.14</org.slf4j-version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- Vendor dependencies from Local Repository -->
<dependency>
<groupId>Vendor</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<dependency>
<groupId>Vendor</groupId>
<artifactId>VendorServiceAuth</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>Vendor</groupId>
<artifactId>VendorServices</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>Vendor</groupId>
<artifactId>VendorComLib</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>Vendor</groupId>
<artifactId>VendorHeaderClass</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
I have the same issue today. It cost me several hours to find the reason.
You just have a conflict with your log4j jar.
The quick fix for this is :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
If you really need log4j, then add the dependency:
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
You can check log4j-over-slf4j to know the detail.
I have encountered similar issues and all of mine were solved by making sure the version I was using was the most updated and that is is compatible with the rest of the code. With your problem, it looks as if the .jar is specifically built to be compatible so I doubt that's the issue. Only things I can think of given the information at hand is:
1.) Try making sure all .jar's being used are newest version. (Sometimes if your other .jar's are not completely updated, they can throw errors/exceptions when trying to access a method from a .jar that is a version ahead.)
2.) If "1" doesn't do it, try using a dependency import function in an IDE. I have had many occurrences where Maven was just causing odd errors from IDE compatibility. Importing all .jar's from a "Dependency" menu could also help.

Categories