我有一個Spring Boot 1.5.4應用程序,它在啓動時註冊了一個日誌appender,這要感謝一個監聽器,它適用於這裏解釋的解決方案register custom log appender in spring boot starter。監聽器是我編寫的初學者的一部分,我的應用程序使用@EnableAutoConfiguration按預期工作。監聽器沒有收到ApplicationPreparedEvent
我有2個其他應用程序使用相同的Spring Boot版本,我想使用相同的啓動器。我相信他們或多或少具有與工作應用程序相同的配置,但方法永遠不會被調用,因此我的日誌appender未註冊。以下是我的配置+監聽器類。
我已經在supportsEventType方法中放置了一個斷點,並且在那裏傳遞了所有的事件,但沒有一個像我的第一個應用程序那樣是ApplicationPreparedEvent的類型。
@Configuration
@EnableConfigurationProperties(MetricProperties.class)
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class MetricsAutoConfiguration implements GenericApplicationListener {
private final MetricProperties metricProperties;
private boolean addedCustomAppender = false;
public MetricsAutoConfiguration(MetricProperties metricProperties) {
this.metricProperties = metricProperties;
}
@Bean
@ConditionalOnProperty(name = "metrics.enabled", havingValue = "true")
public EventsPublisher metricPublisher() {
metricProperties.validate();
return new EventsPublisher(metricProperties);
}
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (metricProperties.isEnabled() && !addedCustomAppender) {
ApplicationPreparedEvent applicationEvent = (ApplicationPreparedEvent) event;
EventsPublisher eventPublisher = applicationEvent.getApplicationContext().getBean(EventsPublisher.class);
//register the log appender
// removed for brevity
addedCustomAppender = true;
}
}
@Override
public int getOrder() {
// this must be higher than LoggingApplicationListener.DEFAULT_ORDER
return Ordered.HIGHEST_PRECEDENCE + 21;
}
@Override
public boolean supportsEventType(ResolvableType eventType) {
return ApplicationPreparedEvent.class.isAssignableFrom(eventType.getRawClass());
}
@Override
public boolean supportsSourceType(Class<?> sourceType) {
return true;
}
}
在爲它工作的申請,我看到3 ApplicationPreparedEvent是建立和supportsEventsType返回true兩次(和onApplicationEvent被調用了兩次,用相同的事件):出現這種情況後,第三個ApplicationPreparedEvent是構建的,而不是之前。
的應用程序註解是:
@SpringBootApplication
@EnableZuulProxy
@EnableBinding(OutPutChannel.class)
@EnableAutoConfiguration(exclude = MetricsDropwizardAutoConfiguration.class)
@IntegrationComponentScan
對於它不工作的其他應用程序,我觀察到只有1 ApplicationPreparedEvent在啓動時建立並不會觸發聽衆,因爲supportsEventsType是所謂,但從未返回true:
對於第一個應用程序,它通過連續叫:
- org.springframework.boot.builder.ParentContextApplicationContextInitializer $ ParentContextAvailableEvent(兩次)
- org.springframework.context.event.ContextRefreshedEvent(兩次)
- org.springframework.boot.context.event.ApplicationReadyEvent(兩次)
應用註解是:
@SpringBootApplication
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class,
HibernateJpaAutoConfiguration.class,
MetricsDropwizardAutoConfiguration.class,
JestAutoConfiguration.class})
- 對於第二應用程式:
- org.springframework.context.event.ContextRefreshedEvent(兩次)
- org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent(兩次)
- org.springframework.boot.context .event.ApplicationReadyEvent(兩次)
與註釋:
@SpringBootApplication
@EnableAutoConfiguration(exclude = JestAutoConfiguration.class)
對於這兩種情況,ApplicationPreparedEvent的跟蹤都未被偵聽器「測試」......
任何提示?我很困惑...
感謝