Spring
Spring
Spring Container
Spring container is the one who is going to take care of POJO (PLAIN OLD JAVA OBJECTS) classes
lifecycle
Sprig container manages,
1. Instantiating POJO class
2. Delivering the POJO object to the caller
3. Destroying POJO class/object
Various Spring Containers
1. Core container
2. J2EE container
3. Web container
Core and J2EE container comes under IOC (Inversion Of Control) container.
To use core container, we need to create object of BeanFactory Interface
To use J2EE container we need to create object of ApplicationContext Interface
To use web container, we need to create object of WebApplicationContext Interface
IOC containers And Dependency Injection
XML Config
POJO class
Driver Class
Note: - Whenever we want to configure multiple POJO or Bean Classes in one configuration file we need
to provide unique bean id else it will get exception
Dependency Injection
The process of injecting values to the variables of particular POJO class using following ways
1. Setter injection
2. Constructor injection
3. Variable/field injection
Setter injection -}
1. The process of injecting values to the variable of POJO class using setter methods is k/as setter
injections
2. To inject the values using setter methods we use property tag inside bean tag of configuration file
by passing variable name and value
Inject object using setter method:gotocode
Inject list using setter method:gotocode
How to inject a map
Class book
Map<string,double>
Constructor Injection:
Inject values to the variables of POJO class using constructor.
@Value
It is used to inject the values to the variables of POJO class
It is a field level annotation
It can be used above the variable, above the setter methods, inside the constructor
@Autowired
1. It is used to inject an object of POJO or Bean class through the reference variable
2. It is a field level annotation
3. it can be used above the variable above the setter method or above the constructor
NoUniueBeanDefinationException:
We get this exception whenever we are using @Autowired annotation above the reference of type interface
which has two or more implementing classes
How to avoid NoUniueBeanDefinationException :
1) using @Primary
2) using @Qualifier
1. @Primary : it is a class level annotation it should be used above the class if interface has two
implementing classes and we want an object of any one of the implementing classes to be injected then
we use @Primary. Among two implementing classes @Primary annotation should be used above any
one of the classes not on both classes
@Qualifier
Read data from properties file and inject it to variables of POJO class
@Bean:
1. Whenever we want IOC container to create an object of inbuilt classes and interfaces and inject them while
autowiring then we make use of @Bean Annotation
2. @Bean it is used above the method which returns an object, and it should be present inside configuration class
Note: if we are not interested to make any class as component (@Component) and still if we want IOC container to
inject the object of that class, we make use of @Bean Annotation
Bean Scope:
1. Whenever we start spring application IOC container will scan the Components from the particular package and
creates an object for all the bean classes
2. By default, IOC container will create only one object for each bean classes that is the bean scope will be singleton
by default
3. Every time we call getBean() by passing bean id the same object will be return from the application context
To change the scope of bean we make use of @Scope annotation by providing scope
Scope as prototype:
1. If we want IOC container to create an object of bean class every time I make request we make use of bean
scope prototype
AVAILABLE SCOPES
1. Prototype
2. Request
3. Session
4. application
5. singleton(default)