Spring IoC tutorial – part2

The spring framework features are based on the concept of “container”. A container is responsible for including, instantiating or sourcing application objects, configuring such objects, and assembling the dependencies between these objects
The root interface for accessing a Spring bean container is org.springframework.beans.factory.BeanFactory. The BeanFactory interface is the central IoC container interface in Spring. This interface is implemented by objects that hold a number of bean definitions, each uniquely identified by a String name.
The org.springframework.context.ApplicationContext is a subinterface of BeanFactory and provides configuration for an application. This is read-only while the application is running, but may be reloaded.
An ApplicationContext implementation provides the following:
– access to beans using bean factory methods
– ability to load file resources
– ability to publish events to registered listeners
– ability to resolve messages and support internationalization (most used in international web applications)
An application context is created by the Spring container and initialized with a configuration provided by a resource that can be an XML file (or more) or a configuration class (or more) or both. When the resource is provided as a String instance, the Spring container tries to load the resource based on the prefix of that String value.
In order to provide the functionality for loading resources, an application context must implement the org.springframework.core.io.ResourceLoader interface.
Depending on the context class used, the resource loaded can have one of the following types :
1. If ctx is a ClassPathXmlApplicationContext instance resource type will be ClassPathResource
With “classpath” prefix:

public static void main(String[] args) throws Exception{
    // when configuration file is in src/main/resources/spring)
    ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:spring/application-context-ioc-setter.xml"); 
    Reservation myReservation=ctx.getBean("reservation", Reservation.class);

With no prefix:

public static void main(String[] args) throws Exception {
    //when configuration file is In root directory where the class creating the context is executed
    ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("application-context-ioc-constructor.xml"); 
    Reservation myReservation=ctx.getBean("reservation", Reservation.class);

2. If ctx is a FileSystemXmlApplicationContext instance resource type will be FileSystemResource:
With “file” prefix:

public static void main(String[] args) {
    ConfigurableApplicationContext ctx = new FileSystemXmlApplicationContext("file:/root/Documents/workspace-sts-3.9.1.RELEASE/jtrainer.education Spring IoC/src/main/resources/spring/application-context-ioc-constructor.xml");
    Reservation myReservation=ctx.getBean("reservation", Reservation.class);

3. If ctx is a WebApplicationContext instance resource type will be ServletContextResource:

public void init(ServletConfig config) throws ServletException {
    ServletContext servletContext = config.getServletContext();
    WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);

When you choose between ClassPathXmlApplicationContext, FileSystemXmlApplicationContext and WebApplicationContext?
– ClassPathXmlApplicationContext is used to read files fromclasspath. They must be in classesfolder of your web application or in a jar of your libs.
– FileSystemXmlApplicationContext can access all your file in your filesystem
– WebApplicationContext can access to files contained in your web application. In addition It implements WebApplicationContext and this means that it will detect ServletContextAware beans, register custom scopes (request, session, …)


Leave a Reply

Your email address will not be published. Required fields are marked *