Spring - Spring is a oprn source application framework used for java development
Why Spring
Spring ecosystem - wide variety of modules
-Spring core
-Spring web
-Spring Data
-Spring Security
-Sprint AOP - Aspect Oriented Programming
Spring Core
What does spring offers
->Spring offers IoC (Inversion of control)
What is IoC(Inversion of Control)?
-> transfer of object creation to framework is called Inversion of Control (IoC
How is it acheived ->
In Spring it is acheived by Dependency Injection
What is dependency injection
->Injected one object into another dependent object is called dependency Injection
How is it acheived in Spring
-> we use @Autowired
How Spring works
Whenever we run the application spring creates the context object if we use
AnnotationConfigApplicationContext then its scans the base package for bean
creation annotations like @Component,@Controller etc
or it can scan xml config if used ClassPathXmlApplicationContext
BeanFactory (Lazy) vs ApplicationContext (Eager)
Lazy vs Eager
Lazy means bean is created when we call context.getBean("Car")
Eager means beans are created when application context is created
Annotation -> ApplicationContext context = new
AnnotationConfigApplicationContext("basePackage);
Bean access - > (ExpectedClassCasting)context.getBean(beanId); ||
(ExpectedClassCasting) beanFactory.getBean(beanId);
Bean Creation -> @Component, @Controller,@RestController,@Service,@Repository
Spring bean -> Class Name -> BeanId - small camelcase className |
incase same className and different package
@Component("optionalBeanName") eg @Component("TE2") class TataEngine // @Component
ConflictingBeanDefinitionException: Annotation-specified bean name 'tataEngine'
to Solve pass name in @Component("id1") on class or pass id
@Autowired- is an annotation used to achieve dependency Injection
By Default required= true -> mandatory Dependency
@Autowired(required=false) -> optional dependency
@Autowired
Searches spring beans
No bean found - NoSuchBeanDefinition
1. byType -> extended/implemented marked beans
Incase more than 1, exception - > NoUniqueBeanDefinitionException - expected
1 but found 2
if found more than 1 then byName
2. byName ->
Incase multiple implementation of interfaces, then use @Primary
Explicity byName ->@Qualifier
defined like below
@Autowired
@Qualifier(BeanId - small camelcase className)
Empty qualifier @Qualifier ->tries to search bean with id =""
Exceptions and resolution
ConflictingBeanDefinitionException
-> spring context-> same className different package
-> Pass beanId in @Component("BeanId")
NoUniqueBeanDefinitionException
-> autowired -> more than 1 bean of same class type (child/implementation),
Resolution -> change field name so that @Autowired will try to match byName
after byType (default behaviour)
-> use @Qualifier to explicity use a bean
-> use @Primary on class whose object should be default
NoSuchBeanDefinitionException -> trying to access object which is not created in
spring context
Resolution -> check the class whose bean is expected if it is annotated with
@Component
-> if @Component is present check if the bean is in @ComponentScan
package /base package
-> If external bean then check if we have @Bean method defined in
@Configuration class
field vs setter vs constructor
optional optional mandatory
If we want optional dependency we will use field or setter autowire
if we want mandatory dependency we will use autowire on single parameterized
constructor
Context->basePackage->Spring bean are initalized->DependencyInjection
Annotations
BeanCreation - @Component,@Controller,@RestController,@Service,@Respository
Dependency Injection
@Autowired : To inject dependent spring bean object
-> byClassType(also child -child should be annotated also)
->more than 1 bean -> byName
Explicity byName ->@Qualifier
defined like below
@Autowired
@Qualifier(BeanId - small camelcase className)
ByDefault implementation -> @Primary -> 1 parent/interface --> multiple
child/implementation class
@Value ->to put the value
Client->Server->Web.xml --> Disp