#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.
This commit is contained in:
Oliver Drotbohm
2020-04-03 18:24:52 +02:00
parent de38b9e200
commit 1f8c0a4230
8 changed files with 26 additions and 23 deletions

View File

@@ -29,7 +29,7 @@ import org.springframework.plugin.core.support.PluginRegistryFactoryBean;
* @author Greg Turnquist
* @author Oliver Gierke
*/
@Configuration
@Configuration(proxyBeanMethods = false)
class EntityLinksConfiguration {
@Bean

View File

@@ -59,7 +59,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
* @soundtrack Elephants Crossing - Wait (Live at Stadtfest Dresden)
* @since 0.19
*/
@Configuration
@Configuration(proxyBeanMethods = false)
@EnablePluginRegistries({ LinkDiscoverer.class })
public class HateoasConfiguration {

View File

@@ -18,9 +18,11 @@ package org.springframework.hateoas.config;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.lang.NonNull;
import org.springframework.web.client.RestTemplate;
@@ -30,16 +32,17 @@ import org.springframework.web.client.RestTemplate;
* @author Greg Turnquist
* @author Oliver Drotbohm
*/
@Configuration
@Configuration(proxyBeanMethods = false)
class RestTemplateHateoasConfiguration {
@Bean
static HypermediaRestTemplateBeanPostProcessor hypermediaRestTemplateBeanPostProcessor(
HypermediaRestTemplateConfigurer configurer) {
ObjectFactory<HypermediaRestTemplateConfigurer> configurer) {
return new HypermediaRestTemplateBeanPostProcessor(configurer);
}
@Bean
@Lazy
HypermediaRestTemplateConfigurer hypermediaRestTemplateConfigurer(WebConverters converters) {
return new HypermediaRestTemplateConfigurer(converters);
}
@@ -54,7 +57,7 @@ class RestTemplateHateoasConfiguration {
@RequiredArgsConstructor
static class HypermediaRestTemplateBeanPostProcessor implements BeanPostProcessor {
private final HypermediaRestTemplateConfigurer configurer;
private final ObjectFactory<HypermediaRestTemplateConfigurer> configurer;
/*
* (non-Javadoc)
@@ -64,11 +67,9 @@ class RestTemplateHateoasConfiguration {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (!RestTemplate.class.isInstance(bean)) {
return bean;
}
return this.configurer.registerHypermediaTypes((RestTemplate) bean);
return !RestTemplate.class.isInstance(bean) //
? bean
: this.configurer.getObject().registerHypermediaTypes((RestTemplate) bean);
}
}
}

View File

@@ -20,10 +20,12 @@ import lombok.RequiredArgsConstructor;
import java.util.List;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.lang.NonNull;
import org.springframework.web.reactive.function.client.WebClient;
@@ -33,19 +35,21 @@ import com.fasterxml.jackson.databind.ObjectMapper;
* Spring WebFlux HATEOAS configuration.
*
* @author Greg Turnquist
* @since 1.0 TODO: Inspect ApplicationContext -> WebApplicationContext -> WebMVC
* @since 1.0
*/
@Configuration
@Configuration(proxyBeanMethods = false)
class WebClientHateoasConfiguration {
@Bean
@Lazy
HypermediaWebClientConfigurer webClientConfigurer(ObjectProvider<ObjectMapper> mapper,
List<HypermediaMappingInformation> hypermediaTypes) {
return new HypermediaWebClientConfigurer(mapper.getIfAvailable(ObjectMapper::new), hypermediaTypes);
}
@Bean
static HypermediaWebClientBeanPostProcessor webClientBeanPostProcessor(HypermediaWebClientConfigurer configurer) {
static HypermediaWebClientBeanPostProcessor webClientBeanPostProcessor(
ObjectFactory<HypermediaWebClientConfigurer> configurer) {
return new HypermediaWebClientBeanPostProcessor(configurer);
}
@@ -59,7 +63,7 @@ class WebClientHateoasConfiguration {
@RequiredArgsConstructor
static class HypermediaWebClientBeanPostProcessor implements BeanPostProcessor {
private final HypermediaWebClientConfigurer configurer;
private final ObjectFactory<HypermediaWebClientConfigurer> configurer;
/*
* (non-Javadoc)
@@ -69,11 +73,9 @@ class WebClientHateoasConfiguration {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (WebClient.class.isInstance(bean)) {
return this.configurer.registerHypermediaTypes(((WebClient) bean).mutate()).build();
}
return bean;
return !WebClient.class.isInstance(bean) //
? bean //
: this.configurer.getObject().registerHypermediaTypes(((WebClient) bean).mutate()).build();
}
}
}

View File

@@ -44,7 +44,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
* @author Oliver Drotbohm
* @since 1.0
*/
@Configuration
@Configuration(proxyBeanMethods = false)
class WebFluxHateoasConfiguration {
@Bean

View File

@@ -29,7 +29,7 @@ import org.springframework.stereotype.Controller;
* @author Greg Turnquist
* @author Oliver Gierke
*/
@Configuration
@Configuration(proxyBeanMethods = false)
class WebMvcEntityLinksConfiguration extends EntityLinksConfiguration {
@Bean

View File

@@ -44,7 +44,7 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl
* @author Greg Turnquist
* @author Oliver Drotbohm
*/
@Configuration
@Configuration(proxyBeanMethods = false)
@Import(WebMvcEntityLinksConfiguration.class)
class WebMvcHateoasConfiguration {

View File

@@ -40,7 +40,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
* @author Greg Turnquist
* @author Oliver Drotbohm
*/
@Configuration
@Configuration(proxyBeanMethods = false)
@RequiredArgsConstructor
public class HalMediaTypeConfiguration implements HypermediaMappingInformation {