Skip to main content

Spring Boot Introduction

INTRODUCTION:

1. The first point to note is that spring framework is to make J2EE application development easier

2. The main objective of spring boot is to make Spring Application development was easier.

3. Spring Boot versions are different from spring version.


Spring Application Development Drawbacks:

• The first drawback is the dependency version compatibility issues.

• Then the boiler plate code is an another problem.

• Then there will be lot of manual configurations.

• For framework related classes we don't have the source code so we need the Bean configuration.

• Time consumption and complexity is high is there.

• Difficult for new comers to try out the spring features.


1. To overcome all these problems spring introduced a module called spring boot.

2. Spring boot does not replace spring framework i.e. It is one of the module like core, MVC, etc..

3. Using spring we can develop a good application but it will take more time than boot applications.

4. Spring boot is a new way of creating spring based application

5. Spring boot aims to simplify the developing productions.


Spring Boot Features:

• Dependency management is easier.

• Auto Configuration

• Embedded server

• Actuator - It will address all non functional requirements like health, monitor, metrics , etc...

• Dev Tools 

• Spring Application without xml configuration.

• Opiniated but highly customizable.

• Spring boot CLI


1. Spring Dependency management advantage:

a. Spring boot introduced a concept called starter dependency

i. Spring boot Parent Starter 

ii. Spring boot starter <feature>

Spring Boot parent starter .pom:

• This xml file will configure all the third party library versions details.
• It also consist of the spring modules version information's.
• Spring boot version is compatible with spring version which will be taken care by spring boot module.
• Spring-boot-starter-parent.pom is used to provide the information of all libraries.

Spring Boot Starter <feature>:
• The feature may be a web, JDBC, redis, JPA. 
• We don't need all the dependency to a web or any other project just add the feature with starter like spring-boot-starter-web.
• This single feature dependency itself bring all the dependency with it for the web.
• Spring boot starter dependency is used to bring all the required dependency of that feature.

2. Auto Configuration:
• Configuration - configure the beans using xml or java or auto wires with components.
• As we are using auto wiring but still we are depending on manual configuration.
• The main problem is we can't apply @Component for all the classes like dependency classes where we can apply only for the code we have but not the framework classes like JDBC Template, Rest Template, etc...
• If we want to use the framework class then we should configure them as wither @Bean or <bean> element. 
• Auto configuration means instead of configuring beans by developer the spring boot will take care to configure and create objects.
3. Embedded server:
• Server Inside an application is called the embedded server.
• Spring boot application itself having server is called embedded.
• This is more useful in cloud because if our application is running in production in many systems the request may go to any of the system so if we want extra system for other request just we can copy the application to other system don't need to totally reconfigure the system.
4. Actuator:
• Before spring boot we need dedicated team to monitor the application like health of application, monitoring, metrics, beans count, threads count, etc..
• These are the non functional requirements.
• But in spring boot the actuator is used to address all these non functional requirements.
• This has the production ready features.

5. Dev tools:
• It is used to improve the developer productivity.
• If any changes done by developer this tool will compile and regenerate jar and deploy application automatically.
6. Spring Boot Without XML Configuration:
• The spring boot is totally free from XML configuration as it is an tedious process.

7. Opiniated but highly customizable:
• It provide default versions but we can customize that versions also.

8. Spring Boot CLI:
• Command line interface used for quick application development.

 Creation of Spring Boot Application:

1. Use the eclipse IDE

2. Using stat.spring.io

3. Using STS IDE


• If we use the Eclipse IDE we need to add the dependency manually in pom.xml

• Then spring configuration need to add manually in the eclipse IDE

• If we use the start.spring.io will provide the zip file with the all the dependency and plugins and etc where the developer will select those

• Every spring boot application need the properties file.

• In the spring.io website we can initialize the project.

• But if we use the STS Ide then it will connect to the website and then it connect to the application.


Spring Boot Project Structure:

• The first entry is pom.xml

• Then the src-main-java-com.aaaa. 

○ This holds all the source codes

• Then there is a resource in the src-main

• Spring boot main class starts the application.

• There will be a default run method class will be in the project file of the spring boot which will run the application

