您的位置:首页 > 编程语言 > Java开发

spring boot获取注入的实例bean

2016-05-13 18:11 513 查看
之前实现的在spring boot里集成的工具类获取spring 注入的bean,期间出现很多问题,其中@configuartion一定要加。再是实现applicationContextAware接口!

@Configuration

public class BeanTools implements ApplicationContextAware {

private static ApplicationContext applicationContext;

public void setApplicationContext(ApplicationContext context) {

applicationContext = context;

}

public static Object getBean(Class classname) {

try{

Object _restTemplate = applicationContext.getBean(classname);

return _restTemplate;

}catch(Exception e){

return "";

}

}

public static void setApplicationContext1(ApplicationContext context) {

applicationContext = context;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: