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

Spring初始化Ioc容器的三种方法

2012-05-24 11:07 344 查看
1、

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

  Student stu = (Student)context.getBean("student");

  stu.setEmail("abc@126.com");

  

2、  

  String filePath = SpringAction.class.getResource("/").getPath();

  filePath = filePath.substring(1);

  Resource resource = new FileSystemResource(filePath+"applicationContext.xml");

  BeanFactory factory = new XmlBeanFactory(resource);

  Student stu1 = (Student)factory.getBean("student");

  stu1.setEmail("abc@163.com");

  3、

  ClassPathResource cr = new ClassPathResource("applicationContext.xml");

  BeanFactory beans = new XmlBeanFactory(cr);

  Student stu2 = (Student)beans.getBean("student");

  stu2.setEmail("abc@qq.com");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring ioc string