Spring Boot Tutorial
Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)
Prerequisite (Spring Core Concepts)
Spring Boot Core
Spring Boot with REST API
Spring Boot with Database and Data JPA
Spring Boot with Kafka
Spring Boot with AOP
BeanFactory
is the root interface for accessing the Spring container. It provides the basic functionalities to manage and access beans, but it's often overshadowed by its more feature-rich sub-interface, ApplicationContext
.
BeanFactory
:Bean Instantiation & Configuration: Allows you to configure and retrieve beans.
Singleton/Prototype Bean Management: Supports both singleton (a single shared instance) and prototype (a new instance every time) bean lifecycles.
Bean Aliasing: Beans can be queried and retrieved using different names.
Type Conversion: Supports type conversion for properties.
Aspect-Oriented Programming (AOP): Provides basic support for AOP through interceptors and advisors.
The most commonly used BeanFactory
implementation is XmlBeanFactory
, which loads beans based on the definitions in an XML file. However, with modern Spring applications, XmlBeanFactory
is now considered deprecated in favor of ApplicationContext
.
Usage of XmlBeanFactory (for older versions of Spring):
Resource resource = new ClassPathResource("beans.xml"); BeanFactory factory = new XmlBeanFactory(resource); MyBean bean = (MyBean) factory.getBean("myBean");
BeanFactory
vs ApplicationContext
:While BeanFactory
provides the basic features for DI (Dependency Injection), ApplicationContext
adds more enterprise-specific functionalities:
MessageSource: Handling of messages, supporting internationalization.
Event Propagation: Supports application event propagation.
Web Applications: The WebApplicationContext
interface integrates with web applications, adding web-related features.
Eager Loading: While BeanFactory
lazily initializes beans by default (i.e., beans are created when requested), ApplicationContext
preloads singleton beans at startup.
Integrated AOP features: More comprehensive support for aspect-oriented functionalities.
For most scenarios, especially in web-based applications, developers typically use ApplicationContext
due to its richer set of features. However, if you're building a lightweight application and only need basic DI features, BeanFactory
might suffice.
In contemporary Spring development, ApplicationContext
is more commonly used, and direct interactions with BeanFactory
are rare.
Spring BeanFactory example:
<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="car" class="com.example.Car"></bean> </beans>
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml")); Car car = (Car) beanFactory.getBean("car");
BeanFactory configuration in Spring:
<bean id="car" class="com.example.Car"></bean>
@Configuration public class AppConfig { @Bean public Car car() { return new Car(); } }
XML configuration for BeanFactory in Spring:
<bean id="car" class="com.example.Car"></bean>
Annotation-based configuration with Spring BeanFactory:
@Configuration public class AppConfig { @Bean public Car car() { return new Car(); } }
BeanFactoryPostProcessor in Spring:
BeanFactoryPostProcessor
interface.public class CustomBeanFactoryPostProcessor implements BeanFactoryPostProcessor { @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { // Customize bean definitions } }
Customizing BeanFactory in Spring:
AbstractBeanFactory
for more customization.public class CustomBeanFactory extends DefaultListableBeanFactory { // Customization logic }