Spring容器的初始化4种方式

1.从classpath路径中加载applicationContext.xml文件。

Resource resourc=new ClassPathResource("applicationContext.xml"); 
XmlBeanFactory bean=new  XmlBeanFactory(); 
BeanA a=(BeanA)bean.getBean("beanid");

2.从文件系统中加载。

Resource resourc=new FileSystemResource("src:/applicationContext.xml"); 
     
或者:如果applicationContext.xml文件放在D:根目录下 
     
Resource resource=new FileSystemResource("d:/applicationContext.xml"); 
XmlBeanFactory bean=new  XmlBeanFactory(resource); 
BeanA a=(BeanA)bean.getBean("beanid");
 

3.从输入流中加载。可以用于网络.

InputStream is=new FileInputStream("d:/applicationContext.xml") 
Resource res=new  InputStreamResource(is); 
XmlBeanFactory bean=new  XmlBeanFactory(res); 
BeanA a=(BeanA)bean.getBean("beanid");
 

4.用于加载多个配置文件。

String[] configFile={"applicationContext-service.xml","applicationContext-dao.xml"}; 
ClassPathXmlApplicationContext ctx =new  ClassPathXmlApplicationContext(configFile); 
MyService = (MyService ) ctx.getBean("myService ");

 

posted on 2013-10-05 21:05  玲珑go  阅读(766)  评论(0编辑  收藏  举报

导航