verysu 设计模式 设计模式 响应式编程 百度开发平台 codeforces leetcode usfca

导航

创建事件监听

创建事件


import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.springframework.context.ApplicationEvent;

@EqualsAndHashCode(callSuper = false)
@ToString
public class VOEvent<T> extends ApplicationEvent {

private Object pkValue;

private ChangeTypeEnum changeType;

private Class<T> vo;

private Object oldObject;

public Object getPkValue() {
return pkValue;
}

public ChangeTypeEnum getChangeType() {
return changeType;
}

public Class<T> getVo() {
return vo;
}

public void setVo(Class<T> vo) {
this.vo = vo;
}

/**
* Create a new ApplicationEvent.
* @param source the object on which the event initially occurred (never {@code null})
*/
public VOEvent(
Object source, Object pkValue, ChangeTypeEnum changeType, Class<T> vo, Object oldObject) {
super(source);
this.pkValue = pkValue;
this.changeType = changeType;
this.vo = vo;
this.oldObject = oldObject;
}

public enum ChangeTypeEnum {
SAVE,
REMOVE;
}
}
发布事件管理器
import dac.common.pub.MessageException;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class EventManager implements ApplicationContextAware {

private static ApplicationContext applicationContext;

private static final transient Map<Class, VOEventPolicy.Type> m_event_policy =
new ConcurrentHashMap<>();
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
EventManager.applicationContext = applicationContext;
}

public static VOEventPolicy.Type getEventPolicy(Class clz) {
if (clz == null) {
throw new MessageException("clz is null.");
}
VOEventPolicy.Type policy = m_event_policy.get(clz);
if (policy == null) {
if (clz.isAnnotationPresent(VOEventPolicy.class)) {
policy = ((VOEventPolicy) clz.getAnnotation(VOEventPolicy.class)).value();
} else {
policy = VOEventPolicy.Type.CLOSE;
}
m_event_policy.put(clz, policy);
}
return policy;
}
private static boolean isPublishEvent(ApplicationEvent event) {
if (event == null) {
return false;
}
if (event instanceof VOEvent) {
return VOEventPolicy.Type.CLOSE != getEventPolicy((Class) ((VOEvent<?>) event).getVo());
}
return false;
}

public static ApplicationContext getSpringContext() {
return applicationContext;
}

public static void publishEvent(ApplicationEvent event) {
if (!isPublishEvent(event)) {
return;
}
applicationContext.publishEvent(event);
}
}
配置事件策略
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.FIELD}) // ElementType.METHOD,
@Inherited
@Documented
public @interface VOEventPolicy {
public enum Type {
/** 不开启 */
CLOSE,
/** 开启 */
OPEN
};
public Type value() default Type.CLOSE;
}
//事件监听处理器
import dac.common.event.VOEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class ChangeEventListener {
@Async
@EventListener
public void onApplicationEvent(VOEvent<Class> event) {
//监听处理逻辑
}
}

多域名共享cookies
import org.apache.tomcat.util.http.LegacyCookieProcessor;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

 * 参考文档:https://docs.spring.io/spring-boot/docs/2.0.3.RELEASE/reference/htmlsingle/#howto-use-tomcat-legacycookieprocessor
 */
@Configuration
public class CookieConfig {

  @Bean
  public WebServerFactoryCustomizer<TomcatServletWebServerFactory> cookieProcessorCustomizer() {
    return (factory) ->
        factory.addContextCustomizers(
            (context) -> context.setCookieProcessor(new LegacyCookieProcessor()));
  }
}

Spring中的对外接口实现

extends SimpleApplicationEventMulticaster广播
implements MetaOjectHandler 入库自动设置属性

posted on 2021-12-29 16:13  泳之  阅读(46)  评论(0编辑  收藏  举报

我是谁? 回答错误