简体   繁体   中英

Inject spring bean in custom el functions

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.

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM