Skip to content

Commit 1f8c0a4

Browse files
committed
#1252 - Polishing.
We now make sure that BeanPostProcessors that are exposed consume their dependencies via ObjectFactory instances to avoid the need to prematurely trigger bean instantiation in the BeanPostProcessor detection phase. Marked the direct dependencies of the post processors as lazy, as the post processor only do actual work if beans of a particular type are available in the ApplicationContext. In case they aren't we don't even need to instantiate the downstream dependencies of the processors. Avoid proxying of configuration classes where possible. Related pull request: #1251.
1 parent de38b9e commit 1f8c0a4

8 files changed

+26
-23
lines changed

src/main/java/org/springframework/hateoas/config/EntityLinksConfiguration.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* @author Greg Turnquist
3030
* @author Oliver Gierke
3131
*/
32-
@Configuration
32+
@Configuration(proxyBeanMethods = false)
3333
class EntityLinksConfiguration {
3434

3535
@Bean

src/main/java/org/springframework/hateoas/config/HateoasConfiguration.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
* @soundtrack Elephants Crossing - Wait (Live at Stadtfest Dresden)
6060
* @since 0.19
6161
*/
62-
@Configuration
62+
@Configuration(proxyBeanMethods = false)
6363
@EnablePluginRegistries({ LinkDiscoverer.class })
6464
public class HateoasConfiguration {
6565

src/main/java/org/springframework/hateoas/config/RestTemplateHateoasConfiguration.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
import lombok.RequiredArgsConstructor;
1919

2020
import org.springframework.beans.BeansException;
21+
import org.springframework.beans.factory.ObjectFactory;
2122
import org.springframework.beans.factory.config.BeanPostProcessor;
2223
import org.springframework.context.annotation.Bean;
2324
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.context.annotation.Lazy;
2426
import org.springframework.lang.NonNull;
2527
import org.springframework.web.client.RestTemplate;
2628

@@ -30,16 +32,17 @@
3032
* @author Greg Turnquist
3133
* @author Oliver Drotbohm
3234
*/
33-
@Configuration
35+
@Configuration(proxyBeanMethods = false)
3436
class RestTemplateHateoasConfiguration {
3537

3638
@Bean
3739
static HypermediaRestTemplateBeanPostProcessor hypermediaRestTemplateBeanPostProcessor(
38-
HypermediaRestTemplateConfigurer configurer) {
40+
ObjectFactory<HypermediaRestTemplateConfigurer> configurer) {
3941
return new HypermediaRestTemplateBeanPostProcessor(configurer);
4042
}
4143

4244
@Bean
45+
@Lazy
4346
HypermediaRestTemplateConfigurer hypermediaRestTemplateConfigurer(WebConverters converters) {
4447
return new HypermediaRestTemplateConfigurer(converters);
4548
}
@@ -54,7 +57,7 @@ HypermediaRestTemplateConfigurer hypermediaRestTemplateConfigurer(WebConverters
5457
@RequiredArgsConstructor
5558
static class HypermediaRestTemplateBeanPostProcessor implements BeanPostProcessor {
5659

57-
private final HypermediaRestTemplateConfigurer configurer;
60+
private final ObjectFactory<HypermediaRestTemplateConfigurer> configurer;
5861

5962
/*
6063
* (non-Javadoc)
@@ -64,11 +67,9 @@ static class HypermediaRestTemplateBeanPostProcessor implements BeanPostProcesso
6467
@Override
6568
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
6669

67-
if (!RestTemplate.class.isInstance(bean)) {
68-
return bean;
69-
}
70-
71-
return this.configurer.registerHypermediaTypes((RestTemplate) bean);
70+
return !RestTemplate.class.isInstance(bean) //
71+
? bean
72+
: this.configurer.getObject().registerHypermediaTypes((RestTemplate) bean);
7273
}
7374
}
7475
}

src/main/java/org/springframework/hateoas/config/WebClientHateoasConfiguration.java

+11-9
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020
import java.util.List;
2121

2222
import org.springframework.beans.BeansException;
23+
import org.springframework.beans.factory.ObjectFactory;
2324
import org.springframework.beans.factory.ObjectProvider;
2425
import org.springframework.beans.factory.config.BeanPostProcessor;
2526
import org.springframework.context.annotation.Bean;
2627
import org.springframework.context.annotation.Configuration;
28+
import org.springframework.context.annotation.Lazy;
2729
import org.springframework.lang.NonNull;
2830
import org.springframework.web.reactive.function.client.WebClient;
2931

@@ -33,19 +35,21 @@
3335
* Spring WebFlux HATEOAS configuration.
3436
*
3537
* @author Greg Turnquist
36-
* @since 1.0 TODO: Inspect ApplicationContext -> WebApplicationContext -> WebMVC
38+
* @since 1.0
3739
*/
38-
@Configuration
40+
@Configuration(proxyBeanMethods = false)
3941
class WebClientHateoasConfiguration {
4042

4143
@Bean
44+
@Lazy
4245
HypermediaWebClientConfigurer webClientConfigurer(ObjectProvider<ObjectMapper> mapper,
4346
List<HypermediaMappingInformation> hypermediaTypes) {
4447
return new HypermediaWebClientConfigurer(mapper.getIfAvailable(ObjectMapper::new), hypermediaTypes);
4548
}
4649

4750
@Bean
48-
static HypermediaWebClientBeanPostProcessor webClientBeanPostProcessor(HypermediaWebClientConfigurer configurer) {
51+
static HypermediaWebClientBeanPostProcessor webClientBeanPostProcessor(
52+
ObjectFactory<HypermediaWebClientConfigurer> configurer) {
4953
return new HypermediaWebClientBeanPostProcessor(configurer);
5054
}
5155

@@ -59,7 +63,7 @@ static HypermediaWebClientBeanPostProcessor webClientBeanPostProcessor(Hypermedi
5963
@RequiredArgsConstructor
6064
static class HypermediaWebClientBeanPostProcessor implements BeanPostProcessor {
6165

62-
private final HypermediaWebClientConfigurer configurer;
66+
private final ObjectFactory<HypermediaWebClientConfigurer> configurer;
6367

6468
/*
6569
* (non-Javadoc)
@@ -69,11 +73,9 @@ static class HypermediaWebClientBeanPostProcessor implements BeanPostProcessor {
6973
@Override
7074
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
7175

72-
if (WebClient.class.isInstance(bean)) {
73-
return this.configurer.registerHypermediaTypes(((WebClient) bean).mutate()).build();
74-
}
75-
76-
return bean;
76+
return !WebClient.class.isInstance(bean) //
77+
? bean //
78+
: this.configurer.getObject().registerHypermediaTypes(((WebClient) bean).mutate()).build();
7779
}
7880
}
7981
}

src/main/java/org/springframework/hateoas/config/WebFluxHateoasConfiguration.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
* @author Oliver Drotbohm
4545
* @since 1.0
4646
*/
47-
@Configuration
47+
@Configuration(proxyBeanMethods = false)
4848
class WebFluxHateoasConfiguration {
4949

5050
@Bean

src/main/java/org/springframework/hateoas/config/WebMvcEntityLinksConfiguration.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* @author Greg Turnquist
3030
* @author Oliver Gierke
3131
*/
32-
@Configuration
32+
@Configuration(proxyBeanMethods = false)
3333
class WebMvcEntityLinksConfiguration extends EntityLinksConfiguration {
3434

3535
@Bean

src/main/java/org/springframework/hateoas/config/WebMvcHateoasConfiguration.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
* @author Greg Turnquist
4545
* @author Oliver Drotbohm
4646
*/
47-
@Configuration
47+
@Configuration(proxyBeanMethods = false)
4848
@Import(WebMvcEntityLinksConfiguration.class)
4949
class WebMvcHateoasConfiguration {
5050

src/main/java/org/springframework/hateoas/mediatype/hal/HalMediaTypeConfiguration.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
* @author Greg Turnquist
4141
* @author Oliver Drotbohm
4242
*/
43-
@Configuration
43+
@Configuration(proxyBeanMethods = false)
4444
@RequiredArgsConstructor
4545
public class HalMediaTypeConfiguration implements HypermediaMappingInformation {
4646

0 commit comments

Comments
 (0)