joplin-notes/joplin notion extension/Web Development/Java/Spring/Coding 1 (Autowire and Dependency Injection).md
2026-11-04 00:33:23 +05:30

239 lines
No EOL
5.4 KiB
Markdown

Coding 1 (Autowire and Dependency Injection)
- Once starter project is ready its time to do some other stuff
- Create POJO (Plain old java object)
In `com/example/demo` where there is java file create another file containing classA `classA.java`
```java
package com.example.demo;
@Component
public class ClassA {
private int aid;
private String aname;
private String tech;
public int getAid() {
return aid;
}
public String getAname() {
return aname;
}
public String getTech() {
return tech;
}
public void setAid(int aid) {
this.aid = aid;
}
public void setAname(String aname) {
this.aname = aname;
}
public void setTech(String tech) {
this.tech = tech;
}
}
```
- Now linking POJO with demo without DI
```java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context= SpringApplication.run(DemoApplication.class, args);
ClassA a = new ClassA();
a.show();
}
}
```
- Now linking POJO with DI.
```java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context= SpringApplication.run(DemoApplication.class, args);
ClassA a = context.getBean(ClassA.class);
a.show();
}
}
```
- Theory : SpringApplication.run will create a spring container and it will contain objects also called spring bean. When spring bean are created we have 2 different scopes in spring core(base version without any web, jdbc,etc) ie Singleton and Prototype. Singleton is the default one. Whenever a container is created object of class with @Component annotation will be created too. if we want them to be created manually then use Prototype scope. Advantage of protoptype is that multiple objects can be created while in singleton same object is returned everytime.
- Add the scope prototype like this
in `ClassA.java`
```java
@Component
@Scope(value="prototype")
public class ClassA {
private int aid;
private String aname;
private String tech;
public ClassA(){
super();
System.out.println("Class A object created");
}
public int getAid() {
return aid;
}
//....
```
In `DemoApplication.java`
```java
ClassA a = context.getBean(ClassA.class);
a.show();
ClassA a1 = context.getBean(ClassA.class);
a1.show();
```
- Create another POJO named Laptop
in `Laptop.java`
```java
package com.example.demo;
import org.springframework.stereotype.Component;
@Component
public class Laptop {
private int lid;
private String brand;
public String getBrand() {
return brand;
}
public int getLid() {
return lid;
}
public void setBrand(String brand) {
this.brand = brand;
}
public void setLid(int lid) {
this.lid = lid;
}
@Override
public String toString() {
// TODO Auto-generated method stub
// return super.toString();
return "Laptop[lid="+lid+", brand="+brand+ "]";
}
public void compile(){
System.out.println("Compiling");
}
}
```
In `ClassA.java`
```java
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
// @Scope(value="prototype")
public class ClassA {
private int aid;
private String aname;
private String tech;
@Autowired
@Qualifier("laptop")
private Laptop laptop;
public ClassA(){
super();
System.out.println("Class A object created");
}
public int getAid() {
return aid;
}
public String getAname() {
return aname;
}
public String getTech() {
return tech;
}
public void setAid(int aid) {
this.aid = aid;
}
public void setAname(String aname) {
this.aname = aname;
}
public void setTech(String tech) {
this.tech = tech;
}
public Laptop getLaptop() {
return laptop;
}
public void setLaptop(Laptop laptop) {
this.laptop = laptop;
}
public void show()
{
System.out.println("in show");
laptop.compile();
}
}
```
- As ClassA is using object of Laptop class means it is dependent on Laptop class so Autowired annotation is used to link with bean of object type `Laptop.class`. If we want to link with object name then we have to use Qualifier annotation and provide name in string form inside parenthesis. By default object name is class name with all the character as lower case and in this case it will be `laptop`. it doesnt depend on name of instance created at ClassA. In order to give custom object name
In `Laptop.java`
```java
@Component("lap")
public class Laptop {
```
and in `ClassA.java`
```java
@Autowired
@Qualifier("lap")
private Laptop lapto;
```