码迷,mamicode.com
首页 > 编程语言 > 详细

初始化spring容器的几种方法

时间:2014-11-04 17:08:45      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   java   sp   div   on   log   

package ssh.spring;
 
import java.io.IOException;
 
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
 
public class Test {
   @org.junit.Test
   public void test1(){
      ApplicationContext ac=new ClassPathXmlApplicationContext("ssh/spring/applicationContext.xml");
      Person p1=(Person)ac.getBean("person");
      System.out.println("test1 "+p1);
   }
   @org.junit.Test
   public void test2(){
      ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml",this.getClass());
      Person p1=(Person)ac.getBean("person");
      System.out.println("test2 "+p1);
   }
   @org.junit.Test
   public void test3(){
      Resource resource=new ClassPathResource("ssh/spring/applicationContext.xml");
      BeanFactory beanFactory=new XmlBeanFactory(resource);
      Person p1=(Person)beanFactory.getBean("person");
      System.out.println("test3 "+p1);
   }
   @org.junit.Test
   public void test4() throws IOException{
      Resource resource=new ClassPathResource("applicationContext.xml",this.getClass());
      BeanFactory beanFactory=new XmlBeanFactory(resource);
      Person p1=(Person)beanFactory.getBean("person");
      System.out.println("test4 "+p1);
   }
   @org.junit.Test
   public void test5(){
      Resource resource=new FileSystemResource("E:/Java/study/WebRoot/WEB-INF/classes/ssh/spring/applicationContext.xml");
      BeanFactory beanFactory=new XmlBeanFactory(resource);
      Person p1=(Person)beanFactory.getBean("person");
      System.out.println("test5 "+p1);
   }
}

 

初始化spring容器的几种方法

标签:style   blog   io   color   java   sp   div   on   log   

原文地址:http://www.cnblogs.com/fabulousyoung/p/4073845.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!