SpringBoot-MS-Dec-07
SpringBoot-MS-Dec-07
Dilip Singh
11 years
Still I am working
Java Devloper
Java : Spring < Spring Boot , MicroServices
Demo:
Java : Spring FW
SpringBoot + MicroServices ?
Spring FW :
Spring Boot : 100 % both are same w.r.to Logic/Progrmaing
Current Versions:
=================
Spring FW : Spring 6
Advantages:
----------
Dependency Injection :
Core Java :
---------
DI :
Dependency is Address:
injected into Employye.
Addreess.java
package co.dilip;
String city;
int pincode;
--> Pass Class information to Spring FW , so that Spring Will create Objects
for Address class and maintined that object by SPring FW.
package co.dilip;
String name;
int id;
--> Pass information of your calss, as well as which Object you are having
dependency provide that information also to Spring FW. So that, SPring will inject
that dependecy Object into Curent Object.
(Inversion of Control) :
Inversion of Control
Inversion : Opposite/reverse
Controle : Managing
=========================
No
Address
Student
1. XML Configuration
1. XML Configuration:
Address {
f}
7.
1. Spring Intro :
Spring FW will create Objects for the Classes.
when we provided class infor mation to Spring via Configuration, then only Spring
FW will create objects and mnages the Object.
Spring : How many Bean Objects can be cretaed for a Bean class?
many
1. Setter Injection:
Rule :
1. To achive Setter injection, we should define always a default
constructor.
2. We should define setter methods always
<beans>
<bean id="student1" class="com.dilip.spring.di.Student">
<!-- id : 100 , name: DIlip , avg : 88.00 -->
<property name="studentId" value="101"></property>
<property name="studentName" value="Dilip Singh"></property>
<property name="avgOfMarks" value="88.00"></property>
</bean>
</beans>
Data Types :
8 Data Types
Wrapper Cllases : 8 primitive
<property name value>
int long short byte double long char boolean
Non-Primitive:
String
<property name value>
Map:
Map :
key-value
Assignmemnts :
Map<Integer, String>
sid, name
Map<Long,String>
Mobile , Names
Non-Primitive:
Employye
Student
Swiggy Order
Adress.
.....
References of Classes:
ref : this attribute will be used to refer another Bean Object which has to be
injected.
500072
HYD
TS
: 100 Customers
Spring :
1 Bean Object : Address
Customer:
========================================
2. Constructor Injection:
CI is part of DI.
DI injection will hapens via constructors of the Bean calsses
Rules :
1. while defining <constructor-arg> values we should follw an order of
Constructor Parmas Order
3. For example , I would like to inject only Id and Name , don't want price
Not Posiible, We should pass 3 values 100%
new Product(1,"vall");
public Product() {
}
new Product(1); // 1 value : 3 values expecyed
public Product(int pid) {
}
new Product(1, "phone"); // 2 value :
public Product(int pid) {
REq : please write functionality, when I created Object only id value must
be assigned.
REq : please write functionality, when I created Object id and name values
must be assigned.
public Product(int pid, String name) {
public Product() {
new Product();
Question : The current Product class is eligible for Setter Injecction or not?
Eligible,
where i can inject any value of property any time for any bean Object.
0 values :
<bean id="mouse" class="com.naresh.spring.di.ci.Product">
</bean>
1 value : id
<bean id="mouse" class="com.naresh.spring.di.ci.Product">
<property name="productId" value="6677"></property>
</bean>
1 value : price
<bean id="mouse" class="com.naresh.spring.di.ci.Product">
<property name="price" value="6677"></property>
</bean>
3 values : id name price
<bean id="mouse" class="com.naresh.spring.di.ci.Product">
<property name="productId" value="6677"></property>
<property name="productName" value="Dell Mouse"></property>
property name="price" value="2444"></property>
</bean>
Req : where i can inject any value of property any time for any bean
List :
Assignment :
Non-Primitives :
SI : <property ref="otherBeanID">
1. Objecte will be created with Default Construcotr
2. defaault values for all properties of an Object;
3. Depends on <property> , setter methods
CI :
1. Object will be created always by using parameterized Constructor
2. Properteis oB Object will be initilized at the time Object creation
3. 100% passing all values for constructor
Hi Sir, earlier we have not used default constructor for setter methods still its
working & currently we are recommended to use default constructor with setter
methods.
Assignment :
Product{
OrderDetails{
Product
//Setters //Construcotrs
}
Bean Scopes:
------------
Bean Scopes:
-----------
when Spring Conatiner created the Bean Object, Once Bean Objects are created By
Conatiner, Conatiner will provide a scope for that bean Objects.
When we are not defined any scope , interanlly Spring will conside
scope=singleton
1. singleton:
This is the default scope for every Bean Object crreated By Conatiner.
That mean, for every bean Object Configuration, Spring Conatiner creating
bean Object, and Spring maintianing that Object as a singleton Object.
Employye
Product
container.getBean();
getBean();
As per Singleton DP class, only one Object has to be avilable for that class
in entire application memorly level.
2. prototype
Whenever We are getting Bean Object from Container, Conatiner will create and
return always a new Object with configuration values.
Req: I would like to get every time new Object from Container:
3. request:
Many Requests
For evry new HTTP request from FE, a new Bean Object wil be created by
Conatiner.
4. session:
HpptSession
foe evry HttpSession Object creation, Spring will create new Bean
Object assocuted to Http Session.
5. application/global-session
across mupltile sessionss , if we wan to sahre some information,
Spring Boot:
============
Spring FW
Spring Boot :
Spring FW
-> Add on features of Spring Boot.
minimum fuss:
Maven :
What is Maven ?
pom.xml
================================================== =======
Maven is tool/software :
5 jar files
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.9</version>
</dependency>
</dependencies>
JDBC :
oracle
target : target folder will conatins all compiled .calls and as well as war file
jar files
Maven goals :
clean : it will clean all .class files from project .java
Spring Boot:
------------
Creation Of Project :
https://start.spring.io/
2. From IDE's
Eclipse
+ Install aplugin called as Spring tools Plugin
IntelliJ
Eclipse + plugins related Spring Boot = STS : Spring Tool Suite
https://spring.io/tools/
spring-tool-suite-4-4.21.0.RELEASE-e4.30.0-win32.win32.x86_64.self-
extracting.jar
STS:
Maven :
Project Metadata :
1. Are you added any jar files information mnaullay to your project ?
No
2. Logic wise , is it Spring or not?
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>6.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId> Core Module
</dependency>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId> JPA Module
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> Web Module
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc11</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Assignment-
-----------
Student
College
1. Primitives
String
References
Collection
List
Set
Map
SI
CI
Scopes
Now we are creating bean calsses and Objects with help Java Annoataions
50 > Annotations
Annotaions Driven
Product{
XML :
1. Java classes
2. create XMl file
3. configure beans in side xml file
<bean>
........
</bean>
Annoataions:
------------
@Configuration
@Bean
@Configuration :
this aanoataion should be used along with a class.
===> Now we have to configure our Bean classes in side Config class.
// Container please refer Spring Bean Config class which is provided as constrct
params
ApplicationContext context =
new AnnotationConfigApplicationContext(SpringBeansConfiguration.class);
// Now Spring Conatiner loads, SpringBeansConfiguration class and find out how many
bean methods
we are Written
// Spring Conatiner will execute all @Bean Methods
// Once @Bean methods are executed by Conatiner, returned Objects will be
maintined as Bean Objects inside //by Spring Conatiner
what will happen , if we use @Bean with same value (iPhone) in second method.
If we are not provided bean Id/name as part of @Bean , then Spring will assign
method name as Bean Name.
Assignment:
Req :
Employee{
id
name
Only One Bean Object has to be created with default values of that Object
@Configuration
BeansConfig{
@Bean
Employye getEmp(){
@Component:
----------
Now, Spring Container will scans Component classes and Sprinf Conatiner will create
an Object for that Component class
@Component
class Emplpoyye{
}
we should inform to container, where our Component class written, Pakcage name.
many packages :
@ComponentScan:
===============
as part @ComponentScan , we will provide package names where out Component classe
are located
@Component :
In this case, Spring will asiign Calss name Bean Id. camel Case
base package :
--------------
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;
com.icici........
com.naresh.meetins
please make sure base packge name is always, where evr SpringbootApplication class
is avilable.
com.naresh.spring.one
com.naresh.spring.one.twi
com.naresh.spring.one.tow.three
com.naresh.spring.four
1. @Bean Method
2. @Component
Use Cases :
==========
Many
Can I achive by using @Component to create more than One Object by Conatiner?
No
@Component
6. Define a class , which object should be created By Default and I want to many
Objects as and when required?
@Component
=====================================
We have to add annoataion on field level where we are havinf dependency of Objects.
@Component
public class College {
@Autowired
private Address address;
}
Address -> College
@Qualifier:
==========
This Annoataion is used always along with combination of @Autowired
S1 , S2 : Insatnces
USe case :
===========
When I have more than One Bean Object of a Dependecy class : Address, I can't
use @Autowired individually.
Using @Qualifier.
Req : eventhough, When I have more than One Bean Object of a Dependecy class
:
@Autowired individually.
A Super
B,C : Sub classess
D{
A a;
College{
int id;
String name;
itn year;
double fee;
@Value("${<key-name>}")
application.peoperties
application.yaml
Spring Boot :
===========
It will take care of Componet scan and Configuration functionlaities when we are
followinf below rule.
com.naresh.spring.abc...
com.naresh.spring.xyz...
Configuration and Component Scan
@SpringBootApplication :
Lombok:
------
POJO classes :
==============
45 MS :
OrderDetails :
150 properties
5 properties : remove
delete 5 varibales
1000 lines
lombok:
2000 POJO classes
150 properties
@Value Annoataion :
==================
@Value("${propertyname}")
Field Injection:
-----------------
Setter Injection
:
====================
Constrcutor Injection:
===============================
Injecting Sub/Impl classes as a List of References:
====================================================
List of Values :
[com.naresh.spring.order.example.Elephant@24528a25,
com.naresh.spring.order.example.Lion@17ae98d7,
com.naresh.spring.order.example.Tiger@59221b97]
[com.naresh.spring.order.example.Tiger@59221b97,
com.naresh.spring.order.example.Elephant@24528a25,
com.naresh.spring.order.example.Lion@17ae98d7]
[com.naresh.spring.order.example.Lion@d71adc2,
com.naresh.spring.order.example.Tiger@3add81c4,
com.naresh.spring.order.example.Elephant@2fb68ec6]
deciding index of Collection Object while injecting all sub/impl classes Object
Interface : Animal
----------------------------
A -> X, Y, Z
@Component
XYZ
@Component
MNO
@Order(-1)
[com.naresh.spring.order.example.Tiger@17ae98d7,
com.naresh.spring.order.example.Elephant@57dc9128,
com.naresh.spring.order.example.Lion@24528a25]
Runners : These are Spring Beans which are implemented from Prdefined Spring Boot
Runners
Runneres are executed only one time immediatley once run() of Spring Application
is exucted.
1. CommandLineRunner
2. ApplicationRunner
Runner Class
I am in MyCommandLineRunnerOne method
I am in MyCommandLineRunnerTwo method
I am in MyCommandLineRunnerFour method
========================================
Configure : DI
// Custom logic
//
destroy :
Calculation :
When a specifc Bean OBject is created and then Immediately some logic has to be
executed, we can define Spring Bean life cycle manually
XML :
attributes
Annotaions : we can define our own methods and we will annoatate thos methods
Interface : Pre Defined Abstract methods
Interview Questions
https://github.com/dilipsingh1306/practice-core-java
Assignment