小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

SpringFramework(3)

 wtf_soft 2005-08-26
 

2、Application Context

1)什么是Application Context

l         聚集能夠被所有組件使用的應(yīng)用程序信息

l         Bean定義的位置

l         加載多個(gè)contexts

l         層次contexts

l         il8n,消息資源

l         資源訪問

l         事件傳播

2ApplicationContext

l         擴(kuò)展BeanFactory

l         可以有一個(gè)父context

l         實(shí)現(xiàn):

         FileSystemXmlApplicationContext

         ClassPathXmlApplicationContext

         XmlWebApplicationContext

l         例子:

ApplicationContext ctx = new FileSystemXmlApplicationContext("c:/beans.xml");
ExampleBean eb = (ExampleBean)ctx.getBean("exampleBean");

l         ApplicationContext可以讀取多個(gè)文件

String[] ctxs = new String[]{"ctx1.xml", "ctx2.xml"};
ApplicationContext ctx = new FileSystemXmlApplicationContext(ctxs);

3)層次contexts

l         如果一個(gè)Beancontext中沒有找到,就會(huì)到父context中去找

l         創(chuàng)建一個(gè)層次contexts

ApplicationContext parent = new ClassPathXmlApplicationContext("ctx1.xml");
ApplicationContext ctx = new FileSystemXmlApplicationContext("ctx2.xml", parent);

4)資源

l         ApplicationContext處理資源位置

l         ApplicationContext方法:

Resource getResource(String location)

         URL全路經(jīng),如file:c:/test.dat

         相對(duì)文件路徑,如WEB-INF/test.dat

         classpathURL,如classpath:test.dat

interface Resource {
boolean exists();
boolean isOpen();
String getDescription();
File getFile() throws IOException;
InputStream getInputStream() throws IOException;
}

5)資源編輯器

l         內(nèi)建PropertyEditor

l         能夠在Bean定義中配置資源屬性

l         例子:

<property name="resourceProperty">
<value>example/image.gif</value>
</property>

6il8n

l         國(guó)際化應(yīng)用程序消息

l         ApplicationContext方法:

String getMessage(String code, Object[] args, String default, Locale loc)

代表一個(gè)messageSource Bean。

l         ApplicationContext搜索messageSource Bean(必須實(shí)現(xiàn)MessageSource接口)

l         例子:在classpath中定義兩個(gè)資源束messageserrors

<bean id="messageSource" class="...ResourceBundleMessageSource">
<property name="basenames">
<value>messages,errors</value>
</property>
</bean>

classpath中搜索:

messages_pt_BR.properties       errors_pt_BR.properties

messages_pt.properties       errors_pt.properties

messages.properties              errors.properties

7)事件

l         事件傳播

         ApplicationContext處理事件,調(diào)用偵聽器

         Beans必須實(shí)現(xiàn)ApplicationListener接口來(lái)接收事件

         應(yīng)用程序可以擴(kuò)展ApplicationEvent

         內(nèi)建事件:

2        ContextRefreshedEvent

2        ContextClosedEvent

2        RequestHandledEvent

l         偵聽事件

public class MyListenerBean implements ApplicationListener {
public void onApplicationEvent(ApplicationEvent e) {
// process event
}
}

l         發(fā)送事件

public class ExampleBean implements ApplicationContextAware {
ApplicationContext ctx;
public void setApplicationContext(ApplicationContext ctx)
throws BeansException {
this.ctx = ctx;
}
public void sendEvent() {
ctx.publishEvent(new MyApplicationEvent(this));
}
}

8BeanFactoryPostProcessor

l         可以用來(lái)在它內(nèi)部配置BeanFactoryBeans;應(yīng)用程序contexts在它們的Bean定義中自動(dòng)檢測(cè)BeanFactoryPostProcessorBeans,在其它Beans創(chuàng)建之前應(yīng)用它們

l         post processor bean必須實(shí)現(xiàn)BeanFactoryPostProcessor接口

l         例子:增加定制編輯器到context

public class MyPostProcessor implements BeanFactoryPostProcessor {
void postProcessBeanFactory(ConfigurableListableBeanFactory bf) {
DateFormat fmt = new SimpleDateFormat("d/M/yyyy");
CustomDateEditor dateEditor = new CustomDateEditor(fmt, false);
bf.registerCustomEditor(java.util.Date.class, dateEditor);
}
}
 
<bean id="myPostProcessor" class="eg.MyPostProcessor"/>

9CustomEditorConfigurer

l         BeanFactoryPostProcessor實(shí)現(xiàn)允許方便的注冊(cè)定制屬性編輯器

<bean id="customEditorConfigurer" class="...CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class="...CustomDateEditor">
<constructor-arg index="0">
<bean class="java.text.SimpleDateFormat">
<constructor-arg><value>d/M/yyyy</value></constructor-arg>
</bean>
</constructor-arg>
<constructor-arg index="1"><value>false</value></constructor-arg>
</bean>
</entry>
</map>
</property>
</bean>

10)典型的應(yīng)用程序contexts

l         應(yīng)用程序contexts通常與J2EE服務(wù)器定義的一個(gè)范圍相聯(lián)系

         Web應(yīng)用程序(javax.servlet.ServletContext):Sring提供通過(guò)偵聽器或Servlet實(shí)例化該context的能力

         Servlet:每個(gè)Servlet能夠擁有自己的應(yīng)用程序context,來(lái)源于Web應(yīng)用程序context

         EJBs:從EJB jar文件中的XML文檔中加載

l         不需要使用Singleton來(lái)引導(dǎo)Bean Factory


    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多