读了外国的本书上才知道,"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 登入