spring加载时调用controller

/ Java / 0 条评论 / 1480浏览
@Component public class SpringUtil implements ApplicationContextAware{ private static ApplicationContext applicationContext = null;   @Override  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { if(SpringUtil.applicationContext == null){
            SpringUtil.applicationContext = applicationContext;  }
    } //获取applicationContext  public static ApplicationContext getApplicationContext() { return applicationContext;  } //通过name获取 Bean.  public static Object getBean(String name){ return getApplicationContext().getBean(name);  } //通过class获取Bean.  public static <T> T getBean(Class<T> clazz){ return getApplicationContext().getBean(clazz);  } //通过name,以及Clazz返回指定的Bean  public static <T> T getBean(String name,Class<T> clazz){ return getApplicationContext().getBean(name, clazz);  }
}

port区分环境

//config 1-k8s type=2 线上 准生产 0-测试(默认) ApplicationContext context = SpringUtil.getApplicationContext(); autoTestController= context.getBean(AutoTestController.class); //调用controller方法 String port = context.getEnvironment().getProperty("local.server.port"); JSONObject obj = new JSONObject(); int type = 0; if(port.equals("5013")) {
    type=2; }else if(port.equals("5014")) {
    type=1; }else{
    type = 0; }
obj.put("type", type); autoTestController.configUrl(obj);