Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring:从InitializingBean';s AfterPropertieSet方法不起作用_Spring_Events - Fatal编程技术网

Spring:从InitializingBean';s AfterPropertieSet方法不起作用

Spring:从InitializingBean';s AfterPropertieSet方法不起作用,spring,events,Spring,Events,最近我发现,当我从org.springframework.beans.factory.InitializingBean.afterPropertiesSet()发布事件时,它无法发布该事件 但是,如果我从@Controller或任何其他类调用它,就会调用这个非常相同的事件触发器(这两个地方的事件调用机制保持不变) 我在InitBean中发布事件之后放置了一个print语句(“触发器完成”),并且该语句也被成功打印出来 如果你对这种行为有任何想法,请告诉我 非常感谢 //Sample code f

最近我发现,当我从org.springframework.beans.factory.InitializingBean.afterPropertiesSet()发布事件时,它无法发布该事件

但是,如果我从@Controller或任何其他类调用它,就会调用这个非常相同的事件触发器(这两个地方的事件调用机制保持不变)

我在InitBean中发布事件之后放置了一个print语句(“触发器完成”),并且该语句也被成功打印出来

如果你对这种行为有任何想法,请告诉我

非常感谢

//Sample code for InitializingBean:
@Component 
public class InitBean implements InitializingBean {

    private final ApplicationEventPublisher publisher;

    public InitBean(ApplicationEventPublisher publisher) {
        this.publisher = publisher;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        this.publisher.publishEvent(new TriggerEvent());
        System.out.println("Trigger done");
    }
}

// Sample code for trigger event:
public class TriggerEvent extends ApplicationEvent {
    public TriggerEvent() {
        super("source");
    }
}

// Sample code for listener:
@Component 
public class TriggerListener {
    @EventListener(TriggerEvent.class)
    public void trigger(TriggerEvent triggerEvent) {
        System.out.println("Trigger event has come");
    }
}


如果不进行测试,我认为问题在于
afterPropertiesSet
在SpringBean生命周期中还为时过早

试着晚一点启动事件。 而是在
@PostConstruct
中,一个init方法,或者在捕获应用程序上下文刷新事件时

@PostConstruct

@Component 
public class InitBean {
...
    @PostConstruct
    public void fire() throws Exception {
        this.publisher.publishEvent(new TriggerEvent());
        System.out.println("Trigger done");
    }
}
初始方法:

@Configuration
public class AppConfig {
    @Bean(initMethod="fire")
    public InitBean initBean (ApplicationEventPublisher publisher) {
        return new InitBean (publisher);
    }
}

public class InitBean {
...
    @PostConstruct
    public void fire() throws Exception {
        this.publisher.publishEvent(new TriggerEvent());
        System.out.println("Trigger done");
    }
}
上下文刷新方式

@Component 
public class InitBean {
...
    @EventListener
    public void onApplicationEvent(ContextRefreshedEvent event) {
        fire();
    }

    public void fire() throws Exception {
        this.publisher.publishEvent(new TriggerEvent());
        System.out.println("Trigger done");
    }
}

我不知道前两种方法是否解决了可疑问题,但最后一种方法应该有效。

谢谢Ralph。我已经尝试了最后一个第一,因为这看起来是一个确定的方式去,它是工作良好。