IoC Container

IoC is also known as dependency injection (DI). It is a process whereby objects define their dependencies, that is, the other objects they work with, only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method. This process is fundamentally the inverse, hence the name Inversion of Control (IoC), of the bean itself controlling the instantiation or location of its dependencies by using direct construction of classes, or a mechanism such as the Service Locator pattern.
The org.springframework.beans and org.springframework.context packages are the basis for Spring Framework's IoC container. The BeanFactory interface provides an advanced configuration mechanism capable of managing any type of object. ApplicationContext is a sub-interface of BeanFactory. The interface org.springframework.context.ApplicationContext represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the aforementioned beans. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata. The configuration metadata is represented in XML, Java annotations, or Java code. In standalone applications it is common to create an instance ofClassPathXmlApplicationContext or FileSystemXmlApplicationContext.
In short, the BeanFactory provides the configuration framework and basic functionality, and the ApplicationContext adds more enterprise-specific functionality
In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container





As the preceding diagram shows, the Spring IoC container consumes a form of configuration metadata; this configuration metadata represents how you as an application developer tell the Spring container to instantiate, configure, and assemble the objects in your application.
XML-based configuration metadata:
<?xml version="1.0" encoding="UTF-8"?>
       

<beans
        xmlns="http://www.springframework.org/schema/beans"
           


       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          


       xsi:schemaLocation="http://www.springframework.org/schema/beans

           http://www.springframework.org/schema/beans/spring-beans.xsd">
        


 

       <bean
         id="..."
             class="...">
                 


       <!-- collaborators and configuration for this bean go here -->
        


       </bean>
        



The location path or paths supplied to an ApplicationContext constructor are actually resource strings that allow the container to load configuration metadata from a variety of external resources such as the local file system, from the Java CLASSPATH,
ApplicationContext context =


       new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});

Composing XML-based configuration metadata

use one or more occurrences of the <import/> element 
<beans>
       


 

       <import
         resource="services.xml"/>
             


       <import
         resource="resources/messageSource.xml"/>
             


       <import
         resource="/resources/themeSource.xml"/>
             


 

       <bean
         id="bean1"
             class="..."/>
                 


       <bean
         id="bean2"
             class="..."/>
                 


 
</beans>
       



It is possible, but not recommended, to reference files in parent directories using a relative "../" path. Doing so creates a dependency on a file that is outside the current application

Using the container

// create and configure beans
       

ApplicationContext context =


       new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});


 
// retrieve configured instance
       

PetStoreServiceImpl service = context.getBean("petStore", PetStoreServiceImpl.class);


 
// use configured instance
       

List userList = service.getUsernameList();



èIdeally your application code should have no calls to the getBean() method at all, and thus no dependency on Spring APIs at all. For example, Spring's integration with web frameworks provides for dependency injection for various web framework classes such as controllers and JSF-managed beans


Bean Overview:
IoC Container manages one or more beans based on configuration metadata. Configuration Metadata can be in XML format, Annotations or Java annotations

No comments:

Post a Comment