Spring Boot
Spring Boot
Note:
- By using Spring core recommended to develop Web Application (Spring MVC).
- By using Spring Boot recommended to develop Restful webservices/ Microservices/
spring Cloud applications.
1. package org.nadim;
2.
3. import org.springframework.boot.SpringApplication;
4. import org.springframework.boot.autoconfigure.SpringBootApplication;
5.
6. @SpringBootApplication
7. public class Application {
8.
9. public static void main(String[] args) {
10. SpringApplication.run(Application.class, args);
11. }
12. }
Examples:
- When we add a dependence “spring-boot-starter” in Maven/Gradle for standalone
application our starter class run() method internally use
“AnnotationConfigApplicationContext” is the class to start or create IoC container.
- When we add a dependence “spring-boot-starter-web” in Maven/Gradle for
standalone application our starter class run() method internally use
“AnnotationConfigServletWebServerApplicationContext” is the class to start or
create IoC container.
- When we add a dependence “spring-boot-starter- webflux” in Maven/Gradle for
standalone application our starter class run() method internally use
"AnnotationConfigReactiveWebServerApplicationContext" is the class to start or
create IoC container.
1. package org.nadim;
2.
3. import org.springframework.boot.Banner.Mode;
4. import org.springframework.boot.SpringApplication;
5. import org.springframework.boot.autoconfigure.SpringBootApplication;
6. import org.springframework.context.ConfigurableApplicationContext;
7.
8. @SpringBootApplication
9. public class Application {
10.
11. public static void main(String[] args) {
12. SpringApplication sa = new SpringApplication();
13. sa.setBannerMode(Mode.OFF);
14. }
15. }
1. spring.main.banner-mode=off
Note:
- We can define multiple runners in one application.
- They are executed in Alphabetical naming order
- We can provide our own order using @Order(int) annotation. If no annotation
@Order is provided then default value is Integer.MAX_VALUE (2147483647).
- If multiple Runners are having same order, then again naming rule is applied.
Note: Both are functional Interface. They are containing only one method
Coding Steps:
1. Define one class and add @Component
2. implement CommandLineRunner/ApplicationRunner and override run() method
3. Define your logic inside run method.
Chapter-01
Spring Boot Core
- @Value will try to read one key data from “application.properties” file and if key is
found inject data into one variable, else key not found then container throws
exception.
-
- @Value we can use only one time per variable. So, if we have n number of variables
to load n number of keys then we should define @Value over n number of variables.
Why?
- “@ConfigurationProperties” creates child class object based on condition,
saying that if data is present (at least for one variable) inside child class then
create object and link.
Application.properties file
=======================
- .properties file will store data in key=val format. Where key are case-sensitive.
- If same key is provided multiple time with different value then last combination is
taken.
- Symbol # indicates comment in properties file.
- You can use _(underscore) . (dot) - (dash) symbols in key name.
o Example: my.app. std-name = value
my.app_name.gender = value
- Auto parsing is supported based on variable datatype. By default key and value
(both ) are String datatype.
1. my.app.name = Service
2. my.app.service.code = 102
3. my.app.service.vaucher = debit
4. my.app.port-number = 8080
Chapter-01
Spring Boot Core
Primitive Data representation
- Syntaxt
prefix.variableName = value
Note:
1. No Duplicate Levels in YAML file
2. Every level should end with either :<NextLine> or :<oneSpace><value>
3. Every new level start (not for 1st line) must have same space count (indentation) [at least
one space]
=====================
Project Lombok API
=====================
- It is a Java Open-Source API, used to generate source code before we compile
the code. After compiling the source code, .class file will be generated. For run
the .class file Lombok is not required.
- We need to add, activate Lombok JAR and use all annotations.
===============================
Step to Activate Lombok API:
===============================
I. After Creating stater project in pom.xml add Lombok maven dependency-
1. <dependency>
2. <groupId>org.projectlombok</groupId>
3. <artifactId>lombok</artifactId>
4. </dependency>
IV. And double click on “lombok-1.18.30.jar” (or execute cmd: “java -jar lombok-
1.18.24.jar” file and select IDE and click on install and Update Button.
V. Quit Installer.
======================
Lombok Annotation:
Chapter-01
Spring Boot Core
======================
1. @Setter: generates set methods
2. @Getter: generates get methods
3. @ToString: override toString logic
4. @EqualsAndHashCode: generates equals and hashCode methods
5. @NoArgsConstructor: Default/zero param const
6. @AllArgsConstructor: All variables selected to create one parameterized
constructor.
7. @RequiredArgsConstructor + @NonNull: To generate selected parameters
constructor. @NonNull use over the specific variables which variables are used as a
constructor parameter.
8. @Data: This one is used to generate set/get, toString, equals, hashCode and
Required Args Constructor.