How spring boots works ,How it loads all the configuration [closed] - spring

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
So I am having my rest Api calls using spring boot.
I want to know how spring boot loads all the configuration step by step.
like what would be the process while loading configuration,what and how the order are getting followed. Spring security,db configuration and bean initialization in term of all How these are getting loaded.
And how the spring boot find the priority (order) ,which should be loaded first and which should be next.

Beans are loaded based on the autoconfiguration mechanism in Spring Boot, there is a spring.factories file in the META-INF folder of the jar file containing the the fully qualified name of the Configuration classes to load.
When Spring Boot finds a file like that, it will load the configuration as a bean, and the configuration usually loads other beans.
Other way configuration could be loaded, by just simply #Importing them, some work like that, example #EnableMetrics.
Jar files containing this autoconfiguration mechanism usually called starters.
There are some special beans as well that will be picked up by Spring, example FilterDefinitionBean that will be turned into a filter, or CommandLineRunner, that will be executed after startup.
Usually you do not need to worry too much about the order of the starters, since Spring Boot automatically detects the correct order to initialize these based on the dependencies, beans with #DependsOn annotations.
Spring Boot starter autoconfiguraions happen after your beans are already defined, so they can give you fallback beans, but only if you have not defined them.
But sometimes, especially when you have #ConditionalOnBean you have to explicitly define the order, this can be done by #AutoConfigureBefore and #AutoConfigureAfter annotations to define where this autoconfiguration has to be initialized.
Another annotation #AutoConfigureOrder is the Spring Boot equivalent of the #Order annotation could also be used to change the order (since #Order does not work for autoconfigurations).
If you check your favourite starter's source code, you will see these order changing annotations are often added to the class.

Related

If Spring creates a DAG of beans it got to create. What is the additional benfit of using #DependOn?

Here, is a detailed post on how spring dependencies on one another can be resolved using #DependsOn --> Controlling Bean Creation Order with #DependsOn Annotation
But some time in the past I have read that spring container creates a DAG of all the beans before it starts initiating or creating the beans. If this is the case, dependencies are automatically addressed using DAG mechanism. So is not #DependsOn redundant.
But if Spring framework is providing one such annotation explicitly, I assume it must carry some real importance. Could someone clarify if there's something I am missing?

