Spring injection outside application context

Are you using ApplicationContext (and friends) to create a single “spring context” for all your web application? If so, you are on the right way. And it is possible that you feel the need to inject properties where Spring isn’t accessible (like a Servlet Filter). You can use this little trick:

public static void inject(ServletContext context,
    Object bean) {
  WebApplicationContext wCtx =
      WebApplicationContextUtils
          .getWebApplicationContext(context);
  if (wCtx != null) {
    wCtx.getAutowireCapableBeanFactory()
        .autowireBeanProperties(bean,
            AutowireCapableBeanFactory.AUTOWIRE_NO,
            false);
  }
}

Tags: , ,

One Response to “Spring injection outside application context”

  1. Extreme Java » Blog Archive » Using Spring with Mojarra InjectionProvider WordPress 2.3.3 Says:

    […] months ago, I posted about using Spring injection outside application context. I forgot to mention that the same trick can be used to replace ELResolver solution if you are […]

Leave a Reply