2.Spring Ioc : IoC & Dependency Injection

1. Dependency Injection
The process of injecting the dependent data by reading xml file.
If a class contains String address as dependent data, here we no need to pass the data programmatically.we just pass the data to xml file, at run time spring container will inject the dependent data to your bean classes
Xml à DI à Bean
2. IOC (Inversion of Control)
·         The spring container is at the core of the Spring Framework.
·         The container will read the spring.xml file and create the objects, inject the dependencies, and manage their lifecycles.
·         The spring container uses dependency injection (DI) to manage the components that make up an application.
·         Here the data is called as metadata. We will inject the data from xml or annotations
Spring IoC Container
3. Spring Core containers.
We have two containers in spring to do DI, they are
1. BeanFactory Container
·         This is the Basic container providing basic support for DI
·         defined by the org.springframework.beans.factory.BeanFactory interface
Syntax:
Resource resource=new ClassPathResource("applicationContext.xml");  
BeanFactory factory=new XmlBeanFactory(resource);  

2. ApplicationContext Container
·         This is the enterprise-specific container
·          defined by the org.springframework.context.ApplicationContext interfaces
·         It adds some extra functionality than BeanFactory such as
§  simple integration with Spring's AOP,
§   message resource handling (for I18N),
§  event propagation,
§  application layer specific context (e.g. WebApplicationContext)
Syntax
ApplicationContext context = new ClassPathXmlApplicationContext ("spring.xml");  

4. BeanFactory Vs ApplicationContext
BeanFactory Container
ApplicationContext Container
·         Does not support the Annotation based dependency Injection.
·         Does not support internationalization (I18N) 
·         Not support JNDI,EJB
·         By default its support Lazy loading
·         Support Annotation based dependency Injection.
·         support internationalization (I18N) 
·         Support  many enterprise services such JNDI access, EJB integration, remoting
·         It’s By default support Aggressive loading.

Lazy Loading: A bean is loaded only when an instance of that Java class is requested by any other method or a class
BeanFactory factory = new XmlBeanFactory(  new InputStreamResource(new FileInputStream("beans.xml"))); // 1
Employee emp = (Employee) factory.getBean("employeeBean");
// 2

Even though "beans.xml" configuration file is loaded with BeanFactory container in line number 1, none of the beans will be instantiated. Instantiation takes place only at line number 2, where bean called "employeeBean" is requested from container. Since the class is instantiated at getBean() method call, time spend to return this method will vary depending on the instantiated object
Aggressive loading: All beans are instantiated as soon as the spring configuration is loaded by a container
ApplicationContext context =    new ClassPathXmlApplicationContext("beans.xml"); // 1
Employee emp = (Employee) context.getBean("employeeBean");
// 2

As all singleton beans are instantiated by container at line number 1, this line will take some considerable time to complete. However line number 2 will return the bean instance immediately since instances are already available inside the container

Post a Comment

Thank You

Previous Post Next Post