spring获取bean的几种方式

http://flatfish2000.iteye.com/blog/465667

 

  1. ApplicationContext ac = new FileSystemXmlApplicationContex("applicationContext.xml");   
  2.     ac.getBean("beanId");  

    ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("appliactionContext.xml");

 

   
  1. ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc)   
  2. ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc)   
  3.     ac1.getBean("beanId");   
  4.     ac2.getBean("beanId");  
 import org.springframework.web.context.support.WebApplicationContextUtils;
ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc)
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc)
    ac1.getBean("beanId");
    ac2.getBean("beanId");

   public class DicBean implements ServletContextAware{
   
private ServletContext servletContext;

public void setServletContext(ServletContext sc) {
    this.servletContext=sc;
    System.out.println("项目的绝对路径为:"+servletContext.getRealPath("/"));
}
}


你可能感兴趣的:(spring,bean,xml,Web,Blog)