获取Spring对象的公共方法

来源:互联网 发布:局域网网络行为监控 编辑:程序博客网 时间:2024/06/06 07:02


 第一种方式(in hedao):

/** * 获取Spring对象的公共方法 *  * @author luoyue * */public class BeanHolder { public static Object getBean(String beanId) {  WebApplicationContext ctx = WebApplicationContextUtils    .getRequiredWebApplicationContext(ApplicationContext      .getContext().getServletContext());  return ctx.getBean(beanId); }}

第二种方式(in yingbo):

ApplicationContext ac = new FileSystemXmlApplicationContext("E:/Workspaces/MyEclipse 10/crawler/resources/applicationContext.xml");TaskDistributor taskDistributor = (TaskDistributor)ac.getBean(beanId);

beanId 为spring配置文件中javabean的id
原创粉丝点击