0% found this document useful (0 votes)
19 views

Day4 - Coding With Spring Boot - Spring Container & Annotations - 7nov

Uploaded by

Prasanta Agasti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Day4 - Coding With Spring Boot - Spring Container & Annotations - 7nov

Uploaded by

Prasanta Agasti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

DIAGRAMS

LIVE NOTES:
Day4: Coding with Spring Boot!!!
Starting now!

Spring & Spring Boot... Java based project dev.


IDE
Jar - Build tool: Maven
start.spring.io - 1st

Spring Boot starter: "Spring web"


MVC + Rest API

java -jar filename.jar


tomcat embedded

clean package
clean spring-boot:run

skip testcases

Coding with Spring Boot!!!

Instead of passing params.. we want to pass as object..


1 object having both the num1 & num2

Spring app
java class to hold num1 & num2

JSON body
{
"num1" : 5,
"num2" : 10
}

with @RequestBody, spring converts json to java object..


Jaxson library
Layered application

controller
entry & exit
routing

service
business

DAO / respository
all db related logic

these layers will be coded using corresponding packages...

- move the add logic to service layer..

- your logs should tell flow of execution of your application.

========

Spring Boot:
managing dependencies (Starters)
Auto config
embedded tomcat
application.property default config
annotation based (No need to xml config)

Spring....

Core Spring System

IOC (Inversion of Control)


Hey developer, you don't create objects, Spring will create it & give you wherever you
need.
Occupy heap.. gc.. DB connection.. system hangs..

Use annotations

Developer should not create java object.. rather Spring should create it.
Developer is supposed to tell spring, which objects it should create & manage.

100 java classes..


20 objects

@Component
add logger in constructor & test, if object is created or not.

Ask spring, give this object to us in controller, so we can use it for calling addmethod.

@Autowired

do experiments with both annotations..

How is spring able to do this.???

Spring/IOC Container
BeanFactory Container
ApplicationContext Container

For spring to give you reference, the object needed reference (Autowire), that object should also
be managed by Spring Container..

multiple other annotations.. spring manages..

@RestController
RestAPI

@Service
core business logic
@Responsitory
DB logic

@Controller
MVC - UI/View

@Configuration

@ControllerAdvice
Globals ErrorHandling

@Component
Util
Helper

The objects which are managed(Container) by Spring is called as "Spring Bean"

How can controller get the service (Both are spring beans)

Dependency Injection
It is given to you directly
When you call @Autowired on Field

Field injection
Constructor injection (better than field injection)

When using Constructor injection, no need to use @Autowired.. spring would by default
inject the Spring bean to you.

- Take inputs from tool for better coding...

Dependency Lookup
You need to ask & take

1. Spring Bean is available in Container

1. Get access to Container


Dependency inject
private ApplicationContext applicationContext;

2. From container you need to get spring bean.


applicationContext.getBean(CalculatorService.class);

========
If you want to currently how many spring beans are there inside Container...

String[] beanNames = applicationContext.getBeanDefinitionNames();


for (String beanName : beanNames) {
logger.info("Bean name: {}", beanName);
}

========

What if, predefined classes are there.. you want to make them as Spring Bean???
Random random = new Random();
int randInt = random.nextInt(100);

We want to make Random as SpringBean

@Configuration
@Bean

write a method where return type is of that prefined class. And in that method, create object of
that predefined class & return object. Annotate this method with @Bean

@SpringBootApplication
@Configuration
@EnableAutoConfiguration
@ComponentScan

APIs & Rest Standards

From Week2 only PAID guys would continue for internship..


Week1, Mon-Fri

FREE is destroying our economy & our country..


USD = INR

Layoffs..

When money is exchanged for valuable service/product..

Program(1/2) + your hard work => 4-5lac

You might also like