A Guide to Spring Container Basics

Spring / spring boot frameworks we get two IOC containers They are

a)Bean Factory

b)Application Context

Spring/Spring Framework are given for two basic operations:

1) Spring Bean Life Cycle Management

activities are: loading spring bean ,creating the spring bean , managing the spring bean, calling the life cycle methods of spring bean ,using its own Garbage Collector to destroy the objects of spring bean.

2) Dependency Management

The Process of arranging the dependent spring bean to target spring bean is called Dependency Management.

These can be done in two ways

1) Dependency Lookup

2)Dependency Injection

The spring bean who uses the services of other spring bean is known as Target Spring Bean.

The spring class whose services are being used is known as dependent spring bean.

Dependency Lookup :

The target spring bean writes some logics and spends some time to search and get that dependent spring bean from various resources.

To provide more global visibility and accessibility to objects of java class we need to place that objects/ objects references in special registry called JNDI Registry.

The way Target spring bean is having JNDI lookup code to search for the dependent spring bean from the JNDI Registry is known as Dependency Lookup.

This can be implemented using

call getBean()

Dependency Injection:

The Underlying server or container or framework or JRE or some other run time environment dynamically injects the dependent spring bean to target spring bean.

This can be implemented using

a )setter Injection

b) constructor injection

c) arbitrary method

d) field injection

To create Bean factory IOC Container take the predefined class that implements Bean Factory directly or indirectly

XMLBeanFactory

Creating the IOC Container

FileSystemResource res= new FileSystemResource(applicationContext.xml);

XmlBeanFactory factory = new XmlBeanFactory(res);

DefaultListableBeanFactory

The Bean Factory IOC Container supports only XML driven configuration

Application Context IOC Container

Code to create Application Context IOC Container having xml driven configuration

1) FileSystemXmlApplicationContext

2) ClassPathXmlApplicationContext

3) XmlWebApplicationContext

Code to create Application Context IOC Container by taking annotation driven configuration

creates IOC Container by taking AppConfig class as @configuration class

AnnotationConfigApplicationContext

AnnotationConfigWebApplicationContext

Setter Injection

If the IOC Container of the spring App is using setter method of Target Spring Bean to assign dependent spring bean class obj to has a property of the HAS-A property of target spring bean is called setter Injection

In xml driven configuration we use <property> tag under <bean> tag

In Annotation Driven Configuration we use @Autowired on the target spring bean