- ComponentScan
- @Value, properties file
- Project Lombok API
- CommandLineRunner & ApplicationRunner
- @ConfigurationProperties
- @Inputs to Application , Option Args
- Spring Boot Profiles Using Inputs (Properties/YAML)
- Spring Boot Profiles Using @Profile
- Spring Boot Stop Watch, Banner and VM args
- Spring Boot Email Programming part-1
- Spring Boot Email Programming part-2
- Spring Boot Scheduling
- Hibernate with JPA
- Spring Boot Data JPA
- Spring Boot Data JPA - Association Mapping
- Spring Boot Web Mvc
- Spring Boot Restful Services
- Microservices
- Spring Batch Processing
- Spring Security
-
All programmer defined files/classes must be under Starter class/Main class package (or its sub-package).
-
If your create your class outside of Main class package (or any diff package) then Spring container can not find your class, no object is created, so to resolve this error we use @ComponentScan on top of main class.
-
Spring Boot creates and loads a file application.properties by default.
-
Read data from properties file for our own keys using @Value("${key}")
-
Create custom properties file and load it into the container
-
This is Open Source java API, used to generate source code before compile
-
Annotations
-
@Getter
-
@Setter
-
@ToString
-
@AllArgsConstructor
-
@NoArgsConstructor
-
@RequiredArgsConstructor
-
@EqualsAndHashCode
-
@Data
-
-
Runner : It is a block of code that gets executed only once when application is started.
Ex: Test Our Logic, CRUD Operation Test -
If we define multiple Runners in Spring Boot application they are executed in Unicode naming order.(A-Z order)
-
UnOrdered Runners are executed at last with naming rule and with last priority.
-
we can read data from Spring Container to our code using
- @Value (in case of Annotation Configuration)
- Environment Autowired , env.getProperty() (in case of Java Configuration)
- @ConfigurationProperties (For Spring Boot)
- @ConfigurationProperties is called as Bulk Load.
- It supports reading multiple keys data into variables by defining this annotation only once at class level.
-
Priority Order
- Option args (Commands Line Args) (--key=val)
- VM Args
- application.properties
- application.yml
-
What ever process we use, finally we can read data using @Value or @ConfigurationProperties only.
-
Create application.properties for different profiles using naming convention application-profileName.properties
-
Create application.yml for different profiles in single file and specify the profiles using
spring: config: activate: on-profile: - profileName
-
set active profiles while running the application using --spring.profiles.active=profileName in the run configuration program arguments
-
To execute class/code based on environment then use @Profile on top of class.
-
A class has no @Profile annotation means execute logic in any environment.
Ex:- SwaggerUi (RestWebservices Test Screen) -- @Profile({"default","qa"})
- RoleDataInsert (QA) -- @Profile("qa")
- GoogleMailService -- @Profile("prod")
-
@Profile can be placed at any class.
-
If current profile is matched with @Profile then code will be executed.
-
We can provide multiple profile names by using @Profile({"profile-1","profile-2"})
-
A class which has no @Profile annotation is executed in every environment.
This is given by Spring f/w used to calculate time taken for a process like method execution time, block execution time..etc
Methods : start(), stop()
When we start spring boot application at console Spring LOGO is printed that is called as banner.
- To disable this we use spring.main.banner.mode=off
- we can even print out own banner by using this link, generate text and place the content in banner.txt under src/main/resources folder
- To modify file name from banner.txt to any other we should use key=val that is spring.banner.location=classpath:sample.txt
A variable created at JVM level to provide common data to multiple application/VM as input in key=val format.
- To create this
Syntax : -Dkey=val - To read data
String value = System.getProperty("key");
- VM args are used for JVM inputs, less used in production.apps. But still Spring boot supports as 2nd priority input.
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=username
spring.mail.password=password
spring.mail.properties.mail.smtp.starttls.enable=true
- Create new Gmail Account
- Login and Click on Profile Name (Top right corner)
- Manage Your google Account
- Click on Security
- Enable Less Secure Apps
- Turn it on.
- Here setText() is overloaded
- setText(text) internally calling setText(text,false)
- setText(String , boolean) is the actual method contains logic to set data.
- if boolean html=false, Text data is sent as Plain Text. html=true , Text data is sent as HTML Content.
- Resource(I) : it indicates a file exist in a location.
- A File exist in your System (D:/abcd folder ex) ==> FileSystemResource()
- A File Exist in internet location ==> UrlResource()
- A File Exist in Project src/main/resources folder ==> ClassPathResource() ==> Use Resource Parameter as Array
- using same method name and but different in paremeters.
- send() method is overloaded as
- to,subject,text (find in SimpleEmailRunner)
- to,subject,text,file (find in EmailWithOneAttachmentRunner)
- to,subject,text,cc,bcc,files (find in FullRunner)
Executing a task/method in a loop based on period of time or point of time.
Examples:-
- Bank account statement (Every for month)
- CC Bill Statement
- Weekly/Monthly/3months Reports
- EMI Reminder
- Birthday Messages
- Tax Payment
- Electricity Bill Payment
- Server reports/Log files/Billing...etc
4hrs, 2mins, 5days, 6years
1st Jan- 9AM, 31st Dec-6PM ..etc
- Entity Class
- Configuration XML
- Test/Main class
Entity Class : A class that is mapped with Database table using JPA Annotations. (@Entity, @Table, @Id, @Column ..etc)
@Entity
@Table(name="stdtab")
class Student {
@Id
@Column(name="sid")
Integer stdId;
@Column(name="sname")
String stdName;
@Column(name="sfee")
Double stdFee;
}
@Entity : Must be applied on top of class, that maps class with table and variable with columns.
@Id : It indicates Primary Key, it must be applied over a variable.
@Table : It is optional. If we do not provide tableName, then class name is taken as tableName.
@Column : It is optional. If we do not provide ColumnName, then variable name is taken as ColumnName.
- Hibernate Initial Releases are not implementation of JPA. They are just ORM implementation.
- After Hibernate 5.x (5.2.x) is following JPA implementation.
old | new |
---|---|
SessionFactory | EntityManagerFactory |
Session | EntityManager |
Transaction | EntityTransaction |
save(obj):Serializable | persist(obj):void |
update(obj) | merge(obj) |
delete(obj) | remove(obj) |
get(T.class,id) | find(T.class,id) |
- Spring Boot Data JPA (Spring Data JPA) is internally using Sun JPA and JBoss Hibernate implementation.
- Data JPA Generates one Proxy class by taking SimpleJpaRepository(C) as input Template.
- Embedded Database : (No Download + No Install/ Runs at RAM) H2, Derby, and HyperSQL(HSQL).
- External Database : MySQL, Oracle, Postgress...etc
- If we move from one database to another databse only jar(Driver JAR) and properties are going to be changed. Code remains same.
application.properties file
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Drive
spring.datasource.url=jdbc:mysql://localhost:3306/boot9am
spring.datasource.username=test
spring.datasource.password=root
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
application.properties file
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/boot9am
username: test
password: root
jpa:
show-sql: true
hibernate:
ddl-auto: create
properties:
hibernate:
format_sql: true
database-platform: org.hibernate.dialect.MySQL8Dialect
-
For Embedded Database above properties comes with default values. Use only for Development/test purpose only, dont use at production.
-
JDBC 4.x has provided -AutoLoading of Driver class ie if we did not specify driver class, based on URL and JAR in classpath Driver is loaded and Register.
-
Hibernate (4.x)- Auto Detect of Dialect is exist, if we did not specify any dialect also fine.
Note:
- Programmer has to define one interface with Model/Entity class and PK DataType.
- That interface must extend any one of below type
- CrudRepository<T,ID>
- PagingAndSortingRepository<T,ID>
- JpaRepository<T,ID>
- For above interfaces implementation logic is given by : SimpleJpaRepository<T,ID>
- At runtime by taking SimpleJpaRepository template one class is generated (no .java/.class physically exist) that gives implementation for your entity class based.
- Transaction Management is implemented using Spring AOP (Aspected Oriented Programming) advices.
- before advice calls beginTx() method
- afterReturningAdvice (on success) ---> commit()
- afterThrowingAdvice(on exception) ---> rollback()
- Here, need not to apply @Repository annotation. No Need of Dao-DaoImpl pattern.
- Providing driver class name and dialect in properties file is optional based on URL provided and JAR added(pom.xml) they are auto-detected/loaded.
- Data JPA is for SQL based databases only. Not supported for NoSQL.
Link one table(Primary Key) with another table(Foreign Key) using PK-FK Columns.
Multiplicity (4)
one to one 1...1
one to many 1...*
many to one *...1
many to many *...*
1...1
employee ------ passport
1...*
employee ------ address
1...*
product ------- brand
1...*
vendor ------- contract
*...1
employee ------ department
*...*
student ------ course
Hint: *(Many) Side FK Column is created.
Note:
- For one-to-many and many-to-one FK column is created at many side.
- For many-to-many one additional table is created with 2 Fk columns
1...1
person --- aadhar
*...*
book ---- author
*...*
Faculty --- Course
1...*
Student --- Contact
1...*
College --- Branch
--------------------------------------------------------------------------
Non-Collection Collection
--------------------------------------------------------------------------
1...1 1...*
*...1 *...*
--------------------------------------------------------------------------
- Define two classes and apply HAS-A Relation (create HAS-A Variable)
- Check for Non-Collection | Collection Type.
- If collection type then change HAS-A variable to collection type.
- Apply Association Mapping annotation over HAS-A Variable.
1...1 | @ManyToOne (unique condition)
*...1 | @ManyToOne
1...* | @OneToMany
*...* | @ManyToMany
- Provide Join Column (or) Join table at HAS-A Variable
- Draw tables with column details..
- When we perform any non-select operation(save/update/delete) same you want to apply on child object also, then use cascading.
- If we use cascading such relation is called as Composition else it is called as Aggregation.
- FetchType(enum) used for select operations, to specify should it fetch parent with child data or not.
- LAZY : Fetch only parent data
- EAGER : Fetch parent and its child data too.
- Default FetchType is
- OneToMany,ManyToMany : LAZY
- OneToOne,ManyToOne : EAGER
- Note : If child is many then default fetch type is lazy, If child is one then default fetch type is eager.
To fetch data from multipl table using single SELECT SQL.
-
INNER JOIN | JOIN : only connected rows of both tables
-
OUTERS JOIN
- LEFT OUTER JOIN | LEFT JOIN : Left side table all rows, connected right side table rows
- RIGHT OUTER JOIN| RIGHT JOIN : Right side table all rows, connected left side table rows
- FULL OUTER JOIN | FULL JOIN : Both connected and non-connected rows(in simple all rows)
- To execute set of statements as a single call can be done using
Stored Procedure concept.
- Stored Procedure === Java Methods
- IN Params === Parameter
- OUT Param === ReturnType
- At database side we create Procedure and at java side we call it/execute it.
- MVC is a design pattern used to develop web applications.
M - Model (Data)
V - View (UI/Display)
C - Controller (Class/request process)\ - At least one Servlet is required to develop web applications using Java.
- Web Browser can run/understand only : HTML,CSS and Javascript.
- Servlets --> (.java)-->(.class) ->service() output : HTML => Sent to browser
- Angular --> .ts ->(bundle) .js --> .js + .css + .html -> Sent to browser
Ex:
--------------------------------
Module ControllerName
--------------------------------
User UserController
Search SearchController
Cart CartController
Payment PaymentController
..etc
-
HandlerMapping helps FC to identify one controller based on Path + httpMethod
-
Controller returns ViewName(no Location and Extension of File) to be independent of UI technologies.
-
Controlle provides Data(Stored inside Model-->Container) which is optional
-
Expression Language : reads data from Container at runtime provide to UI page Ex: Welcome to ${varaible}\
Ex ViewName(String) : Home, Profile, Settings (Controller returns)
ViewResolver will add prefix (location) and suffix(extension)
ViewPage ==> Prefix + ViewName + suffix\
/mypages/ Home .jsp
JSP : Sun(Oracle)
Thymeleaf : https://www.thymeleaf.org/
freemarker : https://freemarker.apache.org/
-
Non-Java UI : Angular/ReactJS
-
In Spring Boot Autoconfiguration is provided for
- FrontCotroller
- ViewResolver (provide prefix and suffix using properties)
- HandlerMapping
-
only code for : Controller, View pages and Model(I)
-
Spring Boot provides servers too (Embedded Server) No need to Download and Install
- Apache Tomcat (default)
- JBoss undertow
- Eclipse Jetty
- Webservices
- Rest webservices
- REST APIs and Endpoints
- Consumer Applications
- Testing(POSTMAN/Swagger/JUnit)
- Microservice(MS#) Introduction
- Patterns used in MS#
-
It is a process of Integration of applications (any language/any technology)
-
Linking Two different projects which are running in two different servers.
-
By using Webservices we are not developing applications. We are just linking/integrate applications.
-
ToDo Integration using 4th layer IL - Integration Layer. This layer communicates with Service Layer of same project and IL of another Project
-
Webservices is implemented using HTTP protocol and data is exchanged using XML/JSON Formats.
-
Here, two applications required for Webservices, they are Producer (Service Provider) and Consumer (Service Reader).
Ex:
Loading Amount to PayTm using ICICI-Bank
Producer = ICICI Bank , Consumer = PayTM\Do payment using PayTM after Selecting Movie Ticket in BookMyShow
Producer = PayTM , Consumer = BookMyShow -
One application may behaves as either producer or consumer (even both based on requirement).
-
There can be multiple producer and consumers exist in network. But atleast 2 apps (1 producer and 1 consumer) required for integration.
-
Consumer and Producer might have implemented in different languages. In that case data is exchanged using Global Data Formats(XML/JSON).
-
Java Object can be converted into JSON Format even reverse is possible using JACKSON API.
-
Java Object can be converted into XML Format even reverse is possible using JAXB API [Java Architecture for XML Binding].
- It is a Object is java-script language.
- But later used in Webservices Concept.
- It is light weight (less memory) compared to Programming language obejcts
- it is global in format.(Every language can read it) Syntax:\
{
"key" : value, "key" : value, ....
}
-
Key must be placed in quotes , value is quoted only if String type. ex:
{ "eid": 101, "ename": "ABC", "esal": 500.0 }
-
When we add Spring Web Dependency in Boot applications, implicite conversions for Object--JSON
-
Two applications are connected using Standard Protocol with Global Data formats that makes application Integration. Such architecture is called as ReST.
Re - Representation (Global Format)
S - State (Date)
T - Trasfer (Exchange) -
HTTP + JSON/XML --- combination is used.
-
Two applications are used.
a. Producer ( Rest never used to design UI part)
b. Consumer ( access producer/may contain UI ) -
RestControllers are even like normal Controllers but never connected with any UI (View or ViewResolvers). It Returns Response back to consumer
@RestController = @Controller + @ResponseBody -
Return Types can be String, ClassType/Custom Type, Collection, ResponseEntity<T>
-
If ReturnType is non-String type, Then it will be converted into JSON/XML.
-
All Support all HTTP methods
GET : To fetch Resource from Producer to Consumer
POST : To create new Resource at Producer
PUT : To Modify existed Resource at Producer
DELETE : To Remove existed Resource at Producer
PATCH : To partially update Resource at Producer\Database Operations --- HTTP Protocol
INSERT --- POST
UPDATE --- PUT/PATCH
DELETE --- DELETE
SELECT --- GET -
HTTP Methods are recomanded standards. If they used in wrong way still you may get output. They are like contract b/w Producer and Consumer.
ResponseEntity<T>: ResponseEntity is a final Response given by Producer app, Which mainly contains Data(Body) and Http Status along with Headers..etc
- ResponseEntity later converted into JSON/XML format (For non-String).
Http Status (5)
1xx -------------- Informational
2xx -------------- Success
3xx -------------- Redirect
4xx -------------- Client Side Error
5xx -------------- Server Side Error\ - Http Status are common codes used to indicate current status of request process for a consumer given by Producer.
POSTMAN : It is a testing tool used to make HTTP Request that support all types of Http method.
-
Project is a collection of Modules. [ Application is a collection of Service]
ex: Amazon Application
Services: Search, Cart, Payment, Feedback, Trace...etcGmail Application
Services: User (Register, Login), Inbox, Sent, Drafts..etcFacebook Application
Services: Register, Login, Post, Comment, Like, Friends , Chat ..etc
Monolithic Application : Creating a single deployable component (single .jar or .war) for a Project is called as "Monolithic Application"
-
Finally above projects converted into Amazon.war, Gmail.war ..etc
compile : .java ---> .class
Build : .class (+HTML,CSS,Images/..etc) - .jar / .war
jar - java archive (stand alone application type)
war - web archive (web application types) -
deploy : Place build file inside server and start server.
-
Service Instance / Instance :- A successful Deployment setup/Environment
-
Load : Client Request (internally one Request = one threads)
-
Max Load : Max no.of request allowed by server (for parallel processing)
- Default is : 200
- server.jetty.threads.max = 200
- server.tomcat.threads.max = 200
-
Load, MaxLoad, LoadFactor
- Load = Current no.of Request
- Max Load = max no.of Request allowed
- LoadFactor = Current Load/ Max Load
LoadFactor = Current Load/ Max Load
= 100 / 200
= 0.5
0 <= LoadFactor <= 1
-
0 <= LoadFactor <= 1
-
Scaling:- it is a process of increase the performance of system/application.
Horizontal Scaling : Creating multiple Service Instances and handle using LBS(Load Balancer Server) is called as Horizontal Scaling.
Vertical Scaling : Enhance System hardware capacity. ie RAM, CPU ,HardDisk/SSD..etc
Load Balancer Server :- It will dispatch request to Actual Service instance.
-
It will choose an Instance which has Less Load Factor.
-
If all are having same LF then choose any one Instance in Random.
-
LBS will not process request.
-
It will dispatch request to ServiceInstance
-
Every ServiceInstance is identified using InstanceId(HexaDecimal Value)
Batch Processing : Transfer large amount of data from source to destination with some operations in between.
Ex: CSV File (10000) --> MySQL, MongoDB ---> XML File
Job : Actual task that indicates sending data from source to destination. Step : One Job can have 1 to n Steps. Reader : Reads data from Source. Processor : Will perform Operations on the data read by the reader. Writer : Writes data to the destination. Job Launcher : Start Job Execution. Job Repository(H2/MySQL) : Stores all details of jobs.
-
IteamReader<T> (I)
read():T -
ItemProcessor<I,O> (I)
process(I i):O -
ItemWriter<T> (I)
write(List<T>):void -
Step (I)
StepBuilderFactory(C) = stepName + reader + processor + writer + chunk (int) -
JobExecutionListener(I)
beforeJob(je)
afterJob(je)
optional to use , to log/trace details/batch status ..etc before and after job. -
Job(I) = JobBuilderFactory(C) = name + steps + execution order + listener
FlatFileItemReader<T> : This is a pre-defined reader class given by batch api, used to read data from a file and convert to Objects(rows as Objects)
JdbcBatchItemWriter<T> : This is a pre-defined writer class given by batch api, used to write data to SQL Database.
JAAS : Java Authentication and Authorization Service
Authentication : Authentication is validating username and password. Authentication is 3 types.
- InMemory Authentication : Using RAM (temp) to store user data [Testing purpose only]
- JDBC Authentication : Using Database + JDBC(SQL) to store and validate user data
- UserDetails Service / ORM Authentication : Using Database + Data JPA to store and validate user data
Authorization : Validate User Role. Authorization is 3 types.
-
permitAll : URL can be accessed by everyone [ No login is required ]
ex: /home, /register, /contactUs, /welcome -
authenticated : URL can be accessed only after login [only login + any role is valid]
ex: /profile, /inbox, /settings, /logout -
hasAuthority : Must Login + Matching Role to access URL
ex: /approveLoan [ Login + Role(MANAGER) ]
Ex: Bank Application:-
Customer --> Open A/c, checkBal , Req Loan
Manager --> View Loan Req, Approve Loan
Cashier --> Deposite, Withdraw ..etc\
Role : Decides what are operations allowed to a user.
- Filters are components used to execute any logic over request(Pre-Process)/response(Post-Process) connected with a servlet.
- Spring Security has given pre-defined filter named as: DelegatingFilterProxy.