0

i want to create a custom el functions to get in a fast way select options from dao. I'm using Spring and i want to inject spring bean dao in my custom el functions class.

In el functions class i'm using static methods and i'm unable to access application context. I used an implementation of ApplicationContextAware in this way

public class AppContextUtil implements ApplicationContextAware
{

    private ApplicationContext applicationContext;

    private static final AppContextUtil instance=new AppContextUtil();

    private AppContextUtil()
    {
    }

    public static AppContextUtil getInstance()
    {
        return instance;
    }

    public <T> T getBean(Class<T> clazz)
    {
        return applicationContext.getBean(clazz);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
    {
        this.applicationContext = applicationContext;
    }

}

but applicationContext is null.

The only way to access to applicationContext is as belove

WebApplicationContext appCtx =
WebApplicationContextUtils.getWebApplicationContext(context.getServletContext());
MyDAO myDAO = appCtx.getBean(MyDAO.class);

but in this way i need to pass PageContext in el functions params.

How i can create an el functions class with spring bean support? how i can access in static way to applicationContext?

Thank you.

vertti
  • 6,847
  • 3
  • 43
  • 77
Premier
  • 3,952
  • 6
  • 39
  • 56

1 Answers1

0

A dirty solution to "inject" a bean or Application Context into an static field:

@Component
public class AppContextUtil  {

    private static ApplicationContext applicationContext;

    @Autowire
    private set ApplicationContext(ApplicationContext applicationContext) {
       AppContextUtil.applicationContext = applicationContext;
    }
}
Ralph
  • 111,219
  • 48
  • 270
  • 362