Spring Application Properties from Dependency - java

Suppose we have a Spring application (say using Gradle or Maven).
We control a Maven dependency which imports various spring boot starters and configures them (using #Configuration and application*.yml files).
When does the imported configuration apply? If the application defines its own application.yml, what are the rules for determining which configuration “wins” over the over?

Related

What is the relationship between Spring Boot and the Maven pom.xml file?

I use maven to build spring boot applications. I know that the maven spring-boot dependencies have annotations (such as #AutoConfiguration or #Condition*). At what point in the SpringApplication.run method in the main class does it actually read from the pom.xml file? I'm stepping through the code line by line and I can't figure out at what point does Spring Boot interact with the pom.xml file.
The #EnableAutoConfiguration enables auto-cofiguration of the Application Context based on jar files in the classpath and user-defined beans, so presumably the pom.xml eventually gets added as a source and the annotations in the dependencies are read when the auto-configuration state is running, but I can't figure out where or when it does so.
SpringApplication.run doesn't read the pom.xml file, it works at a higher level of abstraction. The pom.xml file is used by your IDE or by the mvn command line application to download dependencies and configure your applications classpath.
By the time that SpringApplication.run is called, the classpath is fully configured but SpringApplication itself isn't aware about how this was done.
Auto-configuration occurs by searching the all jars on the classpath looking for a files named META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports (if you're using a recent version). Once these files have been found, the classes listed within them are loaded and used for configuration.
If you want to step though the code, you can set a breakpoint at AutoConfigurationImportSelector.getCandidateConfigurations(...) and see the auto-configuration candidate classes get found. The exact auto-configurations that get applied will depend on the #Condition... annotations on them.

Spring Boot beans not added to context using Bazel

I have one Spring Boot application that uses Bazel to manage all of its dependencies, that I migrated from Maven.
I'm able to launch the app using Bazel, but after checking all of its beans I noticed that are missing the beans of the classes that I created using the #Component, #Service and #Repository annotations.
If I use Maven, these beans are added to the Application context as expected.
Is something missing in my Bazel configuration? How can I generate and add these beans to my application context?

intellij spring boot run cannot load application.yml automatically

my application.yml locate
my application.yml
spring boot run server port
Intellij spring boot run configuration
Hi.
My project is Intellij spring boot 1.5.7 Gradle Project
When Intellij spring boot run cannot load application.yml automatically.
but gradle bootRun is very well run&load.
Please suggestion me.
You should specify profile configuration. From your application.yml, you have multiple configuration separated by "---", this is used for multiple profile configuration for different spring profile. Try removing "---" and specify profile as following :
Multiple configuration is used when you have different configuration for different profile, as following:

Load enviroment data in a project which has been added as dependency to another Spring Boot project

I want to add a project as a dependency to a Spring boot project.
I can't use a maven multimodule approach.
Now in this project I'm exposing a service that is sending queries to a Database. The Database connection data needs to be set from the main Project application.properties.
How can I access the data from the main project? Should I use Spring in the "dependency project" too?
Could you point me in the right direction ? Is this even possible?
Any help is greatly appriciated.
Thanks!!
I solved this problem in a very very convinient way.
I developed my service with a spring boot application + spring-data. When I was done with this service (development testing etc.) I packaged it as a jar.
This jar included only the models, repositories and the services. All Spring configurations have been excluded.
I added this jar as a dependency to my other Spring boot project and changed my #EntityScan and #EnableJpaRepositories to include the packages from the dependency.
My annotations look like this now:
#....
#EntityScan("my.package.entities", "dependency.package.entities")
#EnableJpaRepositories("my.package.repositories", "dependency.package.repositories")
#....
public class JpaConfiguration extends BaseJpaConfiguration
My dependency models and repositories used automatically the spring configuration from the parent project.
I don't know if this is the best way to go about this, I only have a little experience with spring.
If you have any advice to do this in a better way, please write it here.
Thanks for woatching.

How Spring boot giving the dependencies and configuration?

I understand the concept of Spring boot, but I am looking for the logic how it is implemented and where it is maintaining the configuration. When we add any Starter-pom immediately it is giving the dependency and the configuration needed for it. How it automated that feature and where is that automating code in the spring boot?
Thanks in advance
The configuration classes for Spring Boot are in the module spring-boot-autoconfigure. A starter POM has a dependency on that (through the general spring-boot-starter module) and the required 3rd party libraries, and then the autoconfiguration for that library is activated.
The SpringBoot project has been put there so to be more productive & build production ready app in no time. SpringBoot project referes many starter projects like spring-boot-starter-jdbc, spring-boot-starter-logging, etc. All these starter libraries are like maven sub module projects and they add a set of libraries to respective project in turn. Like the spring-boot-starter-jdbc library adds these libraries -> spring-jdbc,spring-tx,tomcat-jdbc.
Now for the configuration part, spring boot has maintained another library called spring-boot-autoconfigure which auto configures all needed configs depending on the libraries present on your pom and the initial set of config annotation been used on the app. For Eg. if it sees ojdbc jar present in your pom then it will autoconfigure oracle datasource to your project
From my bare understanding, this feature are not provided by Spring Boot. It is the power of Maven. Maven allow you to declare dependencies, and the dependencies themselves, PLUS the transitive dependencies will be retrieved.
The starter POMs are simply normal Maven POM-type artifact which declared essential dependencies, and hence, when you include in your own POM, related dependencies will be downloaded.
You may get some more understanding on Maven from Maven Site or Maven Guide by Sonatype

Categories