Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3998610
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:32:50+00:00 2026-05-20T07:32:50+00:00

I am using Spring 3.0.3. I would like to use the applicationContextProvider so I

  • 0

I am using Spring 3.0.3.

I would like to use the applicationContextProvider so I declared:

<?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:aop="http://www.springframework.org/schema/aop"
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:tx="http://www.springframework.org/schema/tx"
     xsi:schemaLocation="
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    <bean id="applicationContextProvider" class="com.mycompany.util.ApplicationContextProvider"></bean>
    <context:annotation-config/>
    <tx:annotation-driven/>
</beans>

and my ApplicationContextProvider:

public class ApplicationContextProvider implements ApplicationContextAware {

    private static ApplicationContext applicationContext = null;

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
    public void setApplicationContext(ApplicationContext _applicationContext) throws BeansException {
        applicationContext = _applicationContext;

    }

}

But the set is never being called!

and whenever I am using ApplicationContextProvider.getApplicationContext() returns null.

why is it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:32:53+00:00Added an answer on May 20, 2026 at 7:32 am

    Part of the problem may be that your getter is static. So its possible for you to call it before Spring has created an instance of ApplicationContextProvider.

    You need to refer to the bean ‘applicationContextProvider’ that Spring has created for you when Spring is “ready” for you to use it. See Bean lifecycle

    E.g. via a Junit test with your bean in ‘app-context.xml’ in src/test/resources

    package com.mycompany.util;
    
    import static org.junit.Assert.*;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.ApplicationContext;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    @ContextConfiguration(locations="classpath:app-context.xml")
    @RunWith(SpringJUnit4ClassRunner.class)
    public class ApplicationContextProviderTest {
    
        @Autowired // Injected by Spring when bean is "ready"
        ApplicationContextProvider contextProvider;
    
    
        @Test
        public void testContext() {
            assertNotNull(contextProvider);
            ApplicationContext context = ApplicationContextProvider.getApplicationContext();
            assertNotNull(context);
    
            System.out.println("My context has " + context.getBeanDefinitionCount() + " beans");
        }
    }
    

    Then this gets a green bar for applicationContext being set.

    Example output (don’t leave System.out in the test btw).

    INFO : org.springframework.test.context.TestContextManager - @TestExecutionListeners is not present for class [class com.mycompany.util.ApplicationContextProviderTest]: using defaults.
    INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [app-context.xml]
    INFO : org.springframework.context.support.GenericApplicationContext - Refreshing org.springframework.context.support.GenericApplicationContext@4c331059: startup date [Sun Feb 27 13:38:13 GMT 2011]; root of context hierarchy
    INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@4b1c2b67: defining beans [applicationContextProvider,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor]; root of factory hierarchy
    My context has 5 beans
    INFO : org.springframework.context.support.GenericApplicationContext - Closing org.springframework.context.support.GenericApplicationContext@4c331059: startup date [Sun Feb 27 13:38:13 GMT 2011]; root of context hierarchy
    INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@4b1c2b67: defining beans [applicationContextProvider,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor]; root of factory hierarchy
    

    app-context.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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="applicationContextProvider" class="com.mycompany.util.ApplicationContextProvider"></bean>
    
    
    </beans>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to be able to use Spring using setter injection into Scala
I am using Spring 3. In my application context xml file I would like
I'm using the Spring class FreeMarkerConfigurationFactoryBean to retrieve FreeMarker templates. I would like these
I have a web application using JPA and JTA with Spring. I would like
I am using spring with appfuse framework and I would like to display two
I would like to use two different implementations for a DAO with Spring's testframework.
I'm developing a DAO using Spring JdbcDaoSupport and would like to know if anyone
I'm using Spring's support for JDBC. I'd like to use JdbcTemplate (or SimpleJdbcTemplate) to
I'm using Spring MVC (3.0) with annotation-driven controllers. I would like to create REST-ful
I would like to compare 4 character string using wildcards. For example: std::string wildcards[]=

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.