1

So I am trying to get applicationContext within a simple utility class but all my attempts so far yielded in a null object. How do I get hold of applicationContext in Sample.java.

First approach:

public class ApplicationContextProvider implements ApplicationContextAware
{

    private static ApplicationContext context;

    public ApplicationContext getApplicationContext()
    {
        return context;
    }

    @Override
    public void setApplicationContext( ApplicationContext ac ) throws BeansException
    {
        context = ac;
    }
}

Defined this bean in applicationContext as:

 <bean id="applicationContextProvder" class="com.premier.web.utils.ApplicationContextProvider"/>

Used it within my Java class as:

ApplicationContextProvider ctx = new ApplicationContextProvider();
ctx.getApplicationContext(); //Results in a null

Second approach: Directly AutoWired the applicationContext within my class.

@Autowired
private ApplicationContext appContext;
  • I don't believe the first approach failing can be explained by the duplicate answer... – Adam Mar 10 '15 at 21:14
  • @Adam `new ApplicationContextProvider();` is not a managed bean. From the duplicate _The field annotated `@Autowired` is null because Spring doesn't know about the copy of MileageFeeCalculator that you created with `new` and didn't know to autowire it._ Replace `@Autowired` with `ApplicationContextAware`. – Sotirios Delimanolis Mar 10 '15 at 21:15
  • Agreed, however he's using a static reference inside the bean.. in the first approach... – Adam Mar 10 '15 at 21:17
  • I don't see how that changes anything. Whether the field is `static` or not, it won't be set because `setApplicationContext` isn't called because the `ApplicationContextProvider` instance is not a bean. – Sotirios Delimanolis Mar 10 '15 at 21:18
  • He also creates it in an application context, " " earlier... which I would expect to have the side effect of setting the static field.... – Adam Mar 10 '15 at 21:19
  • Obviously all this is a massive misuse of spring, but I would have been interested in investigating how things behaved in this corner case... – Adam Mar 10 '15 at 21:20
  • @adam It would, indirectly, if it was a bean. Spring finds all `ApplicationContextAware` beans and invokes their `setApplicationContext` method, which would have set the `static` field. But this is not a Spring bean. – Sotirios Delimanolis Mar 10 '15 at 21:21
  • folks, this is just some trial thing on my end. I did define the 'ApplicationContextProvider' as a bean. That isn't enough? – springforward Mar 10 '15 at 21:21
  • No, you have to use the bean defined in the context. Here, you are instantiating the class yourself. Read the duplicate. – Sotirios Delimanolis Mar 10 '15 at 21:22
  • @SotiriosDelimanolis I have tested the approach given in "First approach" above. This actually works, despite your claims otherwise. I suspect the issue is the ordering of the bean being initialised and the call to ctx.getApplicationContext(); – Adam Mar 10 '15 at 21:30
  • @Adam It definitely doesn't work. You must have use an `ApplicationContext`. There is no `ApplicationContext` in the question as aksed. – Sotirios Delimanolis Mar 10 '15 at 21:54
  • I agree with Sotirius. This is the idea behind container. Please see example usage here: http://www.concretepage.com/spring/example_applicationcontextaware_spring – Gokhan Oner Mar 10 '15 at 22:05

0 Answers0