What is Spring ?
Spring Framework is an application framework and inversion of control container (IOC).
What is inversion of control ?
By name only you can predict that it inverses the control , but question is control of what ? And the answer to this is creation of Beans. To understand this lets understand Association which is of 2 types 1)Aggregation and 2) Composition
In Aggregation we will have instances of one class to other class and in Composition also its same, only difference is that in composition it will be more dependent to each other while in Aggregation both classes will be independent .
But in both the scenarios they need instances of each other. Let's consider below example before Spring how we used to associate a class into another class.
class Student{
String name;
String id;
Address Address=new Address();
int mobile ;
}
class Address{
int houseNumber;
String roadName;
String landMark;
String district;
String state;
int pin;
}
In above example we have used new keyword to create a Address class Object inside Student class.
This is what spring has reversed and taken the control of creating Object of any class and will keep it inside IOC container and inject wherever it is required by intercepting annotation @Autowired.
class Student{
@Autowired
Address Address;
String name;
String id;
int mobile ;
}
class Address{
int houseNumber;
String roadName;
String landMark;
String district;
String state;
int pin;
}
As bean will be created by Spring so to do some operation in between bean's lifecycle we have some annotation which can be use based on our requirement like for init() method we have @PostContruct and for destroy method we have @PreDestroy
What is the Advantage of using Spring ?
Before Spring we used to create our own servlet by extending a class HTTPSERVLET and then we will override methods like doGet(HttpServletRequest, HttpServletResponse) or doPost(HttpServletRequest, HttpServletResponse) and inside these methods we will have our request/response logics and in web.xml file we will write each servlet mapping with desired path.
Example :
Same thing has been done by Spring where all these Servlet creation and request/response mapping will be done by Spring along with Embedded Tomcat Server. And to do so SPRING has created its own front controller to handle all the incoming request and named it as DispatcherServlet . This is main unique controller to handle all the incoming request and Dispatch it to the right custom controller created by us.
As explained above we can also say that SPRING is abstracted framework and SPRING BOOT is more abstracted framework of SPRING MVC. How ?
Spring boot is implementing a lot of things internally which is not required to be configured by developer only dependency details should be given in pom.xml of spring boot application and spring will download the dependency jars and create the respective beans in backend at the time application start.
For Example Spring MVC stands of Model,View and Controller , in which model and view should be configured by developer like for view part we have a class called InternalResourceViewResolver and this class has 2 property prefix and suffix , prefix is the path of view files and suffix is the type of view files. For example if response in SPRING MVC is home and prefix is : apps/file/view/ and suffix is jsp then home.jsp will be searched in this folder : apps/file/view/home.jsp and same will be displayed to client.
In spring boot we have annotation called @RestController which a combination of 2 spring annotation.@ResponseBody
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
Now while hitting the request in Header we need to add one key called Accept and mention the type you are accepting like application/xml or application/json and you will get your results in same format as mentioned. This is called Content negotiations .
Next annotation is @SpringBootApplication , this annotation is also a combination of 3 spring annotation
@EnableAutoConfiguration
@ComponantScan
Instead of mentioning all 3 annotation just mentioned @SpringBootApplication, and spring will do all the jobs of these 3 annotation.
So till now we have noticed based on the annotation spring boot is smart enough to predicate and try to make the things simpler for developer.
Just like annotation, for all other things just put your dependency in pom.xml and spring boot will create the objects and do the default required configuration automatically
How spring boot is able to do this ?
The main keyword is behind this magic is Condition and Conditional interface . You can go to the META-INF/spring.factories file of spring-boot-autoconfigure.jar present in your spring boot application under external libraries. You will be able to see Main class of each dependency of pom.xml .
public interface Condition
boolean matches(ConditionContext var1, AnnotatedTypeMetadata var2);
}
- @SpringBootApplication (@Configuration,@EnableAutoConfiguration, and @ComponentScan)
- @ConditionalOnClass and @ConditionalOnMissingClass
- @ConditionalOnBean and @ConditionalOnMissingBean > @ConditionalOnBean(name = "dataSource")
- @ConditionalOnProperty > @ConditionalOnProperty( name = "usemysql", havingValue = "local" )
- @ConditionalOnResource > @ConditionalOnResource(resources = "classpath:mysql.properties")
- @ConditionalOnWebApplication and @ConditionalOnNotWebApplication
- @ConditionalExpression > @ConditionalOnExpression("${usemysql} && ${mysqlserver == 'local'}")
- @Conditional > @Conditional(HibernateCondition.class)
Comments
Post a Comment