different types of containers in spring

Download Different Types of Containers in Spring

If you can't read please download the document

Upload: sunil-kumar-mohanty

Post on 16-Apr-2017

3.735 views

Category:

Technology


0 download

TRANSCRIPT

Different types of Containers in Spring

In my previous document I have given a brief view on spring container. In this session I am going to emphasize the different types of spring container with simple examples. Spring containers are of two types : 1. Bean Factory Based 2. Application context based

BeanFactory based spring Container

BeanFactory is an interface and XmlBeanFactory is it's implementation.

BeanFactory

XmlBeanFactory


The spring container will be created when we are just instantiating the XmlBeanFactory class . Constructor of XmlBeanFactory class consumes Resource object as an argument. Here resource object represents the spring configuration file.Resource interface have two implementations to specify the spring configuration file. These are : 1.FileSystemResource 2.ClassPathResource

XmlBeanFactory with FileSystemResources

FileSystemResources used to take the complete system path(absolute path) of the xml file. The following example shows how to instantiate the BeanFactory using FileSystemResources.

UsingFileSystemResources.java package com.spring.demo; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.FileSystemResource;

/** * Demonstrating the uses of FileSystemResources * * @author sunilm * */public class UsingFileSystemResources { // Instantiating the BeanFactory using FileSystemResourcesprivate static BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource("/Users/sunilm/spring/example/FileSystemResources/applicationcontext.xml"));public static void main(String[] args) {DemoService service1 = (DemoService)beanFactory .getBean("demoService1");System.out.println(service1.getMessage());DemoService service2 = (DemoService)beanFactory .getBean("demoService2");System.out.println(service2.getMessage());}

}

DemoService.java

package com.spring.demo;

/** * Service class. This is simply a POJO class * * @author sunilm * */public class DemoService {

String message ;/** * No argument constructor */ public DemoService( ) { message = "This is the default message" ; }

// The following code snippet is the remaining part of previous slide

/** * Parameterized constructor * @param msg message */ public DemoService(String msg) { message = msg ; } /** * Get the input message * * @return the message */ public String getMessage( ) { return message ; } }

applicationContext.xml

demonstration of xml configuration file


Note : Use of file system resources to instantiate the container is quite simpler to use. But we can notice that it used to take the complete system path. In this scenario if in future we are going to change the location of the project, again we need to change the location of the spring configuration file in the program. Thats why it is not advisable to use the FileSystemResources always.

XmlBeanFactory with ClassPathResources

In this case the container reads the configuration file from the class path. The following example describes the use of ClassPathResources.

UsingClassPathResource.java package com.spring.demo; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource;

/** * Demonstrating uses of ClassPathResources * @author sunilm * */

public class UsingClassPathResource {

// Instantiating the BeanFactory using ClassPathResourcesprivate static BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));public static void main(String[] args) {DemoService service1 = (DemoService)beanFactory.getBean("demoService1");System.out.println(service1.getMessage());DemoService service2 = (DemoService)beanFactory.getBean("demoService2");System.out.println(service2.getMessage()); } }


NOTE: In the above example we can see that we are not providing the complete path of the configuration file. In this case even we are changing the location of the project in the system, still our application will work without any modification,since it is reading the configuration file from the classpath. So it is advantage to use ClassPathResources.

Facts on BeanFactory Based Container

It is a light weight container.It loads the spring bean and manages the life cycle of the spring bean only when the getbean('beanref') method is getting called. Especially it is used to develop standalone application and mobile application.

ApplicationContextBased Container

ApplicationContext is child interface of BeanFactory.The major implementation of this interface are as follows .

FileSystemXmlApplicationContext ClassPathXmlApplicationContext XmlWebApplicationContext

Hierarchy of ApplicationContext based container

Facts on ApplicationContext

It loads spring beans and manages the life cycle of the bean as soon as the container starts. It does not wait for the 'getBean' method to be called. It provides all the features of the BeanFactory along with the following additional features. Event handling mechanism Internationalization EJB integration Scheduling JNDI look up

FileSystemXmlApplicationContext

For this container we need to pass the absolute path of the spring configuration file to it's constructor. As soon the container starts it loads the beans configured in the spring configuration file. The following example precise s the use of FileSystemXmlApplication context.

UsingFileSystemApplicationContext.java

package com.spring.demo.FileSystemApplicationContext; import org.springframework.context.ApplicationContext; import org.springframework.context.support .FileSystemXmlApplicationContext; import com.spring.demo.DemoService;

/** * Demonstrating the uses of FileSystemApplicationContext * * @author sunilm */public class UsingFileSystemApplicationContext{ // Instantiating the BeanFactory using FileSystemResources private static ApplicationContext context = new FileSystemXmlApplicationContext("/Users/sunilm/spring/ example/FileSystemResources/applicationContext.xml"); public static void main(String[] args) {DemoService service1 = (DemoService)context .getBean("demoService1");System.out.println(service1.getMessage());DemoService service2 = (DemoService)context .getBean("demoService2");System.out.println(service2.getMessage());}

}


In the above case we could found that the absolute path of the spring configuration file is required for the constructor of the FileSystemXmlApplicationContext. If the project location is getting changed in future we need to modify the path in our application. So the use of FileSystemXmlApplicationContext is discouraged.

ClassPathXmlApplicationContext

Here the container reads the spring configuration file from the class path and no need to pass the absolute path of the spring configuration file to the constructor. In this case if we are changing the project location in future, no need to modify the the location of the configuration file in the source code. Use of ClassPathXmlApplicationContext is encouraged in application development.

UsingClassPathXmlApplicationContext.java

package com.spring.demo.FileSystemApplicationContext;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.spring.demo.DemoService;

/** * Demonstrating the uses of ClassPathXmlApplicationContext * * @author sunilm * */public class UsingClassPathXmlApplicationContext{ // Instantiating the BeanFactory using FileSystemResourcesprivate static ApplicationContext context = new ClassPathXmlApplicationContext("/Users/sunilm/spring/example/FileSystemResources/applicationContext.xml"); public static void main(String[] args) {DemoService service1 = (DemoService)context.getBean("demoService1");System.out.println(service1.getMessage());DemoService service2 = (DemoService)context.getBean("demoService2");System.out.println(service2.getMessage());}

}

XmlWebApplicationContext

This is used by Spring web MVC module. The Container object is created by the web container internally. The details on this we will discuss while covering Spring MVC and web.

Hope this presentation provides good overview on spring Containers . In my next session I am going to describe the different types of dependency injections in spring. Thanks