docs/Spring全家桶/Spring/Spring中的事件处理机制.md
2022-05-16 15:29
Ѿ½ Spring ĺ ApplicationContext beans ڡ beans ʱApplicationContext ijЩ͵¼磬ʱContextStartedEvent ֹͣʱContextStoppedEvent
ͨ ApplicationEvent ApplicationListener ӿṩ ApplicationContext д¼һ bean ʵ ApplicationListenerôÿ ApplicationEvent ApplicationContext ϣǸ bean ᱻ֪ͨ
Spring ṩµı¼
| Spring ¼ & | |
|---|---|
| 1 | ContextRefreshedEventApplicationContext ʼˢʱ¼Ҳ ConfigurableApplicationContext ӿʹ refresh() |
| 2 | ContextStartedEventʹ ConfigurableApplicationContext ӿе start() ApplicationContext ʱ¼Եݿ⣬ڽܵ¼κֹͣӦó |
| 3 | ContextStoppedEventʹ ConfigurableApplicationContext ӿе stop() ֹͣ ApplicationContext ʱ¼ڽܵ¼ҪĹ |
| 4 | ContextClosedEventʹ ConfigurableApplicationContext ӿе close() ر ApplicationContext ʱ¼һѹرյĵĩˣܱˢ» |
| 5 | RequestHandledEventһ web-specific ¼ bean HTTP Ѿ |
Spring ¼ǵ̵߳ģһ¼ֱҳеĽߵõĸϢý̱̽ˣ¼ʹãӦóʱӦע⡣
Ϊ˼¼һ bean Ӧʵֻһ onApplicationEvent() ApplicationListener ӿڡˣдһ¼δģԼοôִлijЩ¼
ǡλʹ Eclipse IDEȻIJһ Spring Ӧó
| 1 | һΪ SpringExample ĿڴĿ src ļдһ com.tutorialspoint |
| 2 | ʹ Add External JARs ѡ Spring ⣬ͼ Spring Hello World Example ½ڡ |
| 3 | com.tutorialspoint д Java HelloWorldCStartEventHandlerCStopEventHandler MainApp |
| 4 | src ļд Bean ļ Beans.xml |
| 5 | һǴ Java ļ Bean ļݣӦóʾ |
HelloWorld.java ļݣ
package com.tutorialspoint;
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}
CStartEventHandler.java ļݣ
package com.tutorialspoint;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStartedEvent;
public class CStartEventHandler
implements ApplicationListener<ContextStartedEvent>{
public void onApplicationEvent(ContextStartedEvent event) {
System.out.println("ContextStartedEvent Received");
}
}
CStopEventHandler.java ļݣ
package com.tutorialspoint;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStoppedEvent;
public class CStopEventHandler
implements ApplicationListener<ContextStoppedEvent>{
public void onApplicationEvent(ContextStoppedEvent event) {
System.out.println("ContextStoppedEvent Received");
}
}
MainApp.java ļݣ
package com.tutorialspoint;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ConfigurableApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
// Let us raise a start event.
context.start();
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
// Let us raise a stop event.
context.stop();
}
}
ļ Beans.xml ļ
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="helloWorld" class="com.tutorialspoint.HelloWorld">
<property name="message" value="Hello World!"/>
</bean>
<bean id="cStartEventHandler"
class="com.tutorialspoint.CStartEventHandler"/>
<bean id="cStopEventHandler"
class="com.tutorialspoint.CStopEventHandler"/>
</beans>
һ˴Դ bean ļǾͿиӦóӦóһжϢ
ContextStartedEvent Received
Your Message : Hello World!
ContextStoppedEvent Received
дͷԼԶ¼ಽ衣һ¸˵дʹԶ Spring ¼
| 1 | һΪ SpringExample ĿڴĿ src ļдһ com.tutorialspoint |
| 2 | ʹ Add External JARs ѡ Spring ⣬ͼ Spring Hello World Example ½ڡ |
| 3 | ͨչ ApplicationEvent,һ¼ CustomEvent붨һĬϵĹ캯Ӧô ApplicationEvent м̳еĹ캯 |
| 4 | һ¼࣬Դκзٶ EventClassPublisher ʵ ApplicationEventPublisherAware㻹Ҫ XML ļΪһ bean֮ʶ bean Ϊ¼ߣΪʵ ApplicationEventPublisherAware ӿڡ |
| 5 | ¼һбٶ EventClassHandler ʵ ApplicationListener ӿڣʵԶ¼ onApplicationEvent |
| 6 | src ļд bean ļ Beans.xml MainApp ࣬Ϊһ Spring ӦóС |
| 7 | һǴ Java ļ Bean ļݣӦóʾ |
CustomEvent.java ļݣ
package com.tutorialspoint;
import org.springframework.context.ApplicationEvent;
public class CustomEvent extends ApplicationEvent{
public CustomEvent(Object source) {
super(source);
}
public String toString(){
return "My Custom Event";
}
}
CustomEventPublisher.java ļݣ
package com.tutorialspoint;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
public class CustomEventPublisher
implements ApplicationEventPublisherAware {
private ApplicationEventPublisher publisher;
public void setApplicationEventPublisher
(ApplicationEventPublisher publisher){
this.publisher = publisher;
}
public void publish() {
CustomEvent ce = new CustomEvent(this);
publisher.publishEvent(ce);
}
}
CustomEventHandler.java ļݣ
package com.tutorialspoint;
import org.springframework.context.ApplicationListener;
public class CustomEventHandler
implements ApplicationListener<CustomEvent>{
public void onApplicationEvent(CustomEvent event) {
System.out.println(event.toString());
}
}
MainApp.java ļݣ
package com.tutorialspoint;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ConfigurableApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
CustomEventPublisher cvp =
(CustomEventPublisher) context.getBean("customEventPublisher");
cvp.publish();
cvp.publish();
}
}
ļ Beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="customEventHandler"
class="com.tutorialspoint.CustomEventHandler"/>
<bean id="customEventPublisher"
class="com.tutorialspoint.CustomEventPublisher"/>
</beans>
һ˴Դ bean ļǾͿиӦóӦóһжϢ
My Custom Event
My Custom Event
https://www.w3cschool.cn/wkspring https://www.runoob.com/w3cnote/basic-knowledge-summary-of-spring.html http://codepub.cn/2015/06/21/Basic-knowledge-summary-of-Spring https://dunwu.github.io/spring-tutorial https://mszlu.com/java/spring http://c.biancheng.net/spring/aop-module.html