`

Spring框架中的ApplicationContext

阅读更多

读了外国的本书上才知道,"ApplicationContext"对象,才是构建了Spring框架的灵魂。包括全部的"依赖注入"都是在ApplicationContext内部完成的,如果说"依赖注入"是Spring框架的核心概念,那么"ApplicationContext"就是它的核心对象.

ApplicationContext对象是BeanFactory的特化,这里的特化大概意思是说:ApplicationContext对象可以看作是个功能齐全的BeanFactory。"BeanFactory"是充当Spring框架中所有Bean对象的注册表.通常BeanFactory是负责创建Bean对象,并且将各个Bean之间的依赖关系进行关联。一般的应用程序都是与ApplicationContext互交,而不是和BeanFactory互交.ApplicationContext能通过运行BeanFacotoryPostProcessors自动的处理BeanFactory.

ApplicationContext通常使用XML文件进行配置,该文件使用是简单的DTD.

如下:

<?xml version="1.0">
<!DOCTYOE beans PUBLIC"-//SPRING//DTD BEAN//EN" http://www.springframework.org/dtd/spring-beans/dtd>
<beans>
    <bean  id="cashRegister" class="org.example.CashRegisterImpl">  

         <property  name="priceMatrix" ref="priceMatrixBean"/>

    </bean>

    <bean id="priceMatrixBean" class="org.example.PriceMatrixImpl"/>

</beans>

BeanFactory对象可以有多种实例化的方式,虽然大多数情况下不用我们进行创建,但是还是可以有几种实例化的方式。

它可以InputStream作为参数实例化,也可以将org.springframework.core.io.Resource它实例化.有多种:ClassPathResource、 FileSystemResource、InputStreamResource、ServletContextResource、 UrlResource。可根据不同情况下使用.

Resource resource = new ClassPathResource("applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
HelloBean hello = (HelloBean) factory.getBean("helloBean");

and

ClassPathResource res = new ClassPathResource("applicationConxtext.xml");

XmlBeanFactory factory = new XmlBeanFactory(res);

or

InputStream is = new FileInputStream("application.xml");
XmlBeanFactory factory = new XmlBeanFactory(is);
 or

ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
new String[] {"applicationContext.xml", "applicationContext-two.xml"});

BeanFactory factory = (BeanFactory) appContext; 

 

Beans element can be defined in multiple bean, if you want to take out once all of the bean can be used org.springframework.beans.factory.ListableBeanFactory

for example:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id="helloBeanOfJustin" class="onlyfun.caterpillar.HelloBean">
        <property name="helloWord"><value>Hello!Justin!</value></property>
    </bean>
    <bean id="helloBeanOfcaterpillar" class="onlyfun.caterpillar.HelloBean">
        <property name="helloWord"><value>Hello!caterpillar!</value></property>
    </bean>
</beans>

Resource resource = new ClassPathResource("bean.xml");
        ListableBeanFactory factory = new XmlBeanFactory(resource);
       
        Map helloBeans = factory.getBeansOfType(HelloBean.class, false, false);

 If all of the "bean" is defined in an applicationContext the application is for a small can, but if the big applications, the required bean object is very large, and this time you can use: org.springframework.beans.factory. xml.XmlBeanDefinitionReader to carry out multiple bean.xml the load, it needs to org.springframework.beans.factory.support.BeanDefinitionRegistry object as a build-time parameter。

for example:

BeanDefinitionRegistry reg = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(reg);
   
// 载入bean   
reader.loadBeanDefinitions(new ClassPathResource("bean1.xml"));
reader.loadBeanDefinitions(new ClassPathResource("bean2.xml"));
....

// 获取Bean
BeanFactory bf = (BeanFactory) reg;
Object o = bf.getBean("helloBean");
....

 ApplicationContext继承了org.springframework.context.MessageResource对象。

可以使用getMessage()的各种方法来取得资源,可以实现国际化。MessageResource的一个子类,org.springframework.context.support.ResourceBundleMeaageSource,可以用它来获取资源文件。

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> 
<beans> 
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
        <property name="basename"> //资源文件的名字
            <value>messages</value
        </property> 
    </bean> 
</beans>
>//设定了文件前缀,可以是messages_en_US.properties,messages_zh_TW.properties、messages_*.properties等等。
messages_en_US.properties内容:
userlogin=User {0} login at {1}
message_zh_TW.properties內容:
userlogin=使用者 {0} 於 {1} 登入
native2ascii message_zh_TW.properties message_zh_TW.txt
import java.util.*; 
import org.springframework.context.*; 
import org.springframework.context.support.*; 

public class Test { 
    public static void main(String[] args) throws Exception { 
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); 

        Object[] arguments = new Object[] {"adminsun", Calendar.getInstance().getTime()}; 

        System.out.println(context.getMessage("userlogin", arguments, Locale.US)); 
        System.out.println(context.getMessage("userlogin", arguments, Locale.TAIWAN)); 
    } 
}
ClassPathXmlApplicationContext继承了ApplicationContext。读取xml获取bean对象。给getMessage()方法传递参数。
log4j:WARN No appenders could be found for logger (org.springframework.beans.factory.xml.XmlBeanDefinitionReader). 
log4j:WARN Please initialize the log4j system properly. 
User 良葛格 login at 10/28/04 12:52 PM 
使用者 adminsun 于 2004/10/28 下午 12:52 登入

 

分享到:
评论

相关推荐

    Spring框架配置文件

    Spring框架配置文件applicationContext.xml

    【框架源码篇 05】Spring源码篇-ApplicationContext

    【框架源码篇 05】Spring源码篇-ApplicationContext

    spring jar 包详解

    (1) spring-core.jar 这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工具类。 (2) spring-beans.jar 这个...

    25个经典的Spring面试问答

    请解释下Spring框架中的IoC BeanFactory和ApplicationContext有什么区别 Spring有几种配置方式 如何用基于XML配置的方式配置Spring 如何用基于Java配置的方式配置Spring 怎样用注解的方式配置Spring 请解释Spring ...

    Spring的IoC容器(《Spring揭秘》的精选版)

    针对Spring框架的主要功能以及开发者们遇到最多的问 题,首先介绍问题的相关背景,然后逐条进行深度剖析,最后通过分析来引入Spring框架可以提供的最佳解决方案。虽言Spring,却不局限于 Spring! 本书目录 目录 ...

    spring面试题25道图文并茂的spring面试题

    1. 什么是Spring框架?Spring框架有哪些主要模块? 2. 使用Spring框架有什么好处? 3. 什么是控制反转(IOC)?什么是依赖注入? 4. 请解释下Spring中的IOC? 5. BeanFactory和ApplicationContext有什么区别? 等。...

    Spring框架xml注解配置方式实例

    Spring框架xml注解配置方式实例,包括Maven配置文件pom.xml、Springxml配置文件ApplicationContext-1.xml,以及类源码

    Spring面试专题.pdf

    4、请解释下 Spring 框架中的 IoC? 5、BeanFactory 和 ApplicationContext 有什么区别? 6、Spring 有几种配置方式? 7、如何用基于 XML 配置的方式配置 Spring? 8、如何用基于 Java 配置的方式配置 Spring? 9、...

    Spring面试题.zip

    4、请解释下 Spring 框架中的 IoC? 5、BeanFactory 和 ApplicationContext 有什么区别? 6、Spring 有几种配置方式? 7、如何用基于 XML 配置的方式配置 Spring? 8、如何用基于 Java 配置的方式配置 Spring? 9、...

    开源框架 Spring Gossip

    简介 Spring Inversion of Control Dependency Injection &lt;br&gt; 核心容器 Spring 核心容器实作了 IoC,BeanFactory 与 ApplicationContext 的运用是了解 Spring 的重点所在。 管理 Bean...

    Spring 3.0+Struts2+Mybatis 3 + p6spy 平台框架

    这是自己整合的Spring 3.0+Struts2+Mybatis 3 + p6spy +ehcache的平台框架,内含一点示例代码,目前ehcache没有使用。直接编译后发布就能用 测试环境基于JDK1.6+Tomcat 6.0. 大家拿到后请根据实际情况修改 ...

    基于Spring框架开源ElasticSearch搜索

    本书以实例讲述如何在Spring框架之上搭建ElasticSearch开发,以及如何利用JPA建立、更新和删除索引,如何配置ElasticSearch Server的applicationContext等。

    spring第二天.pdf

    课程目标 1. 搞清楚BeanFactory家族的接口和类的作用 ...8. 可以自主完成阅读Spring框架中Bean实例创建流程的源码 9. 可以自主完成阅读Spring框架中依赖注入流程的源码 10. 可以确定aop流程的源码阅读入口

    spring课堂笔记.docx

    Spring 容器:介绍了 Spring 容器的不同类型,包括 BeanFactory 和 ApplicationContext,以及它们在管理对象生命周期和依赖注入方面的作用。 依赖注入:详细解释了依赖注入的原理和用法,以及如何配置和管理 Bean ...

    Spring面试题含答案.pdf

    26. Spring 框架中的单例 bean 是线程安全的吗? 27. 解释 Spring 框架中 bean 的生命周期 28. 哪些是重要的 bean 生命周期方法? 你能重载它们吗? 29. 什么是 Spring 的内部 bean? 30. 在 Spring 中如何注入一个...

    Spring MVC 框架应用实例

    /WEB-INF/database.xml /WEB-INF/applicationContext.xml org.springframework.web.context.ContextLoaderListener &lt;filter-name&gt;encodingFilter org.springframework.web.filter....

    Spring教程  主要内容:介绍Spring的历史,Spring的概论和它的体系结构,重点阐述它在J2EE中扮演的角色。

    Spring框架概述 3 Spring是什么? 3 Spring的历史 4 Spring的使命(Mission Statement) 4 Spring受到的批判 4 Spring包含的模块 5 总结 6 Spring的IoC容器 6 用户注册的例子 7 面向接口编程 8 (用户持久化类)重构...

    spring-web-5.3.6 jar包.rar

    这个jar文件包含Web应用开发时,用到Spring框架时所需的核心类, 包括自动载入WebApplicationContext特性的类、Struts与JSF集成类、文件上传的支持类、Filter类和大量工具辅助类。 spring的核心类,提供了核心HTTP...

    最全的Spring考题与答案

    一共近百道题与完整答案Spring框架的优点都有什么?简述你对 IoC(Inversion of Control)的理解。spring 中的BeanFactory与ApplicationContext的作用和区别?Spring如何实现资源管理?如何在web应用里面配置spring?...

    用Spring框架,jstl作UI实现数据库的CRUD操作

    用Spring框架,jstl作UI实现数据库的CRUD操作!难点在UI界面和控制层的关联,及控制层,业务层,dao等之间的关系,在applicationContext.xml中配置!

Global site tag (gtag.js) - Google Analytics