• Spring Boot is used to develop end to end application.
• We can develop standalone, web, distributed applications using the spring boot.
• If we use the spring core the developer is responsible for
○ Write config
○ Initialize containers
• But in spring boot the main advantage is configuration is not required.
• The deducefromclasspath method in the springBootApplication.run method which will decide the container type.(Like reactive, web, etc..).
• When the run method executes then the spring application constructor will take care which container is required.
○ Then the deduceFromClasspath will check in class path based on classes found in class path then corresponding spring container will be created.
○ Then it will create an empty environment object.
○ It will detects the configuration for our application and loads into environment object.
• The @SpringBootApplication configuration is combo of @ComponentScan, @EnableAutoConfiguration, @SpringBootConfiguration. 
• The @ComponentScan will search for the component classes at package level and class level.
• The default package name is current package.
• The @EnableAutoConfiguration is used to provide the auto Configuration for the framework related classes.(JDBC, redis cache).
•  If the run method finds the mvc jars then it treats as the WEB as webapplication type. And if it finds web flux jars then it treat as reactive web application.
• It will create AnnotationConfigServletWebServerApplicationContext object if it is web and AnnotationConfigServletReactiveWebServerApplicationContext if it is reactive application.
• And if none is specified then AnnotationApplicationContext object is created.
• Then it will Instantiate the spring factories and register with IOC container.

Loading Properties file in Spring Boot:
• In spring boot there is no need to load the property file using the propertySouce annotation.
• Directly we can add the properties using @Value annotation.
• In Spring boot the properties file name should be application.properties or application.yml.
• If we want to load our own properties file then we should load using @PropertySource.
• Spring has introduced a ConfigurationPropertiesBeanPostProcessor which will take care of bean creation so that it will be automatically registered with the IOC container.
• The properties file can be used without the value annotation by:
○ @EnableConfigurationProperties which is used to create the postprocessor object. This annotation should be present above the class which runs the app.
○ @Configurationproperties(prefix="") which is used to segregate the properties in the basis of prefix which can be used with the getters and the setters. This should present above the class which is using it.

Spring Profiles Loading:
• Using profiles we can switch one environment to another environment.
• Each environment have own configurations.
• The profiles can be directly loaded by the docker file or cicd pipeline, etc..
• The properties can be easily stetted using the spring.profiles.active=dev, etc.

Spring Runners:
• If we want to perform a one time startup activity after IOC container has been created then we use the Runner.
• For Example:
a. Read the values from cache.
b. Displaying some kinds of List.
• Types of Runner:
a. Command line Runner.
b. Application Runner.
• Using application runner we can get many args.
• These two are interfaces so that we should extend the class which has the main method.






Comments

Popular posts from this blog

Java Spring Framework:

CORE: BASICS:           The new technology I had learned is the spring Framework core which is the base of the Spring technology or framework in Java. The main feature of the spring is that it is light weight where the run time is very less. Then the next advantage is the framework use the dependency injection in the object reference in Java where java is a OOPs programming language. The spring framework was found by rod johnson in the year of 2002 and licensed in the apache company in 2003. The other advantages of the Spring is that it can be used to build every application or software which is currently in the market. The framework supports: AOP Spring Boot Spring JDBC Spring MVC Spring EJB Spring Hibernate.           The diagram from the official website of the Spring is a good representation of the Spring Framework architecture which relies on the Spring Core. Fig 1: Spring Framework       ...

Properties and Primitive Insertion in Spring:

 Property Files and Values: The Properties is a subclass of Hashtable class and it represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string. .properties is a file extension for files mainly used in Java-related technologies to store the configurable parameters of an application The Properties file can be used in Java to externalize the configuration and to store the key-value pairs. The Properties.load() method of Properties class is convenient to load .properties file in the form of key-value pairs. In normal java syntax we will use the util.*. to load the properties, URL,  etc. Then we use the load method to load any file and we can use that to retrieve properties. But in spring we can use only one annotation to load the whole properties file and the Annotation is @PropertySource:(file path). The main important job of the property file is to reduce the har...

Annotations and Scope in Spring:

 Basic Annotations: Component: It is used to create the bean without the configuration files like xml and java configurations The auto wired is used along with it to do the dependency injection Autowired: The Autowired annotation is used for the dependency injection the main types are the: Autowired in constructor Autowired in setter Autowired in the field Note : Using the field injection is not recommended. Fig 1. Autowired in constructor. Fig 2. Autowired in the field. Import: It is used to import other configuration files into the class file.  This will be helpful in the scenario of multiple configuration files are there. Fig 3. Import method usage Primary: The @Primary annotation can be present at the starting of a class where it represent the primary class. For example if we implement two classes using a interface and we gave two classes the @Component annotation the spring will be confused.  So when we add the primary tag on any one of them then that one will be cho...