Getting started 15 - aware related interface
Swings provide some Aware-related interfaces, such as BeanFactoryaware, ApplicationContextaware, ResourceEraWare, ServletContextaWare, etc., BEAN, for these Aware interfaces, can get some corresponding resources after being initially, such as the BeanFactoryaWare's bean after initial, The Spring container will be injected into the instance of BeanFactory, and the bean of ApplicationContextaWare will be injected into an instance of ApplicationContext after the bean is initially. Bean's instance of BeanFactory, ApplicationContextaWare is what is the purpose of the general purpose is to obtain some file resources, related message resources, or the mechanisms provided by those injected, such as ApplicationContextaWare provides a public () method, which can support Event propagation mechanism based on OBSERVER mode. The definition of the ApplicationContextaWare interface is as follows:
ApplicationContextaware.java
Public interface applibilityContextaWare {
Void SetApplicationContext (ApplicationContext Context);
}
We demonstrate how this side is injecting ApplicationContext through the actual ApplicationContextaWare, first of all our Hellobean as follows:
Hellobean.java
Package online.
Import org.springframework.context. *;
Public class hellobean implements applicationContextaWare {
Private ApplicationContext ApplicationContext;
Private string helloword = "Hello! World!";
Public void setApplicationContext (ApplicationContext context) {
THIS.ApplicationContext = context;
}
Public void sethelloword (string helloword) {
THIS.HELLOWORD = HelloWord;
}
Public string getHelloword () {
ApplicationContext.publishevent (
New PropertygettedEvent ("[" HelloWord "] is getted"));
Return HelloWord;
}
}
ApplicationContext is injected by the Spring container. The PublisHevent () method requires an object that inherited ApplicationEvent, and our PropertygettedEvent inherits ApplicationEvent, as follows:
PropertygettedEvent.java
Package online.
Import org.springframework.context. *;
Public class protygettedevent extends applicationEvent {
Public PropertygettedEvent (Object Source) {Super (Source);
}
}
When ApplicationContext performs public (), it will automatically find objects of the actual ApplicationListener interface and inform them that the corresponding event is, and we have the toptegettedListener as follows:
PrppertygettedListener.java
Package online.
Import org.springframework.context. *;
Public Class PropertygettedListener Implements ApplicationListener {
Public void onapplicationEvent (ApplicationEvent Event) {
System.out.println (Event.getsource (). TOSTRING ());
}
}
Listener must be instantiated, which we can define in the bean definition file:
XML Version = "1.0" encoding = "UTF-8"?>
bean>
beans>
We write a test program to measure the operation of the event propagation:
Test.java
Package online.
Import org.springframework.context. *;
Import org.springframework.context.support. *;
Public class test {
Public static void main (String [] args) {
ApplicationContext context = new classpathXMLApplicationContext ("bean.xml");
Hellobean Hello = (Hellobean) Context.getBean ("Hellobean");
System.out.println (Hello.getHelloword ());
}
}
The execution result will be as follows:
Log4j: warn no appenders Could Be Found for logger
(Org.SpringFramework.beans.Factory.xml.xmlbeandefinitionReader).
Log4J: Warn Please Initialize The log4j system prot in.
Org.springframework.context.support.classpathxmlapplicationContext: DisplayName = [org.springframework.context.support.classpathXMLApplicationContext;
Hashcode = 33219526]; startup date = [fri Oct 29
10:56:35 CST
2004];
Root of ApplicationContext Hierarchy
[Hello! Justin!] Is getted
Hello! Justin!
The above is to see the actual event to see the actual Aware interface, the action, the same, you can also actually actually, the resourceeloaderaWare interface:
ResourceLoaderaware.java
Public interface resourceloaderaware {
Void setResourceLoader (ResourceLoader Loader);
}
The BEAN of the actual ResourceLoader can get the ResourceLoader instance, so you can use its getResource () method, which is quite useful for Beans that must be accessed. Basically, although Spring provides these Aware-related interfaces, if these interfaces have implemented these interfaces, even if they have spring, from another perspective, although you can implement these interfaces directly on the bean, you also Dependent injection can be done through setter, for example:
Hellobean.java
Package online.
Import org.springframework.context. *;
Public class hellobean {
Private ApplicationContext ApplicationContext;
Private string helloword = "Hello! World!";
Public void setApplicationContext (ApplicationContext context) {
THIS.ApplicationContext = context;
}
Public void sethelloword (string helloword) {
THIS.HELLOWORD = HelloWord;
}
Public string getHelloword () {
ApplicationContext.publishevent (New PropertygettedEvent ("[" HelloWord "] is getted"));
Return HelloWord;
}
}
Note this time we don't have active ApplicationContextaWare, we can inject ApplicationContext instances in the program:
ApplicationContext context = new classpathXMLApplicationContext ("bean.xml");
Hellobean Hello = (Hellobean) Context.getBean ("Hellobean");
Hello.SetApplicationContext (CONTEXT);
System.out.println (Hello.getHelloword ());