1. 程式人生 > >spring boot獲取注入的例項bean

spring boot獲取注入的例項bean

之前實現的在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;
 }
}