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
Post a Comment