获取bean的方式

来源:互联网 发布:淘宝网下载到电脑上 编辑:程序博客网 时间:2024/09/21 09:21

1、监听器中获取

public static Object getBean( ) {

        WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
        return context.getBean(“userDao”);

}

2、ApplicationContext context = new ClassPathXmlApplicationContext("/config/applicationContext.xml", "/config/applicationContext-dao.xml");

      Scheduler scheduler = (Scheduler) context.getBean("quartzScheduler");

3、 servlet获取

private Object getBean( ) {
        ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        return ctx.getBean(ParameterService.class);
    }


4、过滤器中获取

public void init(FilterConfig filterConfig) throws ServletException {
        ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(filterConfig.getServletContext());
        sysUtil = ctx.getBean(SysUtil.class);
    }