Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • ApplicationContextAware and BeanNameAware Interfaces in Spring

    • 0
    • 1
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 1.80k
    Comment on it

    ApplicationContextAware Interface : Like in every web applications, we can also use application context in Spring Framework. Spring Framework provide this object in a different way. One way is that, to create new instance every time when you need the context object (e.g new ApplicationContext()) and another way to use spring out of the box functionality called Aware interfaces. To get context object you need to implement the ApplicationContextAware interface.

    ApplicationContextAware interface provide one method called setApplicationContext(). This method is used to inject the ApplicationContext instance at runtime or when container initializes the beans. At the time of the bean life cycle, container first search for the implemented Aware interfaces. If it found then it will call the method defined by that interface.

    You can create the util class which provides you the singleton object of ApplicationContext. For example:

    public class ApplicationContextUtils implements ApplicationContextAware {
        private static ApplicationContext ctx;
        @Override
        public void setApplicationContext(ApplicationContext appContext)
                throws BeansException {
            ctx = appContext;
        }
    
        public static ApplicationContext getApplicationContext() {
            return ctx;
        }
    }
    

    And you can call the static method getApplicationContext() to get the application context object.

    BeanNameAware Interface : A bean implements the BeanNameAware interface to get the bean name defined in the Application context or configuration file. It awares the bean that what name is used to refer this bean in context. BeanNameAware has defined one method called setBeanName() to set the name of the bean at the time of initialization.

    Example of ApplicationContextAware and BeanNameAware interfaces :

    Address.java

    package com.evon.example.applicationContextAware;
    import org.springframework.beans.factory.BeanNameAware;
    public class Address implements BeanNameAware {
        private String city;
        private String country;
        static String nameOfBean;
    
        public String getCity() {
            return city;
        }
        public void setCity(String city) {
            this.city = city;
        }
        public String getCountry() {
            return country;
        }
        public void setCountry(String country) {
            this.country = country;
        }   
        @Override
        public void setBeanName(String beanName) {
            nameOfBean = beanName;
        }   
        public String getNameOfBean() {
            return nameOfBean;
        }
    }
    

    Student.java

    package com.evon.example.applicationContextAware;
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.BeanNameAware;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    public class Student implements ApplicationContextAware, BeanNameAware{
        private String studName;
        private String course;
        private Address address;
        private String nameOfBean;
    
        ApplicationContext appContext;
    
        public String getStudName() {
            return studName;
        }
        public void setStudName(String studName) {
            this.studName = studName;
        }
        public String getCourse() {
            return course;
        }
        public void setCourse(String course) {
            this.course = course;
        }
        public Address getAddress() {
            if(address==null)getAddressFromContext();       
            return address;
        }
        public void setAddress(Address address) {
            this.address = address;
        }   
        public String getNameOfBean() {
            return nameOfBean;
        }
        private void getAddressFromContext() {
            setAddress((Address)appContext.getBean(Address.nameOfBean));
        }
    
        @Override
        public void setApplicationContext(ApplicationContext appContext)
                throws BeansException {
            this.appContext = appContext;
        }   
    
        @Override
        public void setBeanName(String beanName) {
            this.nameOfBean = beanName;
        }
    }
    

    bean.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:c="http://www.springframework.org/schema/c"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans.xsd">
        <bean id="studentBean" class="com.evon.example.applicationContextAware.Student">
            <property name="studName" value="John"/>
            <property name="course" value="MBA"/>
        </bean>
        <bean id="addressBean" class="com.evon.example.applicationContextAware.Address">
            <property name="city" value="Delhi"/>
            <property name="country" value="India"/>
        </bean>  
     </beans>
    

    Main.java

    package com.evon.example.applicationContextAware;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;
    public class Main {
        public static void main(String[] args) {
            ApplicationContext context = new FileSystemXmlApplicationContext("/src/main/java/com/evon/example/applicationContextAware/bean.xml");
            Student student = (Student)context.getBean("studentBean");
            System.out.println("student name : "+student.getStudName());
            System.out.println("student course : "+student.getCourse());
            System.out.println("city : "+student.getAddress().getCity());
            System.out.println("country : "+student.getAddress().getCountry());
            System.out.println("Student Bean Name or Id : "+student.getNameOfBean());
            System.out.println("Address Bean Name or Id : "+ Address.nameOfBean);
        }
    }
    

    Output:

    student name : John
    student course : MBA
    city : Delhi
    country : India
    Student Bean Name or Id : studentBean
    Address Bean Name or Id : addressBean
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: