spring工具类获取bean


import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

/**
 * 工具类-spring bean*/
public class BeanUtil {
    private static WebApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();

    /**
     * 根据bean名称获取
     * 
     * @param name
     * @return
     */
    public static Object getBean(String name) {
        return ctx.getBean(name);
    }
}

 



 

使用方法

PluginsAccountDao pluginsAccountDao = (PluginsAccountDao) BeanUtil.getBean("pluginsAccountDao");

 

也可以获取service

 

posted @ 2017-08-08 14:34  这个名字想了很久~  阅读(1374)  评论(0编辑  收藏  举报