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
Related
We have a project with several Spring Boot apps, one of them has a Vaadin Flow UI with some dev.hilla #Endpoint.
The project structure:
|-applications
|-app1(vaadin app)
|-app2(cli app)
|-ui
|-ui-module1
|-ui-module2
The ui modules containes the TS files and the corresponding Java files and should conatines the modules endpoints, if any.
App1 uses the ui modules as maven dependencies.
When it comes to #Endpoints, only the classes in app1/src/min/java/** are processed by the vaadin-dev-server. If I place a Java class with #Endpoint annotation in any other maven module, no processing, no TS generation, I can not use that endpoint.
Currently we must place all the endpoint classes in app1.
Is there any way I can configure my project to find all #Endpoint
annotated classes?
Or to use the classpath for scanning instead of
the app1 source folder?
Versions:
Vaadin 23, Spring Boot 2.7
There is a new experimental feature that generates from the byte code instead of the source code and there for also works with Maven multi module projects.
You can enable it in src/main/resources/vaadin-featureflags.properties
com.vaadin.experimental.hillaEngine=true
I'm new to Spring world. I am following Spring Microservices in Action, where they recommend to use Spring Intializr to get Spring dependencies. However, post that whenever we need to add new dependencies, maven dependencies are provided in book. What would professionals do in such situation? How will new dependencies be found and added to the pom.xml?
You can find them in public repositories like https://mvnrepository.com or search for Maven Central alternatives.
Also, you can find your needed dependency on that third-party library's official website, for example, if you want to add Lombok dependency to your project simply find it in https://projectlombok.org/setup/maven
One the other way is Spring Initializr ad you said. Just find and add the dependency inside this website and then click on the Explore link at the bottom of the main page:
And take a look at the pom.xml file. You can see the dependency:
There are absolutely multiple other ways like finding and adding the dependency in your IDE (IntelliJ, Eclipse ...) and ...
So I am writing a library for some features and the inner library needs to use Spring. I don't want to put in the version of Spring in the inner library. I want that the application using the library defines the version of Spring to use.
Is this even possible? If yes, then how?
Maven wise, clearly your library needs to be dependent on spring because it has to be compiled somehow so at least spring annotations like #Autowire or #Configuration / #Bean need to be in the compilation classpath.
However in the pom.xml of the library you can declare the dependency on spring as "optional" (<optional>true</optional>)
So when maven will compile the application that has your library as a dependency won't need to "take" transitively the spring as well
You can read about optional dependencies here. Their example with ProjectA, ProjectB and ProjectX is relevant...
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.
In my example below there seems to be a discrepancy / duplication in the required steps in OSGi resolution and Maven dependency support.
I have a jar which is dependent on a external 3rd party jar, in this case time4j.
<dependency>
<groupId>net.time4j</groupId>
<artifactId>time4j-core</artifactId>
<version>4.16</version>
</dependency>
I can then run my simple jar locally by importing the packages and running etc.
When importing into OSGi I have to ensure that this jar is imported first, often using the PAX Wrap Url.
This is ok for 1 jar dependency but what about when there are multiple dependencies. I could use a features.xml file to collect these jars but why? I've already specified them in my pom.xml.
It seems there should be a way for OSGi / Karaf to read the pom.xml dependencies and import these into the container using the PAX Wrap url when needed.
Have I missed something here?
Sorry but your expectations are not in sync with reality.
First of all Maven dependencies are build-time dependencies. That's why you declare dependencies you know to be available in the runtime as provided
<scope>provided</scope>
Neither OSGi nor Karaf can do anything about your build time dependencies.
BUT with OSGi you can make sure your build dependencies are also available in your runtime and don't interfere with other libraries that might be available.
That's why you need to declare your imports and exports etc.
Karaf does help you with some of the dependencies for example with feature files.
If you have a feature definition maven project, all of your compile scope dependencies can be included in one feature file.
BUT, the OSGi resolver only looks at the currently available bundles and nothing more, no connection what so ever to maven, if you want to have some sort of automagic resolving of external dependencies you need to make sure that you have
a) an OBR resolver enabled (this depends on the karaf version you are using, with 4.x it's already included) and
b) an OBR repository at hand, Karaf Cave would be the project to look for in that case, cause it can reside like a proxy on top of a maven repository.