Spring IOC
Spring IOC
Prerequisites:
1)Core java
2)jdbc
3)Hibernate
4)SQL
5)Html and css
Spring framework:
1)Spring core(IOC)
2)Spring DataJPA
3)SpringMVC
4)Restful WebServices
5)Microservices
Spring Initializer:
-> Spring Intializr is used to create spring boot applications
start.spring.io
-> When we create boot application using initializr it will give the
application in the form of zip file.
-> We need to extract that zip file and we should import that project
into our IDE.
-> We can create spring boot application directley from IDE but
internally IDE will interact with start.spring.io website to create
the project.
- 01-SpringBoot-App
- src/main/java
- com.bikkadit
- Application.java
- src/main/resources
- application.properties
- src/test/java
- com.bikkadit
- ApplicationTest.java
- Maven Dependencies
- jar files
- pom.xml
-> Spring Boot application we have created using Maven build tool
-> Spring Boot start class is also called as Main class in boot
application.
-> Spring Boot application execution will begin from main class only.
------------------------------Application.java------------------------
---
@SpringBootApplication
public class Application {
1) data source
2) smtp
3) actuators
4) kafka
5) redis etc..
----------------------------------------------------------------------
---
-> In boot application by default test class also created
@SpringBootTest ----> To represent class as SB test class
-------------------------------ApplicationTest.java-------------------
---
@SpringBootTest
class ApplicationTests {
@Test
void contextLoads() {
}
}
----------------------------------------------------------------------
---
-> In Boot application by default we will get below 2 dependencies
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-
test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Internals of Boot start class
----------------------------------------------------------------------
--
-> When we create boot application we will get start class by default.
-> Start class is the entry point for boot application execution.
@SpringBootApplication
public class Application{
-> Spring Boot application execution will begin from that start class
only.
-> That boot start class contains main method which calls
SpringApplication.run(..) method
----------------------------------------------------------------------
--
@SpringBootApplication
public class Application{
1) Reactive Application
2) Web Servlet Application
3) Default Application(Standalone Application)
-> In Spring Boot, based on our application type IOC will be created.
Standlone : AnnotationConfigApplicationContext
WebServlet: AnnotationConfigServletWebServerApplicationContext
Reactive: AnnotationConfigReactiveWebServerApplicationContext
Runners in Spring Boot
----------------------------------------------------------------------
--
-> Runners will be called from run ( ) method.
-> Runners are used to execute the logic only one time once the
application got started.
1) ApplicationRunner
2) CommandLineRunner
----------------------------------------------------------------------
---
a) Standalone applications
b) Web applications (Customer 2 Business Communaction)
c) Distributed applications (Business 2 Business
Communication)
a) Spring Core
b) Spring Context
c) Spring DAO
d) Spring ORM
e) Spring AOP
f) Spring Web MVC
Note: Spring Core Module is base module for other modules in Spring
Framework.
a) IOC container
b) Dependency Injection
----------------------------------------------------------------------
---
When Spring framework is already available why Spring Boot Came into
picture?
----------------------------------------------------------------------
---
-> Spring framework providing lot of things for us but We have to
integrate all those things by writing lot of configuration.
Note: Spring Boot is not replacement for Spring framwork. Spring Boot
promoting Spring framework.SpringBoot is an enhancement for spring
framework.
----------------------------------------------------------------------
---------------------------------------------------
-> Spring Boot is not replacement for Spring framwork because Spring
Boot Internally uses Spring framework only.
----------------------------------------------------------------------
--
Spring Boot Advantages
----------------------------------------------------------------------
--
-> Starter Poms
1) Starter Poms
a)spring-boot-starter-web
b)spring-boot-starter-data-jpa
c)spring-boot-starter-mail
d)spring-boot-starter-security
e)spring-boot-starter-actuator
f)spring-boot-starter-webflux
2) Version Management
3) Auto Configuration
-> Spring Boot providing Embedded Servers to run our web applications.
Tomcat is the default embedded server in spring boot.
We can configure external servers like Jetty and undertow.
5)Actuators
-> Actuators are used to provide production ready features from the
application. Actuators will help us in monitoring and maintaing the
application.
a) classes loaded
b) threads running (thread dump)
c) objects created (heap dumps)
d) url mapping
e) last 100 http req info
f) config Props
g) env
h) health
i) info etc....
@SpringBootApplication annotation
----------------------------------------------------------------------
---
-> @SpringBootApplication annotation is used to represent start class.
----------------------------------------------------------------------
---
@SpringBootApplication
public class Application{
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
@ComponentScan:
Component scan will follow package naming convention to scan for the
classes which are
represented as spring bean.
The package names which are starting from base package name is called
as sub package.
@Configuration
@Bean
@Controller
@RestController
@Autowired
----------------------------------------------------------------------
---
@Component : To represent java class as Spring Bean.
-> In java, we can use below 2 ways to call one class method in
another class method.
1) Inheritence
2) Composition
-> To avoid tighlty coupling among the classes Spring provided IOC &
DI.
----------------------------------------------------------------------
-------------
Q) What is Dependency Injection
----------------------------------------------------------------------
------------
-> IOC is a principle which going to manage and colloborate
dependencies among the objects available in the application.
-> IOC will manage the lifecycle of beans those which are available in
application.
-> The process of Injecting one class object into another class is
called as Dependency Injection.
1) Setter Injection
2) Constructor Injection
3) Field Injection
-> If dependent bean object injected into target bean object through
target bean setter method then it is called as 'Setter Injection'.
-> If dependent bean object injected into target bean object through
target bean constructor then it is called as 'Constructor Injection'.
-> If dependent bean object injected into target bean object through
target bean variable then it is called as 'Field Injection'.
Constructor Injection
----------------------------------------------------------------------
---
-> The process of injecting dependent bean object into target bean
object using target bean constructor is called as Constructor
Injection.
-> By default IOC will call 0-param constructor to create bean object.
Setter Injection
----------------------------------------------------------------------
-
-> It is the process of injecting dependent bean object into target
bean object using target bean setterr method is called as Setter
Injection.
Field Injection
----------------------------------------------------------------------
-
-> It is the process of injecting dependent bean object into target
bean object using target bean variable is called as Field Injection.
}
----------------------------------------------------------------------
---------------------
4) It is hiding dependencies
----------------------------------------------------------------------
----------------------------------------
----------------------------------------------------------------------
---
Q) Can we do both setter & Constructor injection for same variable?
----------------------------------------------------------------------
-------------------------------------------------------------
-> In order to perform Dependency Injection we are using @Autowired
-> byType
-> byName
-> no
-> "byType" means auto-wiring will happen based on data type of the
variable
Bean Scopes
----------------------------------------------------------------------
--
-> Bean Scope will decide how many objects should be created for bean
class by IOC container.
1) singleton (default)
2) prototype
3) request
4) session
5) global (It removed from Spring 3.0)
-> By default all spring beans are singleton scope that means IOC
container will create only one object for the class.
-> request & session scopes we will use in Spring Web MVC based
applications.
----------------------------------------------------------------------
--
@Component
@Scope(value = "prototype")
public class Car {
public Car() {
System.out.println("****** Car :: Constructor
*******");
}
}
----------------------------------------------------------------------
@Bean
@Scope(value = "prototype")
public Engine getEngine() {
Engine eng = new Engine();
return eng;
}
----------------------------------------------------------------------
--
-> To specify scope for a bean we will use @Scope annotation like
above.
-> @Scope annotation we can use at class level and method level
(@Bean)
----------------------------------------------------------------------
--