【Spring】10 BeanFactoryAware interface


The Spring framework provides developers with a wealth of extension points, one of which is the callback interface in the Bean life cycle. This article will focus on introducing an important interface BeanFactoryAware , discuss its function, usage, and application scenarios in actual development.

1 Introduction

In Spring, BeanFactoryAware interface is a callback interface that provides a method for setting up a Bean factory. When a Bean implements the BeanFactoryAware interface, after the Bean instance is instantiated, the Spring container will call the setBeanFactory method and pass the factory where the Bean is located as a parameter Pass it in.

The source code is as follows

Insert image description here

2. Function

BeanFactoryAware is mainly used to obtain a reference to the factory (BeanFactory) that loads the current Bean, so that the Bean can obtain information about its own factory at runtime.

3. Use

To make a Bean implement the BeanFactoryAware interface, you need to follow the following steps

Insert image description here

3.1 Create and implement the interface
package org.example.cheney;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;

public class DemoBean implements BeanFactoryAware {
    
    
    @Override
    public void setBeanFactory(BeanFactory beanFactory) {
    
    
        System.out.println("【BeanFactoryAware】Bean 的工厂是:" + beanFactory);
    }
}
3.2 Configure Bean information
xmlCopy code<bean id="myBean" class="com.example.MyBean"/>
3.3 Create startup class
package org.example.cheney;

import org.springframework.context.support.AbstractXmlApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
    
    
    public static void main(String[] args) {
    
    
        String location = "applicationContext.xml";
        try (AbstractXmlApplicationContext context = new ClassPathXmlApplicationContext(location)) {
    
    
            System.out.println("End.");
        }
    }
}

3.4 Startup

Output result:

Insert image description here

4. Application scenarios

BeanFactoryAwareInterfaces are usually used in the following scenarios:

  • Get the Bean factory reference:

    When a Bean needs to obtain a reference to its own factory at runtime in order to perform some factory-related operations

  • Dynamically load other beans:

    Sometimes other beans need to be loaded dynamically, and the factory reference required to load is the factory that loads the bean.

Summarize

The Spring framework provides developers with a wealth of extension points, one of which is the callback interface in the Bean life cycle. The BeanFactoryAware interface provides developers with a simple and useful way to obtain a reference to the factory where a Bean is located. By implementing this interface, a Bean can obtain a reference to its own factory during the initialization phase, thereby handling some factory-related logic more flexibly.

Guess you like

Origin blog.csdn.net/yanyc0411/article/details/135040075