Inversion Of Control → giving control of object creation
, via dependent injection. Someone else is injecting
object .
For example
Service service;
U just give reference.
In the Spring framework, a bean is a Java object that is managed by
the Spring IoC (Inversion of Control) container
Dependency Injection is used by the framework to auto-inject
the dependencies into the beans when beans are created,
How to tell springwork u won’t want object of all classes , u do
that via config file . (first file), or @Component which means
spring boot will create object of this class
We need a server → tomcat server
Spring boot is built on top of spring framework , it is an
opinionated framework , as it gives certain things
start.spring.io
Maven project:Maven is a mature and widely-used build tool
based on XML configuration.
Dependencies and plugins are defined in pom.xml.
Gradle
Gradle is a more modern build tool designed to address some
of Maven's limitations. It uses a DSL (Domain-Specific
Language) for configuration, supporting both Groovy and
Kotlin.
Dependencies are defined in build.gradle using a concise
syntax.
Add dependencies
→Spring Web
Right click run
Springboot favours convention over configuration as it gives u
a lot of default stuff , it gives u to run things u don’t want .
Now, in the main file we have
@SpringBootApplication
public class MyApplication{
psvm(){
SpringApplication.run(MyApplication.class,args);
//this basically creates the IoC container
}
It creates a container : spring has a container inside JVM ,
called an IoC container or spring container , and it creates an
object inside this container.
Dev.java
To call dev object in myApplication:
U can use simply do the general thing OR
Leverage spring boot capabilities of creating objects.
If the type of IoC container is Application Context
ApplicationContext context
=SpringApplication.run(MyApplication.class,args);
//if u click on this run , it return this object
ConfigurableApplicationContext , run is returning u
applicationContext context
Dev obj = context.getBean(Dev.class);
//gives me existing object
obj.func();
------------------------------------------------------------------------------
Autowiring
Now another laptop.java with function compile , to call it in
dev
Public class Dev{
@Autowired //this is known as field injection
private Laptop laptop;
//behind the scene u will get instance of the laptop
void func(){
laptop.compile();
}
}
Option 2;
//constructor injection
public Dev(Laptop laptop){
this.laptop = laptop;
}
Option 3:
Setter injection
For field injection and setter injection u need autowire
Autowiring reduces the efforts of object instantiation by auto injection of
dependencies into beans managed by spring.
@Autowired
public void setLaptop(Laptop laptop){
this.laptop = laptop;
}
—------------------------------
How spring work knows what to connect via autowire .
It does this by working by type :
For example –
Computer → interface
Laptop → class implements computer ///both has
component
Desktop → class implements computer // both has
component
Now in dev
@Autowired
private Computer compute;
To solve confusion
On top of Laptop use
@Primary
But what if u use primary with both
Then u can just write
In dev
@Autowired
@Qualifier(“laptop”)
private Computer comp;
//name of the instance
//by default the instance of Laptop (bean name in IoC)
would //be class name in all small
—-------------------------------------------------------
If u r using spring then u need an xml file
Behind the Scenes
Classes are marked as beans
<beans with that xml tag definition>
<bean id = “dev” class = “com.telsuko.dev”>
//the id would be useful as instead of Dev.class
U can write “dev”
Dev obj = (Dev)context.getBean(“dev”);
//which class u want to manage
<bean>
</beans>
—----------------------------------
To handle the request at server side we need controller
@RestController
public class Contro{
@RequestMapping(‘“/”)
public String greet(){
Return “Welcome”;
}
}
Or
@Controller
public class Contro{
@RequestMapping(‘“/”)
@ResponseBody
public String greet(){
Return “Welcome”;//else it will search for
page named welcome
}
—------------------------------------------------------------------------------
Spring web
Spring boot dev tools
Before Controller spring has front Controller ,
Controller is only for talking and responding to client, use
service layer
Model represents the data
Product.java
ProductService
@Service
Public class ProductService{
Public List<Product> getProducts(){
Return products;
}
So in controller
POSTMAN→
In Product.java
As we r sending data from client to server, it is used to put
data in
. @PathVariable:
● Purpose: Extract values from the URI path.
● Usage: When part of the URL needs to be used as a
method parameter.
● Example Use Case: Extracting an ID or other dynamic
part of the
@ReqestBody:
● Purpose: Bind the HTTP request body to a Java object.
● Usage: When sending data (typically in JSON or XML
format) in the request body, such as for creating or
updating a resource.
● Example Use Case: Sending a Product object in a
POST request to create a new product.
//put for update
@PutMapping(“/products”)
Public void updateProduct(@RequestBody Product prod){
service.updateProduct(prod);
}
//delete /products and product id
@DeleteMapping(“/products/{prodId}”)
Public void deleteProduct(@PathVariable int prodId){
service.deleteProduct(prodId);
}
—-----------------------------------------------------------------------
SPRING DATA JPA
Java persistence API , it is just standard followed by java
Pom.xml mei do h2 , jpa
Application.properties
Spring.datasource.url = jdbc:h2:mem(means in
memory):nameOfDatabase
localhost:8090/h2-console
A repository now
@Repository
Public interface ProductRepo extends
JpaRepository<Product,Integer>{
Tells primary key that Integer,
This JpaRepsitory has lots of methods
In the service use this repository
If u want to use a class as a table u need to right @Entity
What is the purpose of the @SpringBootApplication
annotation?
● Answer: It combines three annotations:
○ @Configuration: Marks the class as a source of
bean definitions.
○ @EnableAutoConfiguration: Enables Spring
Boot's auto-configuration mechanism.
○ @ComponentScan: Scans the package for Spring
components.