RUNNING NOTES
RUNNING NOTES
===============
Pre-Requisites
===============
1) Core Java
- OOPS
- Collectios
- Exception handling
- Multi Threading
- I/O Streams
- Java 8 Features
2) Adv Java
- JDBC
- Servlets
- JSP (optional)
- MVC Architecture
3) Database
- SQL (CRUD)
==============================================
Who should learn Spring Boot & Microservices
==============================================
Freshers
Career Gap
Working Professionals
================
Course Content
================
Module-11 : Tools
- Apache Kafka
- Redis Cache
- Logging
- JUnit
- Docker
===============
Course Details
===============
Backup Videos : 10,000 INR (live classes + material + videos - 1 year) (24 hours)
##########################
Session-02 (04-Nov-2023)
##########################
2) What is Technology
3) What is Framework
=======================
Programming Language
=======================
1) Human Languages
======================
Software Technologies
======================
Ex :
1) get connection
2) create stmt
3) execute query
4) process results
2 Types of logics
1) Common Logics
2) Business Logics
Challenges :
==============
Frameworks
==============
-> If we are using frameworks then we need to focus only on business logics.
1) no duplicate code
2) faster development
3) less lines of code
4) less no of bugs
5) easy maintenence
######################
Session - 03 (06-Nov)
######################
==========================
Layered Architecture
=========================
=============================
What is Spring Framework ?
=============================
=====================
Spring Architecture
=====================
1) Spring Core
2) Spring Context
3) Spring AOP
5) Spring ORM
7) Spring REST
8) Spring Security
9) Spring Batch
=============
Spring Core
=============
1) IOC Container
2) Dependency Injection
3) Auto wiring
Note: Using spring core we can develop application classes with loosely coupling.
================
Spring Context
================
=> AOP is used to seperate primary logics and secondary logics in the application.
===================
Spring JDBC / DAO
===================
Note: Spring JDBC module internally will communicate with JDBC API.
===========
Spring ORM
============
=> Using Spring ORM we can avoid boiler plate code in DAO layer.
================
Spring Web MVC
================
(Customer to business)
1) JSP
2) Thymeleaf
=============
Spring REST
=============
================
Spring Security
================
=============
Spring Batch
=============
==============
Spring Cloud
==============
=> It is used to implement microservices
1) Service Registry
2) API Gateway
3) FeignClient
4) Circuit Breaker etc...
1) What is Framework
3) Struts Framework
4) Hibernate Framework
5) Layered Architecture
6) Spring Framework
7) Spring Architecture
8) Spring Modules
==================================================================
How one java class method can access another java class method
==================================================================
1) By Inheritence (IS-A)
2) By Composition (HAS-A)
===============
IS-A Relation
================
if (status >= 1) {
System.out.println("Journey Started...");
} else {
System.out.println("Failed to start the engine...");
}
}
}
==================
HAS-A relation
==================
if (status >= 1) {
System.out.println("Journey Started...");
} else {
System.out.println("Failed to start the engine...");
}
}
}
=============
Conclusion
=============
=> With IS-A & HAS-A relation our classes are tightly coupled.
=> To develop classes with loosely coupling we will use Spring Core module.
=============
Spring Core
==============
=> By using spring core concepts we can develop classs with loosely coupling.
=> If we want our classes to be managed by IOC then we need to develop our classes
with some rules.
=========================
Strategy Design Pattern
=========================
========================
Constructor Injection
========================
=> Inject one class object into another class obj using constructor.
========================
setter Injection
========================
=> Injecting one class object into another class object using setter method
Ans)
SI will override CI
=====================
Dependency Injection
=====================
=> The process of injecting dependent object into target object is called as
dependency injection.
1) Constructor Injection
2) Setter Injection
3) Field Injection
=====================================
IOC Container (Inversion Of Control)
=====================================
=> It will create dependent object and target object and then it will inject
dependent obj into target object.
====================
What is Spring Bean
====================
=> The java classes which are managed by IOC are called as Spring Bean classes.
=========
Maven
=========
=> Using Maven we can create both stand-alone & web apps.
========================
Creating Maven Project
========================
- groupId : in.ashokit
- artifactId : 01-Spring-IOC-App
- packaging : jar
- Click on Finish
----------------------------------
public interface IEngine {
}
----------------------------------
package in.ashokit.beans;
public DieselEng() {
System.out.println("DieselEng :: Constructor");
}
@Override
public boolean start() {
System.out.println("DieselEng starting.....");
return true;
}
}
----------------------------------------------------------
package in.ashokit.beans;
public PetrolEng() {
System.out.println("Petrol Engine :: Constructor");
}
@Override
public boolean start() {
System.out.println("Petrol Engine starting...");
return true;
}
}
------------------------------------------------------------
package in.ashokit.beans;
public Car() {
System.out.println("Car :: Constructor");
}
}
----------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
-----------------------------------------------------------
package in.ashokit.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import in.ashokit.beans.Car;
System.out.println("=======================");
=> Bean Scope will decide how many objects should be created for spring bean class.
2) Prototype
3) request
4) session
-> For singleton scoped beans only one object will be created by IOC container at
the time of IOC starting (eager loading)
-> For prototype scoped beans IOC will create new object every time based on demand
(lazy loading).
===================
Assinment
===================
Read book data from keyboard and insert into db table using spring-
jdbc.
===========
Autowiring
===========
1) Manual Wiring
2) Auto wiring
=> Manual wiring means programmer will inform to IOC which is dependent obj using
ref attribute like below
=> Auto wiring means IOC will identify dependent object and it will perform DI.
1) byName
2) byType
3) constructor
4) no (default)
========
byName
========
-> Based on target bean variable name IOC will identify dependent bean and it will
inject it.
Note: target bean variable name should match with bean id.
-> If any bean id is not matching with target bean variable name then D.I will not
happen.
========
byType
========
-> Based on data type of variable IOC will identify dependent object.
-> If variable data type is interface then it will check interface implemented
beans.
-> If we have more than one bean which are implementing interface then IOC will get
confused to take decision. Then we will get ambuigity problem.
1) autowire-candidate = false
2) primary = true
=============
constructor
=============
Note: When we go for byName or byType then IOC will perform Setter Injection with
autowiring.
=====
no
=====
========
Summary
=========
1) Layered Architecture
2) P.L vs Technology Vs Framework
3) Struts Framework
4) Hibernate Framework
5) Spring Framework (2004) -> 2022 (6.x)
6) Spring Architecture
7) Spring Modules
8) Tightly Coupling
9) Loosely Coupling
10) Strategy Design Pattern
11) 2 Apps using SDP
12) Dedependency Injection
13) CI & SI & FI
14) IOC Introduction
15) Bean Scopes
16) Autowiring
============
Spring Boot
============
-> Spring Boot is an approach to develop spring based applications with less
configuration.
-> What type of apps we can develop using spring same type of apps we can develop
using spring boot also.
=============================
Advantages with Spring Boot
============================
Ex : spring-boot-starter
spring-boot-starter-web
spring-boot-starter-data-jpa
spring-boot-starter-mail
spring-boot-starter-security
3) Auto Configuration
===========
STS setup
===========
https://cdn.spring.io/spring-tools/release/STS4/4.20.1.RELEASE/dist/e4.29/spring-
tool-suite-4-4.20.1.RELEASE-e4.29.0-win32.win32.x86_64.self-extracting.jar
================================
Create Spring Boot Application
===============================
2) STS IDE
======================================
What is start class in spring boot ?
======================================
===========================================
How IOC will be starting in springboot ?
============================================
=> In springboot run ( ) method will start IOC container based on pom starter.
=> If we add spring-boot-starter then it will use below class to start IOC
=> If we add spring-boot-starter-web then it will use below class to start IOC
=> If we add spring-boot-starter-webflux then it will use below class to start IOC
Class : AnnotationConfigReactiveWebServerApplicationContext
==================================
What is @SpringBootApplication ?
==================================
==============================================
How to represent java class as Spring Bean ?
==============================================
@Component
class UserDao{
==============================================
How component scanning will work internally ?
==============================================
=> boot will scan base pakage and its sub packages
Note : The pkg name which is starting with base pkg name is called as sub package
of base package.
in.ashokit
- Application.java
- UserDao.java
- ReportDao.java
in.ashokit.service
in.ashokit.security
in.ashokit.util
com.tcs
- UserService.java
in.ashokit
com.tcs
@SpringBootApplication
@ComponentScan(basePackages = {"in.ashokit", "com.tcs"})
public class Application {
System.out.println(count);
}
}
==============================
What is @Bean annotation ?
=============================
=> To customize bean obj creation we will write a method with @Bean annotation like
below...
@Configuration
public class AppConfig {
public AppConfig() {
System.out.println("AppConfig :: Constructor");
}
@Bean
public AppSecurity createSecurityObj() {
AppSecurity as = new AppSecurity("SHA-256");
return as;
}
}
- Security Customization
- Kafka customization
- Redis customization
- Swagger Customization
@Component
@Scope
@Configuration
@Bean
@ComponentScan
============
Autowiring
=============
@Autowired annotation
2) constructor level
3) field level
@Component
public class UserService {
@Autowired
private UserDao dao; // field injection (reflection)
1) setter method
2) Constructor
3) Field
@Autowired
package in.ashokit.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import in.ashokit.dao.IUserDao;
@Component
public class UserService {
@Autowired
public void setPwdService(PwdService pwdService) {
this.pwdService = pwdService;
}
@Autowired
public void setUserDao(IUserDao userDao) {
this.userDao = userDao;
}
@Autowired
public void setEmailService(EmailService emailService) {
this.emailService = emailService;
}
// encrypt pwd
String encryptPwd = pwdService.encryptPwd(pwd);
// save user in db
boolean isSaved = userDao.saveUser(name, email, encryptPwd);
// send email
if (isSaved) {
boolean isSent = emailService.sendEmail(email, "Test Subject",
"Test Body");
if (isSent) {
System.out.println("User Registration Completed...");
}
}
}
}
========================================================
@Service
public class UserService {
@Autowired
private PwdService pwdService;
@Autowired
private IUserDao userDao;
@Autowired
private EmailService emailService;
// encrypt pwd
String encryptPwd = pwdService.encryptPwd(pwd);
// save user in db
boolean isSaved = userDao.saveUser(name, email, encryptPwd);
// send email
if (isSaved) {
boolean isSent = emailService.sendEmail(email, "Test Subject",
"Test Body");
if (isSent) {
System.out.println("User Registration Completed...");
}
}
}
========================================================
@Component
@Scope
@Configuration
@Bean (method level annotation)
@Autowired
@Qualifier
@Primary
@Component
===========================================
Q) @Component vs @Service vs @Repository
===========================================
=======================
Banner in Spring Boot
=======================
1) Console (default)
2) Log
3) Off (banner will not print)
location : src/main/resources
URL : https://patorjk.com/software/taag/#p=display&f=Graffiti&t=Ashok
%20IT
=======================
Runners in Spring Boot
=======================
=> Runners will execute only one time when the application starts.
===================
Bean Life Cycle
===================
Thread Lifecycle
============================================
Bean Life Cycle with Programmatic Approach
============================================
InitializingBean - afterPropertiesSet ( )
DisposableBean - destroy ( )
------------------------------------------------
@Component
public class Car implements InitializingBean, DisposableBean {
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("Car starting.....");
}
@Override
public void destroy() throws Exception {
System.out.println("Car stoped...");
}
}
----------------------------------------------------
==================================
Bean Life Cycle with Annotations
==================================
-----------------------------------------------------------------
@Component
public class Motor {
@PostConstruct
public void m1() {
System.out.println("Motor - init() method");
}
@PreDestroy
public void m2() {
System.out.println("Motor - destory() method...");
}
}
----------------------------------------------------------------
1) Spring Vs SpringBoot
===============
SI vs CI vs FI
===============
FI --> not recommended to use
SI --> First Target obj & then dependent obj will be created.
CI --> First dependent obj will be created & then target obj will be created.
@Component ->