afrag  
记录学习和成长的历程
日历
<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678
统计
  • 随笔 - 9
  • 文章 - 5
  • 评论 - 2
  • 引用 - 0

导航

常用链接

留言簿

随笔分类

随笔档案

文章档案

搜索

  •  

积分与排名

  • 积分 - 9899
  • 排名 - 2390

最新评论

阅读排行榜

评论排行榜

 

ApplicationContextBeanFactory提供了更多的功能,因此一般情况下都会使用ApplicationContext,只有在资源有限的情况下(例如在移动设备上)才使用BeanFactory

       ApplicationContext的多个实现中,最常用的有3个:

       ClassPathXmlApplicationContext:在所有的classpath中查找指定的xml文件。

       FileSystemXmlApplicationContext:在文件系统中查找指定的xml文件。(可以指定相对路径,当前路径为当前目录)。

       XmlWebApplicationContext:从一个web应用程序中包含的xml文件中读取context定义。

       ApplicationContext是扩展的BeanFactory接口

public interface ApplicationContext extends ListableBeanFactory, HierarchicalBeanFactory,

              MessageSource, ApplicationEventPublisher, ResourcePatternResolver{

…………

}

       ApplicationContext获取对象也是使用getBean方法。

       ApplicationContextBeanFactory有一个很大的不同是:BeanFactory是在需要bean的时候才会实例化beanApplicationContext会在装入context的时候预先装入所有的singletonbean。(singleton是在bean的定义中<bean>元素的一个属性,缺省值为true)。

例如,针对上一节的例子,我们将ExecutableApp类更改为:
import org.springframework.beans.factory.BeanFactory;

import org.springframework.beans.factory.xml.XmlBeanFactory;

import org.springframework.core.io.FileSystemResource;

 

public class ExecutableApp {

         public ExecutableApp () {

         }

 

public static void  main(String args[]){

         System.out.println(“Before load xml file”);

ApplicationContext  factory= new FileSystemXmlApplicationContext("configuration.xml");

System.out.println(“After load xml file”);

                  Greeting personA = (Greeting)factory.getBean("greetingService");

personA.sayHello();

    }

}

运行的结果是:
Before load xml file

Instance GreetingImpl object

Instance MrSmith object

After load xml file

Hi,Mr Smith

也就是说,ApplicationContext在装载xml文件的同时就实例化了GreetingImpl类和MrSmith类。

如果我们将xml文件的更改为:
<?xml version = "1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTDBEAN//EN"

"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

  <bean id="person" class="MrSmith" singleton"false"/>

  <bean id="greetingService"

    class="GreetingImpl" singleton="false">

    <property name="greeting">

      <value>Hello</value>

</property>

<property name="who”>

  <ref bean="person"/>

</property>

  </bean>

</beans>

那么ExecutableApp的运行结果是:
Before load xml file

 

After load xml file

 

Instance GreetingImpl object

 

Instance MrSmith object

 

Hi,Mr Smith

 

也就是说,在装载xml文件时,ApplicationContext并没有实例化GreetingImpl类和MrSmith类,直到需要这两个类的时候才会实例化它们。
posted on 2006-01-08 20:40 afrag 阅读(568) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 
 
Copyright © afrag Powered by: 博客园 模板提供:沪江博客