Skip to content


Spring AOP

AOP stands for Aspect-Oriented Programming and it helps decouple cross-cutting concerns from the object that they affect. It’s similar to DI in the sense that DI helps decouple an application’s object from each other.

According to the Spring,

Spring event

From Spring Application Context introduction, we know one of the capability that ApplicationContext has, but `BeanFactory1 doesn't have is event publication.

ApplicationContext provides the event handling through the ApplicationEvent class and the ApplicationListener interface.

If a bean that implements the ApplicationListener interface is deployed into the context, every time an ApplicationEvent gets published to the ApplicationContext, that bean is notified. Essentially, this is the standard Observer design pattern.

Spring Application Context

What is ApplicationContext?

According to the Spring document,

The org.springframework.beans and org.springframework.context packages are the basis for Spring Framework’s IoC container. The BeanFactory interface provides an advanced configuration mechanism capable of managing any type of object. ApplicationContext is a sub-interface of BeanFactory. It adds:

  • Easier integration with Spring’s AOP features

  • Message resource handling (for use in internationalization)

  • Event publication

  • Application-layer specific contexts such as the WebApplicationContext for use in web applications.

In short, the BeanFactory provides the configuration framework and basic functionality, and the ApplicationContext adds more enterprise-specific functionality. The ApplicationContext is a complete superset of the BeanFactory and is used exclusively in this chapter in descriptions of Spring’s IoC container.

Spring Bean Lifecycle

How Spring Manage Bean

ApplicationContext is an interface in Spring that provides configuration information to an application. It's the central interface in a Spring application. ApplicationContext is responsible for instantiating, configuring, and assembling beans. It reads configuration metadata to determine what objects to instantiate, configure, and assemble.

ApplicationContext provides basic features, including:

  • Publishing events to registered listeners

  • Methods for accessing application components

  • Internationalization support

  • Loading file resources

ApplicationContext is read-only while the application is running, but it can be reloaded if the implementation supports it. ApplicationContext is different from BeanFactory. BeanFactory creates a bean object when the getBean() method is called. ApplicationContext loads all the beans and creates objects at startup.

Spring Bean Scope

Spring Bean has four scopes,

  • singleton

    Singleton instance per Spring IoC container. This is the default.

  • prototype

    Scopes a single bean definition to any number of object instances. That means, every time, it requires to inject the bean or the getBean method is called.

Spring Understanding

I first learnt Spring in 2008 when I was a student, and I remember that day had a verify famous pattern named SSH, that including,

  • Spring

    Spring is used for manage beans, compared with EJB way, it's very easy for a beginner like me to learn and use.

  • Struts

    Apache Struts was used for MVC, although, it's outdated with current Spring MVC, but in that time, it's much better than using JSPs.

  • Hibernate

    Hibernate is never changed, it's always used as an ORM

After many years experience with Spring, and Spring boot, I am coming up with my mind,

Java Stream Guide

Assume we have the following classes,

public class User {
  private long id;
  private String name;
  private int age;

  private List<Address> address;

public class Address {
  private String postCode;
  private String street;
  private String building;

Inject Properties From YAML in Spring

In Spring, we can easily define configuration value in a YAML file, and inject those configuration into a Java class.

Take the following the YAML configuration file as an example.

server: localhost

# by default, spring split the string with comma
listValues: a,b,c,d
# if we want to split the string with customize character, we can use Spring EL
listValuesWithSpEL: a,b;c,d;e,f

# we can inject the whole complexObject into another class, with annotation ConfigurationProperties
    port: 90
    ssl: false
    listValues: a,b,c,d
    listValuesWithSpEL: a,b;c,d;e,f