Day4 - Coding With Spring Boot - Spring Container & Annotations - 7nov
Day4 - Coding With Spring Boot - Spring Container & Annotations - 7nov
LIVE NOTES:
Day4: Coding with Spring Boot!!!
Starting now!
clean package
clean spring-boot:run
skip testcases
Spring app
java class to hold num1 & num2
JSON body
{
"num1" : 5,
"num2" : 10
}
controller
entry & exit
routing
service
business
DAO / respository
all db related logic
========
Spring Boot:
managing dependencies (Starters)
Auto config
embedded tomcat
application.property default config
annotation based (No need to xml config)
Spring....
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.
@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
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..
@RestController
RestAPI
@Service
core business logic
@Responsitory
DB logic
@Controller
MVC - UI/View
@Configuration
@ControllerAdvice
Globals ErrorHandling
@Component
Util
Helper
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.
Dependency Lookup
You need to ask & take
========
If you want to currently how many spring beans are there inside Container...
========
What if, predefined classes are there.. you want to make them as Spring Bean???
Random random = new Random();
int randInt = random.nextInt(100);
@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
Layoffs..