Loading application.properties from another Spring Boot project - spring

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/

Related

Spring Boot - configure properties of a jar included as dependency into another jar

I have a Spring Boot web app A and its dependent on a Spring Boot jar library B. I have some properties that I want to configure within B and don't want the client apps (e.g. the web app A) to configure them. I have these properties files in B.
application-dev.properties
application-stage.properties
applicatino-live.properties
The issue is that these properties are not recognized when the library is added as a dependency in web app A. What is the way to achieve this?
The PropertySource annotation can be used in a Configuration class on webapp A to achieve what you're looking for:
#Configuration
#PropertySource("classpath:application-dev.properties"),
#PropertySource("classpath:application-stage.properties"),
#PropertySource("classpath:application-live.properties")
public class WebappAConfiguration {
}
I also found the order that spring boot looks for externalized configuration helpful here.

read properties from third party jar in spring boot application

I’m adding a third party jar as a maven dependency in my spring boot application A. Third party jar is a jar of another spring-boot application B. it has its own configurations saved in conf folder "B.properties".
I have used #Import({B.class}) annotation to import the beans of application B in my application but not able to access the configuration properties of application B. Is there any way to access the configuration properties of an application from a jar?
You can create your own Config Server just add #EnableConfigServer annotation and provider configurations from service A to service B.
You can review this example:
https://spring.io/guides/gs/centralized-configuration/
UPDATED Fix adding
#PropertySource("classpath:/conf/B.properties"),

Spring Boot multi module WAR Generation

I have made a maven Spring boot (REST) Project that has 3 (maven) sub modules (i. api ii. implementation and iii. service modules).
The main method (#SpringBootApplication) is in the root of the project. The REST web service works fine from IDE but maven does not allow me to package this project as war and deploy to external tomcat.
To solve this I added a new module and added dependencies of other modules within this and packaged this as war (by adding maven-war-plugin). But when deployed on server; the webservice does not get hit.
Structure-
Service Project
main()(This is within root project)
api module
service module
implementation module
Newly added module (that has above 3 modules injected as dependency and the plugin that let me package this as a war)
Expecting a war that has all these submodules that can be deployed on external Tomcat 9 server.
How to achieve this?
Please Note - I have added spring-boot-maven-plugin
to repackage in the root project, but it is not working.
Adding parent to this newly added module fixed the issue and I was able to make a working jar!

What is a general way to use a Spring project inside another?

I have a Spring project which is a regular jar file. It uses JPA and Spring Data.
I'd like to use it in another Spring project, which is a war running in Tomcat. It also uses JPA and Spring Data.
I have installed the jar into the local maven repository, and have declared it as a dependency in the parent project.
What do I need to do to make them work together correctly?
Are there naming conventions for the various context, properties, and persistence files?
Do I need to import the library configuration files in the "parent" configuration files?
I am getting the following error when trying to run the parent:
IllegalArgumentException: Not an managed type: class [some domain class in the parent project]
Use Maven Modules. Reference here:
http://books.sonatype.com/mvnex-book/reference/multimodule.html

Spring autowiring classes between 2 distinct project

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.

Resources