序文
昨日、Springフレームワークのソースコードを調べたところApplicationContext
、Springコンテナーのこのコンテナー部分には、実際には独自のコンテナー部分があることがわかりましたApplicationEventPublisher
。これはプッシュではありませんか?
ソースクラス
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.springframework.context;
import org.springframework.beans.factory.HierarchicalBeanFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.core.env.EnvironmentCapable;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.lang.Nullable;
public interface ApplicationContext extends EnvironmentCapable,
ListableBeanFactory, HierarchicalBeanFactory, MessageSource,
ApplicationEventPublisher, ResourcePatternResolver {
@Nullable
String getId();
String getApplicationName();
String getDisplayName();
long getStartupDate();
@Nullable
ApplicationContext getParent();
AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException;
}
構成して使用する
付属しているのでApplicationEventPublisher
、どうやって弾くの?
- データ受け入れクラスの
記述データ受け入れクラスは、主に、データ情報構造をカプセル化するためのカスタムデータ受け入れクラスをカプセル化するために使用されます。
package notify;
import org.springframework.context.ApplicationEvent;
public class EventNotify extends ApplicationEvent {
private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public EventNotify(Object source) {
super(source);
}
public EventNotify(Object source, String msg) {
super(source);
this.msg = msg;
}
}
- リスナークラス登録Bean
はApplicationEventPublisher
インターフェースを使用する必要があるため
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.springframework.context;
@FunctionalInterface
public interface ApplicationEventPublisher {
default void publishEvent(ApplicationEvent event) {
this.publishEvent((Object)event);
}
void publishEvent(Object var1);
}
publishEvent
方法、プッシュデータは、その後、関連する情報を受信するために使用することができる監視クラスが存在しなければなりません。
リスナークラスを作成し、Springコンテナに登録します。
package notify;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
public class NotifyListener implements ApplicationListener {
public void onApplicationEvent(ApplicationEvent env) {
System.out.println(env);
EventNotify eventNotify = (EventNotify) env;
System.out.println("--->"+eventNotify.getMsg());
}
}
spring-notify.xml:
<bean class="notify.NotifyListener" />
- テストクラスを作成する
package notify;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.concurrent.TimeUnit;
public class Test {
public static void main(String[] args) throws InterruptedException {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-notify.xml");
applicationContext.publishEvent(new EventNotify("xiangjiao","666666"));
TimeUnit.SECONDS.sleep(5);
}
}
テストを実行する
データがコンテナにプッシュされた後、コンテナ内の対応するリスナークラスがプッシュ情報を受信できることが発見されました。
予防
1.ApplicationContextコンテナのみが許可されます。
2.プッシュデータが大量にある可能性があり、監視クラスでは異なる方法で使用する必要があります。といった:
if(env instanceof EventNotify) {
·······
}