Spring - Spring Data REST - Prevent The Methods in Event Handlers Be Triggered Twice

自定义发布事件机制避免重复触发
本文介绍如何通过创建自定义注解配置上下文来防止应用事件在不同层次触发多次,具体步骤包括重写发布事件方法、移除默认多播事件调用,并设置自定义上下文到DispatcherServlet。
<span style="background-color: rgb(255, 255, 255);">To prevent the methods be triggered twice, we need to overwrite the </span><strong style="background-color: rgb(255, 255, 255);">publishEvent(ApplicationEvent event)</strong><span style="background-color: rgb(255, 255, 255);"> method, then remove the code below</span>
<span style="white-space:pre">	</span>this.getApplicationEventMulticaster().multicastEvent(event);
        if(this.parent != null) {
            this.parent.publishEvent(event);
        }
So, it need to create a customized AnnotationConfuigWebApplicationContext:
package com.siemens.ct.its.iam.deploy.configuration;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.util.Assert;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by z002x71p on 2015/9/23.
 */
public class MyAnnotationConfigWebApplicationContext extends AnnotationConfigWebApplicationContext {

    public void publishEvent(ApplicationEvent event) {
        Assert.notNull(event, "Event must not be null");
        if(this.logger.isTraceEnabled()) {
            this.logger.trace("Publishing event in " + this.getDisplayName() + ": " + event);
        }

        this.getApplicationEventMulticaster().multicastEvent(event);
    }

    ApplicationEventMulticaster getApplicationEventMulticaster() throws IllegalStateException {
        for(Class<?> clazz = this.getClass() ; clazz != Object.class ; clazz = clazz.getSuperclass()) {
            String className = clazz.getName();
            System.out.println(className);
            if(className.equals("org.springframework.context.support.AbstractApplicationContext")){
                try {
                    Method method = clazz.getDeclaredMethod("getApplicationEventMulticaster") ;
                    method.setAccessible(true);
                    return (ApplicationEventMulticaster) method.invoke(this);
                }catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e){
                    throw new RuntimeException(e);
                }
            }
        }
        return null;
    }
}
And set it into the DispacherServlet.

### Spring Boot Starter Dependency Error Analysis In a Spring Boot project, encountering issues such as dependencies being marked in red within the IDE often indicates unresolved references or configuration problems. This could stem from multiple causes including incorrect Maven/Gradle configurations, network restrictions preventing artifact downloads, or even CLIR (Calling Line Identification Restriction)[^1], which might block access to external repositories. The following factors should be considered when addressing this issue: #### Incorrect Repository Configuration If the `pom.xml` file does not correctly specify the repository URLs where `spring-boot-starter-*` artifacts reside, it can lead to unresolved dependencies. Ensure that your `pom.xml` includes the central Maven repository and any additional required ones[^2]. ```xml <repositories> <repository> <id>central</id> <url>https://repo.maven.apache.org/maven2</url> </repository> </repositories> ``` #### Network Restrictions Network limitations like firewalls or proxy settings may prevent downloading necessary libraries. Verify whether there are active proxies configured properly both at system level and inside build tools' settings files (`settings.xml` for Maven). For example, configure proxy details under user-specific Maven settings: ```xml <proxies> <proxy> <active>true</active> <protocol>http</protocol> <host>your-proxy-host</host> <port>8080</port> <username>optional-user</username> <password>optional-password</password> </proxy> </proxies> ``` #### Cache Issues with Build Tools Sometimes cached metadata of previous failed attempts corrupts subsequent builds leading to errors despite correct setup elsewhere. Clearing local caches manually through commands provided by respective toolsets helps resolve these situations quickly. To clean up maven cache use command below: ```bash mvn dependency:purge-local-repository ``` Additionally restarting IDE after ensuring all above points addressed usually clears UI glitches showing false positives about missing jars etc., thus resolving visual representation discrepancies shown as 'red-underlined'.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值