Springframework (3)

zhaozj2021-02-16  96

2, Application Context

(1) What is Application Context

l Concentrated application information that can be used by all components

l bean defined position

l Load multiple contexts

l level contexts

l IL8N, message resources

l Resource access

l Event communication

(2) ApplicationContext

l extended BeanFactory

l can have a parent Context

l Implementation:

FileSystemXmlapplicationContext

ClassPathXMLApplicationContext

XMLWebApplicationContext

l example:

ApplicationContext CTX = New FileSystemXmlapplicationContext ("c: /beans.xml");

EXAMPLEBEAN EB = (Examplebean) CTX.getBean ("ExampleBean");

l ApplicationContext can read multiple files

String [] ctxs = new string [] {"ctx1.xml", "ctx2.xml"};

ApplicationContext CTX = New FileSystemXmlapplicationContext (CTXS);

(3) Level Contexts

l If a bean is not found in Context, it will go to the parent CONTEXT to find

l Create a hierarchical contexts

ApplicationContext Parent = New ClassPathXMLApplicationContext ("CTX1.xml");

ApplicationContext CTX = New FileSystemXmlapplicationContext ("CTX2.xml", Parent);

(4) Resources

l ApplicationContext processing resource location

l ApplicationContext method:

Resource getResource (String location)

• URL full circuit, such as File: C: /Test.dat

? Relative file path, such as web-inf / Test.dat

ClassPath Pseudo URL, such as ClassPath: Test.dat

Interface resource {

Boolean exissrs ();

Boolean isopen ();

String getdescription ();

File getFile () throws oException;

InputStream GetInputStream () THROWS IOException;

}

(5) Resource editor

l Built-in PropertyEditor

l Configuring resource properties in Bean definitions

l example:

eXample / image.gif

(6) IL8N

l International application news

l ApplicationContext method:

String getMessage (String code, Object [] args, string default, locale LOC

Represents a Messagesource Bean.

l ApplicationContext Search for Messagesource Bean (Must implement Messagesource interface)

l Example: Define two resource beam messages and errors in ClassPath

Messages, Errors

Search in ClassPath:

Messages_PT_br.properties ErrorS_PT_Br.Properties

Messages_pt.properties Errors_pt.properties

Messages.properties ERRORS.PROPERTIES

(7) event

l Event communication

• ApplicationContext handling events, call listeners

Beans must implement the ApplicationListener interface to receive events

? Application can extend ApplicationEvent

? Built-in event:

2 contextrefreeshedEvent

2 ContextCloseDevent

2 RequesthandEvent

l Listening event

Public class mylistenerbean imports applicationListener {

Public void onapplicationEvent (ApplicationEvent E) {

// process evenet

}

}

l Send event

Public Class ExampleBeaMplements ApplicationContextaWare {

ApplicationContext CTX;

Public void setApplicationContext (ApplicationContext CTX)

Throws beansexception {

THIS.CTX = CTX;

}

Public void sendevent () {

CTX.Publishevent (New MyApplicationEvent (this));

}

}

(8) BeanFactoryPostProcessor

l can be used to configure BeanFactory or Beans in it; application context automatically detect BeanFactoryPostProcessor's beans in their bean definition, applying them before other Beans created

l POST Processor Bean must implement the BeanFactoryPostProcessor interface L example: add a custom editor to Context

Public class mypostProcessor imports 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);

}

}

(9) CustomeditorConfigurer

l BeanFactoryPostProcessor implements a convenient registration custom property editor

d / m / yyyy

false

(10) Typical application contexts

l Application Contexts is usually linked to a range defined by the J2EE server.

• Javax.Servlet.ServletContext: SRING provides the ability to instantiate the Context by listener or servlet

? Servlet: Each servlet can have its own application Context, from the web application context

? EJBS: Load from the XML document in the EJB JAR file

l Don't use Singleton to boot bean factory

转载请注明原文地址:https://www.9cbs.com/read-11139.html

New Post(0)