Tuesday, December 10, 2013

Spring BeanFactory

1. Bean


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. These beans are created with the configuration metadata that you supply to the container, for example, in the form of XML <bean/> definitions.

All the above configuration metadata translates into a set of the following properties that make up each bean definition.
Properties
Description
class
This attribute is mandatory and specify the bean class to be used to create the bean.
name
This attribute specifies the bean identifier uniquely. In XML-based configuration metadata, you use the id and/or name attributes to specify the bean identifier(s).
scope
This attribute specifies the scope of the objects created from a particular bean definition and it will be discussed in bean scopes chapter.
constructor-arg
This is used to inject the dependencies and will be discussed in next chapters.
properties
This is used to inject the dependencies and will be discussed in next chapters.
autowiring mode
This is used to inject the dependencies and will be discussed in next chapters.
lazy-initialization mode
A lazy-initialized bean tells the IoC container to create a bean instance when it is first requested, rather than at startup.
initialization method
A callback to be called just after all necessary properties on the bean have been set by the container. It will be discussed in bean life cycle chapter.
destruction method
A callback to be used 


 2. Spring Container:

There is no single Spring Container, actually comes with two containers:
  •    Bean Factory (org.springframework.beans.factory.BeanFactory interface).
  •  Application Context (org.springframework.beans.factory.ApplicationContext interface).

i. Bean Factory: 

are the simplest of containers providing basic support for dependency injection.It is an implementation of the Factory Design Pattern.
Bean factory is like a factory that contains a collections of bean. Bean Factory holds Bean definition of multiple beans within itself and then instantiates the bean whenever asked for by clients.
  • Bean Factory is able to create associations between collaborating bean object as they are instantiated. This removes the burden of configuration from bean itself and the beans client.
  • bean factory also takes part in the life cycle of a bean making calls to init and destroy methods.
There are several implementations of BeanFactory in Spring but the most useful one is org.springframework.beans.factory.xml.XmlBeanFactory.

To create XmlBeanFactory, pass a java InputStream to the constructor.The Inputstream will provide the XML to the factory.

BeanFactory bf  = new XmlBeanFactory(new FileinputStream("spring.xml"));

Above line of code tells the bean factory to read the bean definition from the XML file.But the bean factory does not instantiate the beans just yet. That means no object of bean is yet created bcoz they are lazily instantiated meaning the bean themselves will not be instantiated until they are needed.

For making BeanFactory to instantiate a bean you need to call the getBean() method.

MyBean myBean = (MyBean) bf.getBean(<bean id>) ;

MyBean myBean = (MyBean) bf.getBean("car") ;



you can also use:

BeanFactory bf  = new ClassPathXmlApplicationContext("spring.xml");




ii. Application Context: 

ApplicatinContext interface extends beanFactory interface.It is same as bean factory. Both load bean definitions, provides collaboration between two and mode beans and takes part in the Life cycle.
Apart from this Application context also provides: 
  • resolving text messages
  • Supports for internationalization
  • Load file resources
  • Publish events to the beans that are registered as listener. 
Because of additional functionality Application Context is prferred over a BeanFactory in nearly all applications.The only time you might considering a Beanfactory are in circumstances where resources are scarce as mobile device.

Lazy-Instantiation 

Another Big Difference between Application Context and bean Factory is how singleton beans are loaded. A bean factory lazily loads all the beans, creating bean object until the getBean() method is called.
An application context is a bit smarter and preloads all singleton beans upon context startup.This helps us to ensure all the configurations and dependencies are intact.


Common implementation of Application Context:
  • ClassPathXMLApplicationContext : loads content definition from an XML file (or configuration file) located in the class path.
          ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");

  • FileSystemXMLApllicationContext :  loads content definition from an XML file (or configuration file) located in the file system.
         ApplicationContext ctx1 = new  FileSystemXmlApplicationContext("C:/tushar_saran/spring.xml");

  • XMLWebApplicationcontext :  loads content definition from an XML file (or configuration file) located in the web application.


Next Page > Bean Life Cycle





No comments:

Post a Comment