Spring autowiring classes between 2 distinct project - spring

I'm trying to autowire classes between 2 projects with spring (using eclipse IDE). So I added to the first project the second project to the build path. The imports are resolved correctly but when I launch the app the log tells me that he can't find the class which I'm trying to wire to.

The classpath have solved the problem.

Related

Loading application.properties from another Spring Boot project

I have three Spring Boot projects. Project A Autowires a bean from Project B. That bean in Project B relies on properties from the application.properties in Project C. Project B will always be run with Project C. However, there are times when Project A will be run standalone.
Project A build.gradle.kts
dependencies {
implementation(project(":project-b"))
Rather than duplicate the properties in Project A's application.properties, is there a way to load, or reference, the application.properties from Project C in Project A without it running?
I've tried to use adding a dependency to Project C and also #PropertySource in Project A #Configuration file, but no luck:
#Configuration
#PropertySource("classpath:/com/meanwhile/in/hell/project-c/src/main/resources/application.properties")
Error:
Caused by: java.io.FileNotFoundException: class path resource [com/meanwhile/in/hell/project-c/src/main/resources/application.properties] cannot be opened because it does not exist
You can make use of Centralized Configuration(Spring Cloud Config Server), the application property file is saved in git(remote or local) the server will be sync the git and you can consume the application files by running the server. for reference https://spring.io/guides/gs/centralized-configuration/

Converting existing project to Spring boot project in InelliJ

I have an existing project. I need to make it a Spring boot based project and I am using IntelliJ CE.
What would be correct procedure for doing it?
Edit:
Project has no initial structure. It is a totally empty project. So no existing modules etc.
Spring Boot is an Ultimate feature, so first you would need to try/buy the IntelliJ IDEA Ultimate.
From there, you can add Spring support to existing project modules or use the Spring Initializr wizard to select the necessary configuration when creating a new project or module.
In your maven pom.xml or build.gradle file, I would add the spring boot starter dependency:
spring-boot-starter (the group id is org.springframework.boot)
If the application is a web application, I would also add the web starter spring-boot-starter-web also with the same group id (org.springframework.boot)
For convenient features, applying the spring boot plugin would help in creating a runnable jar with all required dependencies bundled called a fat jar.
A great tool I use is the spring boot project generator. It allows you to configure the modules you want and create a project template.
Spring Boot requires IntelliJ IDEA Ultimate. If you want to use IntelliJ CE, please create a project using Spring Initilizer then import the same to your IntelliJ CE (File -> Open -> Choose the project root folder). After you import the project, wait for some time so that IntelliJ can download the dependency and build your project. You can check from (Build -> Build your project). Then find the main class of spring boot and run it using the green play button

Spring Boot - package application classes as a jar in BOOT-INF/lib

I am using Spring Boot 1.4.1 with Gradle 3.1. The module which has the Spring Boot plugin applied creates its own jar with the jar task, and also has the 'fat' jar created with bootRepackage. However, the classes from that module are in BOOT-INF/classes, but I would like them to be in a separate jar in BOOT-INF/lib. How to do this?
EDIT: I know I can move the code to a separate module, but for various reasons I can't make such a split (unless there is no other way). I am looking for a single-module solution, if one exists.
You'll need to set up a multi-project build and move all of your Jersey-related classes into a separate project. You can then depend upon this new project in your Spring Boot project using a project dependency. For example:
dependencies {
compile project(':jersey-endpoints')
}

Spring-Boot : Referencing another Spring Boot Project

I created two spring boot projects, one is with JPA and the other with Web. I initially combined both of them into one project and everything works perfectly.
I now want to separate the JPA portion from the Web. So I added the JPA project as a dependency of the Web. But spring-boot is unable to detect the beans on JPA.
Is there any examples on how to implement this?
I am getting the exception when I tried to autowire the beans from the JPA project.
BeanCreationException: Could not autowire field:
The #EntityScan and #EnableRestRepository by default only scan the sub packages of the current project so if you want to have two projects you should implicitly set the path of the other project to be scanned.

Spring 4 submodule annotaions

I am using Spring 4.x for my latest project, no xml, only annotations. It was working fine till I decided to move part of my project into a submodule. I see the submodule jar in the lib folder of the project jar (used spring boot jar maven plugin to package).
The problem now is that Spring does not recognize the components in the submodule jar. The package names are the same for both the jars.
sample code
#EnableAutoConfiguration(exclude={JmsTemplateAutoConfiguration.class})
#ComponentScan("com.abc")
Structure
project.jar+
|-lib+
|-submodule.jar
Spring loads all the beans / components in the project for that package, but nothing from the embedded jar file.
Stacktrace excerpt:
No qualifying bean of type [com.abc.xxxx] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency....
What am I doing wrong here?
It's not a great idea to split packages across multiple jar files. I imagine it will work if you name the packages uniquely. Please raise an issue in github if you think it might be fixable in the class loader.

Resources