`

attachment spring配置全书.doc & chapter

阅读更多

三个接口:
BeanFactory  :Ioc容器,面向spring本身 (父)   1.手工加载postProcess.  2.getBean()的时候初始bean
ApplicationContext:Spring容器,面向开发者  (子)  1.postProcess配置文件中定义就可以了. 2.加载配置文件的时候就init bean (scope="prototype")
WebApplicationContext:面向Web,同时 bean可以是request,session,application,singleton,prototype

ApplicationContext的实现类
加载spring配置文件:ClassPathXmlApplicationContext , FileSystemXmlApplicationContext

 


1.构造Resource,用来加载配置文件(可以加载所有文件)
加载资源:(任意资源) ,这是因为ApplicationContext是ResourceLoader的子接口,该在接口定义方法getResource()
Resource res1 = ApplicationContext
    .getResource("file:/D:/masterSpring/chapter3/src/com/baobaotao/context/resource.txt");

Resource res = new ClassPathResource("com/baobaotao/beanfactory/beans.xml")

//res1.getInputStream(); 可以把Resource转换成为InputStream,或者其他你希望的流 (Resource 还有其他的子接口和实现类?)

2.XmlBeanFactory reads bean definitions from an XML document ,这是一个很重要的类 !!!
BeanFactory bf = new XmlBeanFactory(res);//resource做为参数
Car car = (Car) bf.getBean("car");

2.如果resource是一个普通的文件,可以按一般文件来读内容
FileReader fr = new FileReader(res1.getFile());
BufferedReader br =  new BufferedReader(fr);
String strText= "";
 while ( (strText = br.readLine())!=null){
  System.out.println(strText);
 }
   

3.对于加载配置文件,也可以通过这样的方式,简单快速
 String[] CONFIG_FILES = {"com/baobaotao/context/beans.xml"};

 ApplicationContext ctx = new ClassPathXmlApplicationContext(CONFIG_FILES);
 ApplicationContext ctx = new FileSystemXmlApplicationContext(CONFIG_FILES);
ctx.getBean("car");

 

org.springframework.util,Assert
org.springframework.util.ResourceUtils
Resource res1 = ctx
    .getResource(ResourceUtils.CLASSPATH_URL_PREFIX+"com/baobaotao/context/resource.txt");

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics