Spring Framework Tutorial
Software Setup and Configuration (STS/Eclipse/IntelliJ)
Core Spring
Spring Annotations
Spring Data
Spring JDBC
Spring Security
The BeanFactory
is a central interface in the Spring framework for accessing application components. It is the root interface for accessing the Spring container. The BeanFactory
provides advanced configuration facilities to manage any type of object, and the objects it manages are called beans.
The ApplicationContext
is a sub-interface of BeanFactory
and offers a more complete and sophisticated integration for building enterprise applications. Typically, for most use cases, developers interact more with ApplicationContext
, but it's essential to understand BeanFactory
as the foundation.
Configuration Mechanism: BeanFactory
uses configuration metadata provided to it (typically in the form of XML configuration) to instantiate and wire up beans as required.
Lazy Initialization: By default, the BeanFactory
initializes beans lazily. This means that a bean isn't instantiated until an application explicitly asks for it. This behavior is in contrast with ApplicationContext
, which typically pre-instantiates singleton beans on startup.
Dependency Injection: BeanFactory
can inject dependencies into your beans, either through constructor injection or setter injection. This inversion of control ensures that components have their dependencies met without being explicitly responsible for the creation or lookup of their dependencies.
Bean Lifecycle: The BeanFactory
manages the entire lifecycle of beans from creation to destruction. It also allows for custom initialization and destroy methods.
Resource Loading: While BeanFactory
itself doesn't provide a way to load resources (like properties files), there's a separate ResourceLoader
interface (which ApplicationContext
implements) for this purpose.
Here's a simple example of using BeanFactory
with XML configuration:
beans.xml:
<?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="myBean" class="com.example.MyClass" /> </beans>
Java Code:
import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; public class BeanFactoryExample { public static void main(String[] args) { // Load bean definitions from XML BeanFactory factory = new XmlBeanFactory(new ClassPathResource("beans.xml")); // Retrieve the bean from the container MyClass myBean = (MyClass) factory.getBean("myBean"); myBean.doSomething(); } }
XmlBeanFactory
is one of the implementations of the BeanFactory
interface, which reads bean definitions from an XML file.
In most applications, especially modern ones, it's more common to use ApplicationContext
(like ClassPathXmlApplicationContext
or AnnotationConfigApplicationContext
) instead of BeanFactory
. ApplicationContext
brings more features like internationalization, event propagation, and specialized contexts (like WebApplicationContext
for web apps).
In summary, while BeanFactory
provides the core functionality required for managing beans and dependency injection, the richer ApplicationContext
interface is more widely used in modern Spring applications due to its extended features.
Configuring beans with BeanFactory:
BeanFactory
to configure beans by defining them in XML or Java configuration. ApplicationContext
provides a more feature-rich alternative.XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("beans.xml")); MyBean myBean = (MyBean) factory.getBean("myBean");
Using BeanFactory for dependency injection in Spring:
BeanFactory
supports dependency injection by automatically wiring dependencies. Define dependencies in bean configurations.XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("beans.xml")); MyService myService = (MyService) factory.getBean("myService");
Lazy loading beans with BeanFactory in Spring:
BeanFactory
supports lazy loading, allowing beans to be created only when requested.XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("beans.xml")); MyLazyBean lazyBean = (MyLazyBean) factory.getBean("lazyBean");
Bean instantiation and retrieval with BeanFactory:
BeanFactory
. Beans are created when getBean
is called.XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("beans.xml")); MyBean myBean = (MyBean) factory.getBean(MyBean.class);
Customizing BeanFactory in Spring applications:
BeanFactory
for specific requirements, such as customizing bean instantiation or lifecycle.public class CustomBeanFactory extends DefaultListableBeanFactory { // Customizations... } CustomBeanFactory factory = new CustomBeanFactory(); MyBean myBean = (MyBean) factory.getBean("myBean");
Programmatic bean definition in Spring using BeanFactory:
BeanFactory
. This is useful when XML or annotation-based configurations are not suitable.XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("beans.xml")); BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MyBean.class); factory.registerBeanDefinition("myBean", builder.getBeanDefinition()); MyBean myBean = (MyBean) factory.getBean("myBean");