我使用spring boot编写了一个应用程序。我添加了一个ApplicationListener监听DataSourceInitializedEvent,但我的监听器在应用程序启动时没有被调用。以下是我的DataSourceInitializedEvent listenser:
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&#39;被叫了。
答案 0 :(得分:1)
问题是由于在注册自定义侦听器之前触发了事件。这是因为DataSourceInitializer
首先被初始化。
解决方案是实施BeanFactoryPostProcessor
并使其依赖EdgeTestDatasourceListener
。这将保证BeanFactoryPostProcessor
的实例将在EdgeTestDatasourceListener
之后和所有其他bean之前创建(尤其是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 {
}
}