The use of Aware interface in Spring-ApplicationContextAware and BeanNameAware

When ApplicationContext creates an object instance that implements the org.springframework.context.ApplicationContextAware interface, the instance will provide a reference to the ApplicationContext. The following shows the definition of the ApplicationContextAware interface:

public interface ApplicationContextAware {
    void setApplicationContext(ApplicationContext applicationContext) throws BeansException;
}

Therefore, beans can create their own ApplicationContext programmatically, and can expose additional functions through the ApplicationContext interface or a reference to a subclass of this interface (such as ConfigurableApplicationContext). One use is to programmatically retrieve other beans. Sometimes this ability is useful. However, in general, you should avoid using it because it couples the code with Spring, and does not follow the IOC style of passing dependencies as attributes to beans like other methods. Other methods of ApplicationContext provide access and release to file resources. Application events and access to MessageResource. These additional functions are described in Additional Functions of ApplicationContext.

In Spring 2.5, autowiring is another option for obtaining ApplicationContext references. The "traditional" constructor and byType auto-assembly mode (as described in Auto-Assembly) can respectively provide ApplicationContext type dependencies for constructor parameters or setter method parameters. In order to obtain greater flexibility, it includes auto-assembly fields and multiple For the ability of parameter methods, please use the new annotation-based autowiring function. If you perform this operation, the ApplicationContext will be automatically assembled into the fields, constructor parameters, or method parameters that require the ApplicationContext type, provided that the related fields, constructors or methods Annotated with @Autowired . For details, see Using @Autowired .

When the ApplicationContext creates a class that implements the org.springframework.beans.factory.BeanNameaWare interface, it will provide the class with a reference to the name defined in its associated object definition. The following list shows the definition of the BeanNameaWare interface:

public interface BeanNameAware {
    void setBeanName(String name) throws BeansException;
}

This method will call the callback after filling the ordinary bean properties, but before the initialization callback (such as Initializingbean, afterPropertieSset, or custom init-method). In addition to ApplicationContextAware and BeanNameAware (discussed earlier), Spring also provides a series of Aware callback interfaces to allow beans to indicate to the container that they need some kind of infrastructure dependency. As a general rule, the name indicates the type of dependency. The following table summarizes the most important Aware interfaces:

Name

Dependency injection

ApplicationContextAware

Declare ApplicationContext

ApplicationEventPublishAware

Encapsulate ApplicationContext event publishing

BeanClassLoaderAware

Class loader for loading bean classes

BeanFactoryAware

Declare a BeanFactory

BeanNameAware

Declare the name of the bean

BootstrapContextAware

The resource adapter BootstrapContext that the container runs on. Usually only available in ApplicationContext instances that support JCA.

LoadTimeWeaverAware

Define the defined weaver for the processing class definition at load time.

MessageSourceAware

Configuration strategy used to parse messages (support parameterization and internationalization)

NotificationPublisherAware

Spring JMX announcement

ResourceLoaderAware

Loaders are configured for low-level access to resources.

ServletConfigAware

The current Servletconfig running by the container. Only valid in web-aware SpringApplicationContext.

ServletContectAware

The current ServletContext container in which the container is running. Only valid in web-aware SpringApplicationContext.

 Spring provides many Aware interfaces. As shown in the table above, what is the use of these Aware? The following explains the use of Aware. Aware is provided so that we can use the functions of some interfaces or classes provided by Spring. Let's take ApplicationContextAware as an example. If we want to use ApplicationContext, we can implement the ApplicationContextAware interface in the defined class. The code is as follows:

@Component
public class ApplicationContextAwareBean implements ApplicationContextAware {
    //ApplicationContext实例会在ApplicationContextAwareBean 实例化时通过
    //下面的setApplicationContext方法赋值
    //我们在该类可以使用ApplicationContext实例提供的任何功能
    private ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}

In addition to ApplicationContextAware, we can also implement the ApplicationEventPublisherAware interface, which will set an ApplicationEventPublisher instance. The code is as follows. We can publish events through the ApplicationEventPublisher instance. For details, please refer to the blog Spring event handling mechanism-ApplicationEvent .

public interface ApplicationEventPublisherAware extends Aware {
	void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher);
}

 

Guess you like

Origin blog.csdn.net/wk19920726/article/details/108740755