Monday, 18 September 2017

How to get the ApplicationContext reference in a bean ?


In some situation, you may want to get the reference of Spring ApplicationContext (which is initialized once) somewhere in bean.

GET APPLICATIONCONTEXT REFERENCE IN THE BEAN
1. Implement interface ApplicationContextAware in your bean.
2. Define a member variable of type ApplicationContext
3. Override setApplicationContext method in your bean

public class Triangle implements ApplicationContextAware {
  ...
  ApplicationContext context = null;
  ...
  @override
  public void setApplicationContext(ApplicationContext context) 

         throws BeansException {
    this.context = context;
  }
}



No comments:

Post a Comment

Note: only a member of this blog may post a comment.