只需要servlet 中的servletContext 对象作为参数 就可以获得spring中的bean对象,方法如下:
public static Object getBean(String name, ServletContext context) {
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);
return ctx.getBean(name);
}

本文介绍了一种通过servletContext对象获取Spring中bean的方法。利用WebApplicationContextUtils工具类,只需要提供servletContext即可轻松获取到所需的bean实例。

被折叠的 条评论
为什么被折叠?



