Saturday, August 12, 2017

Spring boot


Spring boot - simplifies spring application development

It is not a framework but it is approach to develop spring based application with very less configuration.

Features:-
Auto-Configuration:-
- works by analyzing the classpath. Can automatically configure common configuration scenarios. If you forget a dependency, Spring Boot can’t configure it.

=> Spring Boot automatically configures required classes depending on the libraries on its classpath.
– Sets up a JPA Entity Manager Factory if a JPA implementation is on the classpath.
– Creates a default Spring MVC setup(configures  DispatcherServlet & ContextLoaderListener), if Spring MVC is on the classpath.


Starter Dependencies:-
- It offers help with project dependency management. The starter brings you required dependencies as well as some predefined configuration bits.

=> In simple words, if you are developing a project that uses Spring Batch for batch processing, you just have to include spring-boot-starter-batch that will import all the required dependencies for the Spring Batch application. This reduces the burden of searching and configuring all the dependencies required for a framework.

CLI:- 
- Lets you write complete applications with just application code, but no need for a traditional project build.

Actuator:-
- Gives you insight into what’s going on inside of a running Spring Boot application OR expose different types of information about the running application – health, metrics, info, dump, env etc.

So, Spring Boot can take the burden of configuration off your hands.


Q:- Suppose your application want to interact with DB?
 - Spring Data libraries on class path
 - define properties in the application.properties file exists in classpath of application
database.host=localhost
database.user=admin
then it automatically sets up connection to DB along with the Data Source class.

Q:- How to reload changes on Spring Boot without having to restart server?
Include following maven dependency in the application.
<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>springloaded</artifactId>
 <version>1.2.6.RELEASE</version>
</dependency>


- Spring Boot includes support for embedded Tomcat, Jetty, and Undertow servers. By default the embedded server will listen for HTTP requests on port 8080.

- By adding logback.xml file to the application we can override the default logging configuration providing by the Spring Boot. This file place in the classpath (src/main/resources) of the application for Spring Boot to pick the custom configuration.

- Automatic restart
Applications that use spring-boot-devtools will automatically restart whenever files on the classpath change. This can be a useful feature when working in an IDE as it gives a very fast feedback loop for code changes. By default, any entry on the classpath that points to a folder will be monitored for changes

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>

This can be achieved using DEV Tools. With this dependency any changes you save, the embedded tomcat will restart. Spring Boot has a Developer tools (DevTools) module which helps to improve the productivity of developers. One of the key challenge for the Java developers is to auto deploy the file changes to server and auto restart the server. Developers can reload changes on Spring Boot without having to restart my server. This will eliminates the need for manually deploying the changes every time. Spring Boot doesn’t have this feature when it has released it’s first version. This was a most requested features for the developers. The module DevTools does exactly what is needed for the developers. This module will be disabled in the production environment.

- Configuration file 
application.properties file is very important where we would over write all the default configurations. Normally we have to keep this file under the resources folder of the project.

- what if I want to use Jetty server instead of tomcat?
spring-boot-starter-web which pull spring-webmvc, jackson-json, validation-api and spring-boot-starter-tomcat automatically and when we run the main() method it started tomcat as an embedded container so that we don’t have to deploy our application on any externally installed tomcat server.

Simple, exclude spring-bootstarter-tomcat from spring-boot-starter-web and include spring-boot-starter-jetty.


Magic behind the SpringBoot’s AutoConfiguration:-

SpringBoot provides various AutoConfiguration classes in spring-boot-autoconfigure-{version}.jar which are responsible for registering various components/beans.

@EnableAutoConfiguration annotation enables the auto-configuration of Spring ApplicationContext by scanning the classpath components and registers the beans that are matching various Conditions using @Conditional feature.
 
For example consider org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration class.




No comments:

Post a Comment

Web Development

Design Phase:- Below all these represent different stages of the UX/UI design flow:- Wireframes represent a very basic & visual repr...