Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to load ApplicationContext in spring framwork?

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

    ApplicationContext is an interface and it's provides configuration information to an application. Here multiple classes are provided by springframework that implements this interface and helps us to use configuration information in applications. ApplicationContext provides standard beanfactory lifecycle capabilities. Important capability which we will be using in below code is, class implementing ApplicationContext and it should be scan for ApplicationContextAware beans and invoke setApplicationContext by passing an implementation of its instance.

    Create project in your eclipse and follow the below steps
    Step: 1 Create spring-context.xml and put below code.

    <?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:mvc="http://www.springframework.org/schema/mvc"
            xmlns:context="http://www.springframework.org/schema/context"
            xsi:schemaLocation="
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
            <bean id="applicationContextUtils" class="com.findnerd.spring.ApplicationContextUtils"></bean>
    
            <bean id="helloWorld" class="com.findnerd.spring.HelloWorld" />
    
            <bean id="strHelloWorld" class="java.lang.String">
                    <constructor-arg value="Hello World" />
            </bean>
    
    </beans>
    

    Step: 2 Create ApplicationContextUtils.java
    Create the following utility class, it implements ApplicationContextAware and provides the setApplicationContext which will be invoked by spring container and the applicationContext will be passed by it. We store it in a static variable and expose it through a get method so that it can be accessed all through the application.

    package com.findnerd.spring;
    /** * Developer : Bhagwan Singh */
    
    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    
    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;
            }
    }
    

    Step: 3 Create HelloWorld.java
    This is a java bean which use our utility method to get access to the ApplicationContext.

    package com.findnerd.spring;
    
    /** * Developer : Bhagwan Singh */
    import org.springframework.context.ApplicationContext;
    
    public class HelloWorld {
    
            public String getValueFromContext(String beanName) {
                    ApplicationContext appCtx = ApplicationContextUtils
                                    .getApplicationContext();
                    String strFromContext = (String) appCtx.getBean(beanName);
                    return strFromContext;
            }
    }
    

    Step: 4 Create TestAppContext.java

    package com.findnerd.spring;
    /** * Developer : Bhagwan Singh */
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class TestAppContext {
    
            public static void main(String[] args) {
                    ApplicationContext appContext = new ClassPathXmlApplicationContext(
                                    "META-INF/spring-context.xml");
                    HelloWorld hw = (HelloWorld) appContext.getBean("helloWorld");
    
                    String output = hw.getValueFromContext("strHelloWorld");
                    System.out.print(output);
            }
    }
    

 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: