Automatic assembly - Aware injection into Spring underlying components & Principles

23. Automatic assembly - Aware injection into Spring underlying components & Principles

  • Aware interface, which provides functions similar to callback functions
  • Custom components want to use some components (Application Context, Bean Factory) at the bottom of Spring container; custom components need to implement the xxaware interface; when creating objects, they will call methods specified by the interface to inject relevant components
package org.springframework.beans.factory;

public interface Aware {

}

23.1 automatic injection of applicationcontextaware into IOC container

package org.springframework.context;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.Aware;

public interface ApplicationContextAware extends Aware {

    void setApplicationContext(ApplicationContext applicationContext) throws BeansException;

}

23.2 ApplicationEventPublisherAware injection event dispatcher

package org.springframework.context;

import org.springframework.beans.factory.Aware;

public interface ApplicationEventPublisherAware extends Aware {

    void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher);

}

23.3 BeanClassLoaderAware class loader

package org.springframework.beans.factory;

public interface BeanClassLoaderAware extends Aware {

    void setBeanClassLoader(ClassLoader classLoader);

}

23.4 BeanFactoryAware Bean factory

package org.springframework.beans.factory;

import org.springframework.beans.BeansException;
public interface BeanFactoryAware extends Aware {

    void setBeanFactory(BeanFactory beanFactory) throws BeansException;

}

23.5 BeanNameAware Bean name

package org.springframework.beans.factory;

public interface BeanNameAware extends Aware {

    void setBeanName(String name);

}

23.6 EmbeddedValueResolverAware Embedded value resolver

package org.springframework.context;

import org.springframework.beans.factory.Aware;
import org.springframework.util.StringValueResolver;

public interface EmbeddedValueResolverAware extends Aware {

    void setEmbeddedValueResolver(StringValueResolver resolver);

}

23.7 EnvironmentAware environment

package org.springframework.context;

import org.springframework.beans.factory.Aware;
import org.springframework.core.env.Environment;

public interface EnvironmentAware extends Aware {

    void setEnvironment(Environment environment);

}

23.8 ImportAware import related

package org.springframework.context.annotation;

import org.springframework.beans.factory.Aware;
import org.springframework.core.type.AnnotationMetadata;

public interface ImportAware extends Aware {

    void setImportMetadata(AnnotationMetadata importMetadata);

}

23.9 LoadTimeWeaverAware import related

package org.springframework.context.weaving;

import org.springframework.beans.factory.Aware;
import org.springframework.instrument.classloading.LoadTimeWeaver;

public interface LoadTimeWeaverAware extends Aware {

    void setLoadTimeWeaver(LoadTimeWeaver loadTimeWeaver);

}

23.10 message source aware internationalization

package org.springframework.context;

import org.springframework.beans.factory.Aware;

public interface MessageSourceAware extends Aware {

    void setMessageSource(MessageSource messageSource);

}

23.11 notification publisheraware sending notification support

package org.springframework.jmx.export.notification;

import org.springframework.beans.factory.Aware;

public interface NotificationPublisherAware extends Aware {

    void setNotificationPublisher(NotificationPublisher notificationPublisher);

}

23.12 ResourceLoaderAware resource loader

package org.springframework.context;

import org.springframework.beans.factory.Aware;
import org.springframework.core.io.ResourceLoader;

public interface ResourceLoaderAware extends Aware {

    void setResourceLoader(ResourceLoader resourceLoader);

}

23.13 test cases

package com.hw.springannotation.beans;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.stereotype.Component;
import org.springframework.util.StringValueResolver;

/**
 * @Description TODO
 * @Author hw
 * @Date 2018/11/28 15:44
 * @Version 1.0
 */
@Component
public class Red implements ApplicationContextAware, BeanNameAware, EmbeddedValueResolverAware {

    private ApplicationContext applicationContext;

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("Afferent IOC " + applicationContext);
        this.applicationContext = applicationContext;
    }

    public void setBeanName(String name) {
        System.out.println("current bean Name" + name);
    }

    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        String value = resolver.resolveStringValue("Hello ${os.name} I am#{20*20}");
        System.out.println("Parsed string:" + value);
    }
}

Function:

 

To put it bluntly, Aware interface class is an abstraction of callback class

 

Combined with the following words to see more feeling:


Spring source code analysis: Spring Aware principle analysis
https://blog.csdn.net/qq_24210767/article/details/90146173

Spring 4.1.8 extension Practice II: Aware interface uncovering
https://blog.csdn.net/boling_cavalry/article/details/81611426


Automatic assembly - Aware injection into Spring underlying components & Principles
https://www.cnblogs.com/Grand-Jon/p/10040199.html


What is Spring Aware?
https://www.cnblogs.com/FraserYu/p/11211235.html
 

Tags: Programming Spring

Posted on Wed, 06 Nov 2019 12:12:13 -0800 by rscott7706