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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM