tomcat启动后,在普通java类中获取spring管理的bean和ServletContext,(经过验证,可以取到)

//从spring容易中获取bean
public static Object getBean(String beanName){
ApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
return context.getBean(beanName);
}
//获取ServletContext
public static ServletContext getServletContext(){
WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
return context.getServletContext();
}
上一篇:在Servlet中获取Spring注解的bean


下一篇:Servlet中获取Spring管理的bean