Spring获取ApplicationContext对象工具类的实现方法

(1)实现的工具类:

package com.util;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

final public class ApplicationContextUtil {

  private static ApplicationContext ac=null;

  private ApplicationContextUtil(){

  }

  static{
    ac=new ClassPathXmlApplicationContext("applicationContext.xml");
  }

  public static ApplicationContext getApplicationContext(){
    //获得返回的容器对象
    return ac;
  }

}

(2)使用方法:

public static void getByUtil(){
    ApplicationContextUtil.getApplicationContext().getBean("userService");
  }

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

02-08 06:53