SoFunction
Updated on 2025-03-08

Detailed explanation of Aware interface functions in spring

There are many interfaces named after XXXAware in spring, and many people don’t know what these interfaces are used for. This article will describe some commonly used interfaces.

1. ApplicationContextAware

Gets the spring container to access other beans defined in the container. Implement interface method public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {}

eg:

package ;
 
import ;
import ;
import ;
/**
  * Get spring container to access other beans defined in the container
  */
public class SpringContextUtil implements ApplicationContextAware {
    // Spring application context    private static ApplicationContext applicationContext;
    /**
      * Implement the callback method of the ApplicationContextAware interface and set the context environment
      */
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
         = applicationContext;
    }
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
     * Get the object Rewrite herebeanmethod,Plays a major role
     *
     * @param name
     * @return  Object A registered with the given namebeanExamples
     * @throws BeansException
    public static Object getBean(String beanId) throws BeansException {
        return (beanId);
}

2. ApplicationEventPublisherAware

This is an event notification publishing interface that implements the public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) method. The class that implements the ApplicationListener<ApplicationEvent> interface can listen to this event notification in the onApplicationEvent(ApplicationEvent event) method.

eg: Source code source: /article/details?id=50970667

Define events:

package ;
 
import ;
/**
  * Define an event to send text messages
  * Implemented ApplicationEvent
  * @author zghw
  *
  */
public class SendMessageEvent extends ApplicationEvent {
    private static final long serialVersionUID = 1L;
    //Message object    private Message message;
     
    //source represents the source of the release of the event    public SendMessageEvent(Object source,Message message) {
        super(source);
         = message;
    }
    public Message getMessage() {
        return message;
    public void setMessage(Message message) {
}

Define the listener observer:

package ;
 
import ;
import ;
/**
  * Send a SMS listener and start sending when the event is heard.
  * Implement ApplicationListener
  * @author zghw
  *
  */
@Component
public class SendMessageListenter implements ApplicationListener&lt;SendMessageEvent&gt;{
    /**
      * Listen to the event SendMessage, and call this method when an event occurs.
      */
    public void onApplicationEvent(SendMessageEvent event) {
        Message message = ();
        String msg=();
        String phone = ();
        try {
            ("Start to the phone"+phone+"Send a text message, the text message content is:"+msg);
            (1000);
            ("Send the text message successfully!");
        } catch (InterruptedException e) {
            ();
        }
    }
}

Define the Event Registration Center and publish the Event Topic:

package ;
 
import ;
import ;
import ;
/**
  * Implement ApplicationEventPublisherAware to enable container ApplicationContext as event publishing center.
  * Because ApplicationContext implements ApplicationEventPublisher
  * @author zghw
  *
  */
@Service
public class UserService implements ApplicationEventPublisherAware{
    private ApplicationEventPublisher publisher;
     
    public void registerUser(String name,String phone) throws InterruptedException{
        ("Registered User");
        (300);
        ("Register complete!");
         
        Message message=new Message();
        ("Hello,"+name+"You've won 1000W");
        (phone);
        SendMessageEvent event=new SendMessageEvent(this,message);
        //Release Center Release Event        (event);
    }
    /**
      * Implement the ApplicationEventPublisherAware method. When spring is used, it will automatically inject the UserServicebean object for us.
      * Implementation of ApplicationEventPublisher
      */
    public void setApplicationEventPublisher(
            ApplicationEventPublisher applicationEventPublisher) {
         = applicationEventPublisher;
}

This is the end of this article about the detailed explanation of the Aware interface function in spring. For more related Aware interface content in spring, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!