我使用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
实例之后和所有其他bean(尤其是EdgeTestDatasourceListener
)之前创建DataSourceInitializer
实例。
否则,您无需在BeanFactoryPostProcessor.postProcessBeanFactory
中编写任何代码。当Spring启动时,它将检测到此BeanFactoryPostProcessor
并调用其postProcessBeanFactory
,因此您无需实例化它。
要使BeanFactoryPostProcessor
依赖于EdgeTestDatasourceListener
,可以在实现@DependsOn("classname")
的过程中使用注释BeanFactoryPostProcessor
@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 {
}
}