Spring
It is a framework built in 2004 by the led by Rod Johanson
Why spring?
We use spring to overcome drawback of EJB(Enterprise Java Beans) application
EJB application are heavy weighted application which works on OS
Features of spring
We can build lightweight application using spring
Helps in building loosely coupled application
Provide spring container and dependency injection
Code reusability
And simplified code
Less configurations
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
Steps to use core container
1. Create a POJO class
2. Create a Configuration XML file and Configure POJO class inside it using bean tag
3. Create a driver class
Example to use core container
POJO class:
public class Person{
run(){
sop(“running);
}
}
Config XML -> create this file in src/main/java folder
<bean id=”myperson” class=”[Link]”></Bean>
main(){
Resources resource = new ClassPathResource(“[Link]”);
BeanFactory bf = new XmlBeanFactory(resource);
Person person = (Person) [Link](“myperson”);
[Link]();
}
Using J2EE Container
To use J2ee container we need to create an object of ApplicationContext
Where ApplicationContext is an interface and having implementing class known as
ClassPathXmlApplicationContext
We need to create object of ClassPathXmlApplicationContext by passing [Link] filename using new
operator
We can store the reference of this object either in ApplicationContext type or in
ConfigurableApplicationContext type
Configuring multiple bean classes in one Xml file
Goto code
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
Multiple xml configuration files
Goto code
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.
Inject and object to the reference variable in POJO class
Post construct and pre destroy
Post construct: it is a method which will be getting executed just after POJO class object is created
pre destroy: it is a method which will be getting executed just before Bean object is Destroyed
XML and Annotation based configuration
In xml and annotation-based configuration we will not configure bean class inside xml file instead we use @Component
Annotation and ComponentScan tag in XML file by providing base package
@Component: 1. it is a class level annotation that is it should be used above the class.
2. Spring container or IOC container will create an object of bean class only if it is annotated with
@Component
3. IOC container will scan all components(i.e, java bean) present inside given package in xml file and
then it will create an object of that bean class
Default BeanId for classes with various names
Example , Person person
PERSON -> PERSON
PersonApp -> personApp
We can provide our own bean id using value attribute with @Component annotation
Example, @Component(value = "person")
public class Person {
public void run() {
[Link]("running");
}
}
@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
@Autowired with interface
Whenever we are using @Autowired above the reference type interface since it is not possible to create an
object of interface it will look for the implementing class if there is implementing classes with
@Component it will create an object of that class and injected.
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
It is used to avoid NoUniqueBeanDefinationException
@Qualifier annotation is used this @Autowired Annotation
It is a field level annotation
To @Qualifier annotation, we need to pass a bean id of which we want object to be injected
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
Change the scope of a bean
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)