Duplicate config code problem for all spring microservices [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Is there a way to create a spring configuration class and use it to all my microservices?
Right now I have to copy a duplicate config class to all microservices. And if there are changes , I have to make them in all modules.
I use the apache zookeeper for centralize my configurations in single place. For setting authentication details on connection to zookeeper server I have to create a new bean for CuratorFramework class in spring #Configuration in any microservices that keep their configs on the zookeeper.
Yes, you can:
Create a module with a configuration (maven, gradle managed, whatever). Add all common beans to this module for reuse. It will be a kind of "infra".
Optionally add a spring.factories file to automatically "load" the configuration
In Microservices (each of them should be in a different module) add a dependency on that common module.
If you've added spring.factories - the configuration will be loaded automatically, if you didn't you'll have to import this configuration explicitly in each module. But in any case you won't need to duplicate code.
There are many tutorials / threads in SO about spring factories, so in case you're not familiar with this feature - you can read here for example
If a system has lots of common stuff and stuff that changes often it's a monolith. Maybe your split to microservices is not the best approach.
Regarding shared common stuff you get 3 choices:
Copy/paste (like you do for now)
Break out library (each microservice has a dependency to a common module)
Isolate stuff in his own microservice
Remember that DRY must be strict inside one microservice but can be relaxed between microservices.

Spring boot annotations [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm a beginner with spring boot so i need to ask some questions:
1 - what is difference between #async and #service("async")
2 - what is #transactional do ?
3 - what is the difference between maven project and spring-starter-project ?
thanks for all ,,
Question 1
The annotations don't have any relation.
#Async marks a method as a candidate for asynchronous execution. Can also be used at the type level, in which case all of the type's methods are considered as asynchronous.
#Service annotation serves as a specialization of #Component, allowing for implementation classes to be autodetected through classpath scanning.
If you use #Service("async") you are creating a Bean with name "async", nothing to do you asynchronous processing.
Question 2
#Transactional is used on methods that should span operations (like DB operations) in a single transaction. Let's say for example that you have a method that is going to save 3 records to different tables on DB and you have it annotated with #Transactional. Then all the 3 operations are going to be commited only if the method completes successfully. If the last operation fails, then nothing is commited to DB.
Question 3
A maven project can be any type of Java project. A Spring project is a project that uses components from Spring framework.
If you want to create an automatic Maven project with Spring dependencies take a look at Spring Initializer. It will automatically generate the files for you.

Is Spring Container is just like a jvm? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Is Spring Container is just like a jvm? or it is different?
Why is Spring IOC mainly used? If it is for creating the objects without using new operator? what is wrong in using new operator?
If we are creating singleton objects and return the same object whenever application wants, we are loading all the objects on server start up? will that not make the application heavy?
If it is so, then why we need spring core?
How is filter,bean post processor, aop is different?
if aop is used for implementing cross cutting concern, why do we need beanProcessor interface?
Is Spring Container is just like a jvm?
No, Spring is a Java framework. It provides classes you can use to run a Java application on a JVM.
Why is Spring IOC mainly used?
Learn what Inversion of Control is and you will understand why it is used so heavily.
If it is for creating the objects without using new operator? what is
wrong in using new operator?
The new keyword forces compile time dependencies. Inversion of Control and Dependency Injection remove those dependencies, mostly through reflection.
If we are creating singleton objects and return the same object
whenever application wants, we are loading all the objects on server
start up? will that not make the application heavy?
You will usually want all those objects at startup so it's a non-issue. You can delay the initialization of those objects (beans) with lazy loading.
If it is so, then why we need spring core?
If what is so?
How is filter,bean post processor, aop is different?
The BeanFactory creates and initializes beans. A BeanPostProcessor is meant to wrap a bean in a proxy or modify that bean's properties. The javadoc has more details.
Aspect oriented programming is a style of programming. In order to implement it with plain old Java, you need to use JDK or CGLIB proxies. Those are applied using BeanPostProcessor instances by wrapping the processed bean. Calls going to the target bean will be intercepted by the proxy which will (possibly) perform logic before delegating to the target bean. Java's AOP capabilities are almost completely limited to method calls.

Spring Annotations when java file is compiled

I started learning spring today and i have a question regarding what happens to the annotations when java files with annotations is compiled ?.
The reason i am asking this is because of the fundamental difference i see when we choose to use the xml approach vs the annotations approach , and what i think is the philosophy of spring. The way i understand is spring says that all your java classes can be simple pojo's and all the spring related config should be kept independent (Like xml file.)
In case of developing spring application using xml *.java files have no idea about spring container and are compiled in to .class without any spring related dependencies.
But now when we annotate the .java file and the file is compiled the compiled file now has all spring related dependencies hard baked in to it and no longer are your classes simple pojo's.
Is this correct ? I am not sure if i am missing some thing here.
Annotations can be considered as metadata of a class or its element (method, field, local variable...). When you put annotation, you don't implement any behaviour. You just give additional info on an element.
That way, Spring, which is in charge of instanciating its bean can collect the info with reflection (see also this site) and process it.
To conclude, your Spring beans still remain POJO and there is no difference with the XML way (...from that point of view) since Spring gets from annotations the information it would have got from XML .
I think you are right and your question is justifiable, that's the way how I think about it too.
Not only compiled code but also dependency on spring jars bother me. Once you use this annotations your resulting jar depends on spring library.
It's reasonable to store beans in model according to DDD but spring is some kind of infrastructure layer so I didn't like the dependency.
Even if you would use XML, it's useful for few placed to use attributes. E.g. #Required attribute which is useful to verify that linked bean was injected. So, I've decide to use constructor dependency injection to omit this attribute, see my article. I completely leave out the dependency on spring in the code.
You can probably find such mind hook for many annotation you want/force to use.
You can use annotations only for your configuration classes, without marking them actual bean classes. In such scenario if you not use spring you just not load configuration classes.

Resources