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