Skip to content

Commit 30a0cca

Browse files
committed
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
1 parent 622606d commit 30a0cca

File tree

20 files changed

+85
-84
lines changed

20 files changed

+85
-84
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/jersey/JerseyWebEndpointManagementContextConfiguration.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,8 +21,6 @@
2121
import java.util.HashSet;
2222
import java.util.List;
2323

24-
import javax.annotation.PostConstruct;
25-
2624
import org.glassfish.jersey.server.ResourceConfig;
2725
import org.glassfish.jersey.server.model.Resource;
2826

@@ -112,10 +110,10 @@ static class JerseyWebEndpointsResourcesRegistrar {
112110
this.mediaTypes = endpointMediaTypes;
113111
this.basePath = basePath;
114112
this.shouldRegisterLinks = shouldRegisterLinks;
113+
register();
115114
}
116115

117-
@PostConstruct
118-
void register() {
116+
private void register() {
119117
// We can't easily use @ConditionalOnBean because @AutoConfigureBefore is
120118
// not an option for management contexts. Instead we manually check if
121119
// the resource config bean exists

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/cache/CacheMetricsRegistrarConfiguration.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,8 +19,6 @@
1919
import java.util.Collection;
2020
import java.util.Map;
2121

22-
import javax.annotation.PostConstruct;
23-
2422
import io.micrometer.core.instrument.MeterRegistry;
2523
import io.micrometer.core.instrument.Tag;
2624

@@ -56,15 +54,15 @@ class CacheMetricsRegistrarConfiguration {
5654
this.registry = registry;
5755
this.cacheManagers = cacheManagers;
5856
this.cacheMetricsRegistrar = new CacheMetricsRegistrar(this.registry, binderProviders);
57+
bindCachesToRegistry();
5958
}
6059

6160
@Bean
6261
CacheMetricsRegistrar cacheMetricsRegistrar() {
6362
return this.cacheMetricsRegistrar;
6463
}
6564

66-
@PostConstruct
67-
void bindCachesToRegistry() {
65+
private void bindCachesToRegistry() {
6866
this.cacheManagers.forEach(this::bindCacheManagerToRegistry);
6967
}
7068

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BasicBatchConfigurer.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.boot.autoconfigure.batch;
1818

19-
import javax.annotation.PostConstruct;
2019
import javax.sql.DataSource;
2120

2221
import org.springframework.batch.core.configuration.annotation.BatchConfigurer;
@@ -26,6 +25,7 @@
2625
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
2726
import org.springframework.batch.core.repository.JobRepository;
2827
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
28+
import org.springframework.beans.factory.InitializingBean;
2929
import org.springframework.boot.autoconfigure.transaction.TransactionManagerCustomizers;
3030
import org.springframework.boot.context.properties.PropertyMapper;
3131
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
@@ -40,7 +40,7 @@
4040
* @author Stephane Nicoll
4141
* @since 1.0.0
4242
*/
43-
public class BasicBatchConfigurer implements BatchConfigurer {
43+
public class BasicBatchConfigurer implements BatchConfigurer, InitializingBean {
4444

4545
private final BatchProperties properties;
4646

@@ -90,7 +90,11 @@ public JobExplorer getJobExplorer() throws Exception {
9090
return this.jobExplorer;
9191
}
9292

93-
@PostConstruct
93+
@Override
94+
public void afterPropertiesSet() {
95+
initialize();
96+
}
97+
9498
public void initialize() {
9599
try {
96100
this.transactionManager = buildTransactionManager();

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfiguration.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,8 +19,6 @@
1919
import java.util.ArrayList;
2020
import java.util.List;
2121

22-
import javax.annotation.PostConstruct;
23-
2422
import org.apache.commons.logging.Log;
2523
import org.apache.commons.logging.LogFactory;
2624

@@ -57,9 +55,9 @@ public class FreeMarkerAutoConfiguration {
5755
public FreeMarkerAutoConfiguration(ApplicationContext applicationContext, FreeMarkerProperties properties) {
5856
this.applicationContext = applicationContext;
5957
this.properties = properties;
58+
checkTemplateLocationExists();
6059
}
6160

62-
@PostConstruct
6361
public void checkTemplateLocationExists() {
6462
if (logger.isWarnEnabled() && this.properties.isCheckTemplateLocation()) {
6563
List<TemplateLocation> locations = getLocations();

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.security.CodeSource;
2020
import java.security.ProtectionDomain;
2121

22-
import javax.annotation.PostConstruct;
2322
import javax.servlet.Servlet;
2423

2524
import groovy.text.markup.MarkupTemplateEngine;
@@ -77,9 +76,9 @@ public static class GroovyMarkupConfiguration {
7776
public GroovyMarkupConfiguration(ApplicationContext applicationContext, GroovyTemplateProperties properties) {
7877
this.applicationContext = applicationContext;
7978
this.properties = properties;
79+
checkTemplateLocationExists();
8080
}
8181

82-
@PostConstruct
8382
public void checkTemplateLocationExists() {
8483
if (this.properties.isCheckTemplateLocation() && !isUsingGroovyAllJar()) {
8584
TemplateLocation location = new TemplateLocation(this.properties.getResourceLoaderPath());

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hateoas/HypermediaHttpMessageConverterConfiguration.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,11 +20,10 @@
2020
import java.util.Collection;
2121
import java.util.List;
2222

23-
import javax.annotation.PostConstruct;
24-
2523
import org.springframework.beans.BeansException;
2624
import org.springframework.beans.factory.BeanFactory;
2725
import org.springframework.beans.factory.BeanFactoryAware;
26+
import org.springframework.beans.factory.InitializingBean;
2827
import org.springframework.beans.factory.ListableBeanFactory;
2928
import org.springframework.beans.factory.config.BeanPostProcessor;
3029
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@@ -60,12 +59,13 @@ public static HalMessageConverterSupportedMediaTypesCustomizer halMessageConvert
6059
* {@code Jackson2ModuleRegisteringBeanPostProcessor} has registered the converter and
6160
* it is unordered.
6261
*/
63-
private static class HalMessageConverterSupportedMediaTypesCustomizer implements BeanFactoryAware {
62+
private static class HalMessageConverterSupportedMediaTypesCustomizer
63+
implements BeanFactoryAware, InitializingBean {
6464

6565
private volatile BeanFactory beanFactory;
6666

67-
@PostConstruct
68-
void configureHttpMessageConverters() {
67+
@Override
68+
public void afterPropertiesSet() {
6969
if (this.beanFactory instanceof ListableBeanFactory) {
7070
configureHttpMessageConverters(((ListableBeanFactory) this.beanFactory)
7171
.getBeansOfType(RequestMappingHandlerAdapter.class).values());

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceJmxConfiguration.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@
1818

1919
import java.sql.SQLException;
2020

21-
import javax.annotation.PostConstruct;
2221
import javax.sql.DataSource;
2322

2423
import com.zaxxer.hikari.HikariDataSource;
@@ -59,10 +58,10 @@ static class Hikari {
5958
Hikari(DataSource dataSource, ObjectProvider<MBeanExporter> mBeanExporter) {
6059
this.dataSource = dataSource;
6160
this.mBeanExporter = mBeanExporter;
61+
validateMBeans();
6262
}
6363

64-
@PostConstruct
65-
void validateMBeans() {
64+
private void validateMBeans() {
6665
HikariDataSource hikariDataSource = DataSourceUnwrapper.unwrap(this.dataSource, HikariDataSource.class);
6766
if (hikariDataSource != null && hikariDataSource.isRegisterMbeans()) {
6867
this.mBeanExporter.ifUnique((exporter) -> exporter.addExcludedBean("dataSource"));

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfiguration.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,7 +19,6 @@
1919
import java.util.Collections;
2020
import java.util.EnumSet;
2121

22-
import javax.annotation.PostConstruct;
2322
import javax.servlet.DispatcherType;
2423
import javax.servlet.ServletContext;
2524
import javax.servlet.ServletException;
@@ -95,22 +94,11 @@ public class JerseyAutoConfiguration implements ServletContextAware {
9594

9695
private final ResourceConfig config;
9796

98-
private final ObjectProvider<ResourceConfigCustomizer> customizers;
99-
10097
public JerseyAutoConfiguration(JerseyProperties jersey, ResourceConfig config,
10198
ObjectProvider<ResourceConfigCustomizer> customizers) {
10299
this.jersey = jersey;
103100
this.config = config;
104-
this.customizers = customizers;
105-
}
106-
107-
@PostConstruct
108-
public void path() {
109-
customize();
110-
}
111-
112-
private void customize() {
113-
this.customizers.orderedStream().forEach((customizer) -> customizer.customize(this.config));
101+
customizers.orderedStream().forEach((customizer) -> customizer.customize(this.config));
114102
}
115103

116104
@Bean

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mail/MailSenderValidatorAutoConfiguration.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.boot.autoconfigure.mail;
1818

19-
import javax.annotation.PostConstruct;
2019
import javax.mail.MessagingException;
2120

2221
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
@@ -44,9 +43,9 @@ public class MailSenderValidatorAutoConfiguration {
4443

4544
public MailSenderValidatorAutoConfiguration(JavaMailSenderImpl mailSender) {
4645
this.mailSender = mailSender;
46+
validateConnection();
4747
}
4848

49-
@PostConstruct
5049
public void validateConnection() {
5150
try {
5251
this.mailSender.testConnection();

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfiguration.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.boot.autoconfigure.mustache;
1818

19-
import javax.annotation.PostConstruct;
20-
2119
import com.samskivert.mustache.Mustache;
2220
import com.samskivert.mustache.Mustache.Collector;
2321
import com.samskivert.mustache.Mustache.TemplateLoader;
@@ -57,9 +55,9 @@ public class MustacheAutoConfiguration {
5755
public MustacheAutoConfiguration(MustacheProperties mustache, ApplicationContext applicationContext) {
5856
this.mustache = mustache;
5957
this.applicationContext = applicationContext;
58+
checkTemplateLocationExists();
6059
}
6160

62-
@PostConstruct
6361
public void checkTemplateLocationExists() {
6462
if (this.mustache.isCheckTemplateLocation()) {
6563
TemplateLocation location = new TemplateLocation(this.mustache.getPrefix());

0 commit comments

Comments
 (0)