Spring Boot Final
Spring Boot Final
start.spring.io
Gradle
Add dependencies
→Spring Web
psvm(){
SpringApplication.run(MyApplication.class,args);
//this basically creates the IoC 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.
ApplicationContext context
=SpringApplication.run(MyApplication.class,args);
------------------------------------------------------------------------------
Autowiring
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;
—----------------------------------
To handle the request at server side we need controller
@RestController
public class Contro{
@RequestMapping(‘“/”)
public String greet(){
Return “Welcome”;
}
}
Or
@Controller
@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 ,
ProductService
@Service
Public class ProductService{
So in controller
POSTMAN→
In Product.java
As we r sending data from client to server, it is used to put
data in
. @PathVariable:
@DeleteMapping(“/products/{prodId}”)
Public void deleteProduct(@PathVariable int prodId){
service.deleteProduct(prodId);
}
—-----------------------------------------------------------------------
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