6

If I need access to the ApplicationContext in the constructor of my bean class is there a way I can configure the bean in XML rather than implementing ApplicationContextAware?

Note: I know I can do this using annotation-driven configuration and marking the constructor with @Autowire. I'm specifically interested in whether it's possible with XML configuration.

Rob Fletcher
  • 8,317
  • 4
  • 31
  • 40
  • You can try with `` but not sure if that works. I wouls say `@Autowired` is the only way. I would like to note btw, that the need for the `ApplicationContext` would raise a red flag on my part because generally you don't want or need this. – M. Deinum Feb 03 '15 at 07:25
  • Well, I generally agree but the thing I'm working on is a small library that integrates something with Spring so it's totally appropriate to tie it to Spring concepts. – Rob Fletcher Feb 03 '15 at 07:27
  • `` doesn't work, sadly. – Rob Fletcher Feb 03 '15 at 07:30
  • This works using configuration class instead of XML ... so I guess there should be a way to make it works in XML also. Did you get any advance on this? – Rafael Apr 06 '18 at 11:29
  • I need the same.. Don't know how to get it.. :( – grundic May 03 '18 at 19:15

1 Answers1

1

The need to

access to the ApplicationContext in the constructor of my bean class

is already "bad"...: [1] [2] (please primarily try to get rid of this need/change your mind!:)

Of course, ref="applicationContext" would be nice, but it doesn't work/noone knows how, ...but this "sledgehammer" approach can overcome: [3] (2007...mmmhk, ..."the wheel" is even older....;)

..a "singelton application context wrapper bean", which you can (constructor-) inject (almost;) everywhere. (As in xml, as in java config.)

ApplicationContextWrapper.java:

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
*  Thx to: http://sujitpal.blogspot.com/2007/03/accessing-spring-beans-from-legacy-code.html
**/
public class ApplicationContextWrapper implements ApplicationContextAware {
  /** the original post has static variable and get-method, but within spring context,
  * and clean initialisation&wiring, it can be oblet.**/
  private /*static*/ ApplicationContext CONTEXT;

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

  public /*static*/ ApplicationContext getContext() {
    return CONTEXT;
  }
}

applicationContext.xml:

<bean id="myWrapper" class="[some.package.]ApplicationContextWrapper"/>

<bean id="myCtxtDependentBean" class="[some.package.]CrazyBean" >
   <constructor-arg ref="myWrapper" />
</bean>

CrazyBean.java:

public class CrazyBean {

    public CrazyBean(ApplicationContextWrapper wrapper) {
        ApplicationContext ctxt = wrapper.getContext();
        // do crazy stuff here :)
    }

    // or in a static variant, just:
    public CrazyBean() { // ...no arguments, no injection
       ApplicationContext ctxt = ApplicationContextWrapper.getContext();
       // but ensure setApplicationContext's been called...
    }
}
xerx593
  • 12,237
  • 5
  • 33
  • 64