gpt4 book ai didi

java - 为什么在应用程序启动期间没有调用监听 DataSourceInitializedEvent 的自定义 ApplicationListener?

转载 作者:行者123 更新时间:2023-11-30 07:40:51 26 4
gpt4 key购买 nike

我使用 Spring Boot 编写了一个应用程序。我添加了一个监听 DataSourceInitializedEvent 的 ApplicationListener,但在应用程序启动期间尚未调用我的监听器。以下是我的 DataSourceInitializedEvent 监听器:

package com.athena.edge;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.autoconfigure.jdbc.DataSourceInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
* Created by minggaoxi on 1/11/16.
*/
@Component
public class EdgeTestDatasourceListener implements ApplicationListener<DataSourceInitializedEvent> {
private static Log logger = LogFactory.getLog(EdgeTestDatasourceListener.class);

@Override
public void onApplicationEvent(DataSourceInitializedEvent event) {
logger.debug("EdgeTestDatasourceListener called");
}
}

我的主要应用程序如下所示:

package com.athena.edge;

import io.undertow.servlet.api.DeploymentInfo;
import org.h2.tools.Server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.undertow.UndertowDeploymentInfoCustomizer;
import org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainerFactory;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Profile;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.util.Date;

@SpringBootApplication
@EnableRedisHttpSession
@EnableZuulProxy
@RestController
public class EdgeApplication {

public static void main(String[] args) {
SpringApplication.run(EdgeApplication.class, args);
}

@RequestMapping(value = "/", method = RequestMethod.GET)
public String home() {
return "Hello World from Edge-Server at " + new Date();
}

// Config H2 tcp server when we're in development stage, in order to
// enable inspecting the in-memory database content from outside.
@Bean(name = "org.h2.tools.Server", initMethod = "start", destroyMethod = "stop")
@Profile("dev")
public Server h2TcpServer() throws SQLException {
return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "9980");
}

// Configuration related to Zuul error handling in Undertow.
// Refer to http://blog.jmnarloch.io/2015/09/16/spring-cloud-zuul-error-handling/
@Bean
public UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory() {
UndertowEmbeddedServletContainerFactory factory = new UndertowEmbeddedServletContainerFactory();
factory.addDeploymentInfoCustomizers(new UndertowDeploymentInfoCustomizer() {
@Override
public void customize(DeploymentInfo deploymentInfo) {
deploymentInfo.setAllowNonStandardWrappers(true);
}
});
return factory;
}
}

我不知道为什么当“DataSourceInitializedEvent”被触发时我的监听器没有被调用,但是我注意到 Spring 内部的“DataSourceInitializer”被调用了。

最佳答案

该问题是由于在注册自定义监听器之前触发该事件而引起的。这是因为 DataSourceInitializer 肯定首先被初始化。

解决方案是实现 BeanFactoryPostProcessor 并使其依赖于 EdgeTestDatasourceListener。这将保证 BeanFactoryPostProcessor 的实例将在 EdgeTestDatasourceListener 实例之后、所有其他 bean(尤其是 DataSourceInitializer)之前创建。

否则,您不需要在BeanFactoryPostProcessor.postProcessBeanFactory中编写任何代码。当 Spring 启动时,它会检测到这个 BeanFactoryPostProcessor 并调用它的 postProcessBeanFactory,因此您不需要实例化它。

要使 BeanFactoryPostProcessor 依赖于 EdgeTestDatasourceListener,您可以在 BeanFactoryPostProcessor 的实现上使用注释 @DependsOn("classname")

 @Component
@DependsOn("edgeTestDatasourceListener")
public class DataSourceListenerRegistrarForcer implements BeanFactoryPostProcessor, PriorityOrdered {
@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE;
}
@Override
public void postProcessBeanFactory(
ConfigurableListableBeanFactory beanFactory) throws BeansException {
}
}

关于java - 为什么在应用程序启动期间没有调用监听 DataSourceInitializedEvent 的自定义 ApplicationListener?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34721747/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com