IoC Basics

Differences Between BeanFactory and ApplicationContext

“BeanFactory is the top-level interface of the Spring framework’s IoC container. It only defines some basic functions and specifications, while ApplicationContext is a sub-interface of it, so ApplicationContext has all the functionality provided by BeanFactory.”

Typically, we call BeanFactory the Spring IoC container. ApplicationContext is an advanced interface of the container, having more features than BeanFactory, such as internationalization support and resource access.

BeanFactory

  • Is the core interface of the Spring framework, providing the most basic IoC container functionality
  • Mainly used for lazy initialization of beans
  • Suitable for resource-constrained environments (e.g., mobile devices, embedded systems)
  • Implementation class: DefaultListableBeanFactory

ApplicationContext

  • Is a sub-interface of BeanFactory, providing more features
  • Suitable for large enterprise applications, providing advanced features such as event publishing and internationalization support
  • Implementation classes: ClassPathXmlApplicationContext, FileSystemXmlApplicationContext, etc.

Use Cases

BeanFactory scenarios:

  • Used in resource-constrained environments or testing scenarios
  • If the application does not need ApplicationContext’s advanced features, BeanFactory is a lighter-weight choice

ApplicationContext scenarios:

  • Used in enterprise applications, supporting internationalization, event listeners, AOP, and other advanced features
  • Recommended for most Spring applications

Start the IoC Container

Java Environment

  • ClassPathXmlApplicationContext: Loads configuration files from the classpath (recommended)
  • FileSystemXmlApplicationContext: Loads configuration files from disk path
  • AnnotationConfigApplicationContext: Starts Spring container in pure annotation mode

Web Environment

XML Method

Modify web.xml under WEB-INF in webapp:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

Configuration Class Method

Start the container from the configuration class:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </context-param>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>wzk.SpringConfig</param-value>
    </context-param>
    <!-- Use listener to start Spring's IoC container -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

Required Maven Dependencies

<!-- Spring -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.1.12.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>5.1.12.RELEASE</version>
</dependency>
<dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>javax.annotation-api</artifactId>
    <version>1.3.2</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>5.1.12.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.13</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.1.12.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>5.1.12.RELEASE</version>
</dependency>