Remove use of @PostConstruct from main code
When running on Java 11 (where `@PostConstruct` is no longer part of the JRE) and without a dependency on jakarta-annotation-api, `@PostContruct` annotions are silently dropped. This leads to obscure and hard-to-track down changes in the behaviour of our auto-configuration as the `@PostConstruct`-annotated methods are not invoked. To allow users to run on Java 11 without having jakarta-annotation-api on the classpath, this commit removes use of `@PostConstruct` from main code. A Checkstyle rule has also been added to prevent its usage in main code from being reintroduced. Closes gh-23723
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -20,8 +20,6 @@ import java.lang.reflect.Modifier;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.KeyDeserializer;
|
||||
@@ -33,6 +31,7 @@ import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.HierarchicalBeanFactory;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.ListableBeanFactory;
|
||||
import org.springframework.boot.jackson.JsonComponent.Scope;
|
||||
import org.springframework.core.ResolvableType;
|
||||
@@ -51,7 +50,7 @@ import org.springframework.util.ObjectUtils;
|
||||
* @since 1.4.0
|
||||
* @see JsonComponent
|
||||
*/
|
||||
public class JsonComponentModule extends SimpleModule implements BeanFactoryAware {
|
||||
public class JsonComponentModule extends SimpleModule implements BeanFactoryAware, InitializingBean {
|
||||
|
||||
private BeanFactory beanFactory;
|
||||
|
||||
@@ -60,7 +59,11 @@ public class JsonComponentModule extends SimpleModule implements BeanFactoryAwar
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
registerJsonComponents();
|
||||
}
|
||||
|
||||
public void registerJsonComponents() {
|
||||
BeanFactory beanFactory = this.beanFactory;
|
||||
while (beanFactory != null) {
|
||||
|
||||
@@ -18,9 +18,9 @@ package org.springframework.boot.jdbc;
|
||||
|
||||
import java.sql.DatabaseMetaData;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
|
||||
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
|
||||
@@ -35,7 +35,7 @@ import org.springframework.util.Assert;
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.5.0
|
||||
*/
|
||||
public abstract class AbstractDataSourceInitializer {
|
||||
public abstract class AbstractDataSourceInitializer implements InitializingBean {
|
||||
|
||||
private static final String PLATFORM_PLACEHOLDER = "@@platform@@";
|
||||
|
||||
@@ -50,7 +50,11 @@ public abstract class AbstractDataSourceInitializer {
|
||||
this.resourceLoader = resourceLoader;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
initialize();
|
||||
}
|
||||
|
||||
protected void initialize() {
|
||||
if (!isEnabled()) {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user