Spring Framework Tutorial: IoC Container, BeanFactory vs ApplicationContext
Spring Framework
- Spring is one of the most popular frameworks for Java Enterprise Edition (EE).
- Spring Framework is used for developing reliable and high-quality applications.
- Spring Framework is a lightweight framework.
- Spring Framework was designed by Rod Johnson.
- Spring Framework is based on several modules: Spring Core, Spring Context, Spring Web-MVC, Spring AOP, Spring JDBC, Spring Security, and more.
- Using Spring Framework you can achieve loose coupling, easy testing, and better maintainability.
- Spring is based on configuration.
- You can achieve configuration using XML or annotations.
What is an IoC Container?
The Spring IoC container is the core part of the Spring Framework. It is responsible for the following:
- Creating instances of beans.
- Managing the lifecycle of the bean.
- Configuring the object.
- Assembling dependencies between objects.
- Converting values to Java types using Spring Expression Language (SpEL).
Spring provides two types of containers:
- BeanFactory
- ApplicationContext
BeanFactory
- The BeanFactory API provides the underlying basis for Spring's IoC functionality.
- It is mostly used in integration with other parts of Spring and related third-party frameworks.
- It is the root interface for accessing a Spring bean container.
ApplicationContext
- It is a sub-interface of ListableBeanFactory, and ListableBeanFactory is a sub-interface of BeanFactory.
- So ApplicationContext includes all the functionality of BeanFactory.
BeanFactory vs ApplicationContext
| BeanFactory | ApplicationContext |
|---|---|
| BeanFactory supports only singleton and prototype scope. | ApplicationContext supports all the bean scopes. |
| BeanFactory supports lazy initialization. | ApplicationContext supports eager initialization. |
| BeanFactory does not support internationalization (I18N). | ApplicationContext supports internationalization (I18N) for text messages. |
| BeanFactory does not support event publication. | ApplicationContext supports event publication to registered listeners. |
| BeanFactory annotation-based dependency injection is not supported. | ApplicationContext supports annotation-based dependency injection. |
| BeanFactory implemented class: XmlBeanFactory. | ApplicationContext implemented classes: ClassPathXmlApplicationContext, FileSystemXmlApplicationContext, and WebApplicationContext. |
| BeanFactory does not support loading multiple configuration files. | ApplicationContext supports loading multiple configuration files. |
| BeanFactory is used where resources are limited, like mobile. | ApplicationContext is used where resources are not limited. |
How to create a BeanFactory
First way: using FileInputStream
InputStream in = new FileInputStream("spring_config.xml");
BeanFactory demo = new XmlBeanFactory(in);
// Get bean from configuration XML file
HelloWorld obj = (HelloWorld) demo.getBean("hello");
Second way: using FileSystemResource
// lookup from system location
Resource res1 = new FileSystemResource("spring_config.xml");
BeanFactory demo1 = new XmlBeanFactory(res1);
Third way: using ClassPathResource
// lookup from classpath location
ClassPathResource res2 = new ClassPathResource("spring_config.xml");
BeanFactory demo2 = new XmlBeanFactory(res2);
How to create an ApplicationContext
First way: from classpath
// lookup from classpath location
ApplicationContext ac = new ClassPathXmlApplicationContext("spring_config.xml");
HelloWorld obj = (HelloWorld) ac.getBean("hello");
Second way: from system location
// lookup from system location
ApplicationContext ac = new FileSystemXmlApplicationContext("spring_config.xml");
HelloWorld obj = (HelloWorld) ac.getBean("hello");
Third way: from a web location
// lookup from web location WebXmlApplicationContext is used within a web application.
What is Dependency Injection?
- Dependency Injection is a part of the IoC container.
- Dependency Injection means how an object is injected by a container.
- Dependency Injection is a design pattern that removes the dependency from the programming code.
- Dependency Injection provides loose coupling.
You can achieve dependency injection in two ways:
- By setter method
- By constructor
Bean Scopes
When you create a bean definition, that means you can create an actual instance of the class defined by the bean definition. You can create one or many instances from a single scope. By default, the bean scope is singleton.
The Spring Framework supports six scopes. Four of them are available only if you use a web-based ApplicationContext.
| Scope | Description |
|---|---|
| singleton | Single instance for each Spring IoC container. |
| prototype | Multiple instances for each Spring IoC container. |
| session | Single scope bean definition for the lifecycle of an HTTP session (HttpSession). |
| request | Single scope bean definition for the lifecycle of an HTTP request (HttpServletRequest). |
| application | Single scope bean definition for the lifecycle of the application scope (ServletContext). |
| websocket | Single scope bean definition for the lifecycle of a WebSocket. |
Bean Definition Inheritance
- It is similar to Java inheritance, but in Spring it has nothing to do with the Java
extendskeyword. - A child bean can access the properties of the parent bean and can also override them.
- A child bean can add new fields in its own class.
- In Spring, bean definition inheritance uses the
parentattribute in the child bean definition.
Where to go next
You now understand the Spring Framework's core: the IoC container, the difference between BeanFactory and ApplicationContext, dependency injection, bean scopes, and bean definition inheritance.
If you remember one thing, remember this: Spring does not create objects for you because it is magic. It creates them because you told it how in a configuration file. The IoC container reads that configuration, builds the objects, wires the dependencies, and hands them back when you ask.
If you want to go deeper, try these next steps:
- Write a small XML configuration with two beans and a setter-based dependency injection. Run it and confirm both beans are created.
- Change the scope of one bean from singleton to prototype. Create two objects and confirm they are different instances.
- Define a parent bean with common properties and a child bean that overrides one of them. Confirm the override works.
- Move on to annotation-based configuration with
@Component,@Autowired, andAnnotationConfigApplicationContext. The concepts are the same; only the configuration style changes.
No comments:
Post a Comment