From 0f7488f84c4bd2d098a30a7d5af2b5c1e6d452ea Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Wed, 19 Oct 2022 13:18:50 -0400 Subject: [PATCH] GH-369: Fix HeaderEnricher for no proxyBeanMethod (#376) Spring Boot `@AutoConfiguration` comes now with a `proxyBeanMethods = false`, so we cannot call bean methods within the same configuration class. * Rework `HeaderEnricherFunctionConfiguration` for bean methods autowiring * Create an `ExpressionEvaluatingHeaderValueMessageProcessor` instances directly in the `headerEnricher` bean definition and propagate an injected `BeanFactory` * Re-enable disabled tests Fixes #369 --- .../HeaderEnricherFunctionConfiguration.java | 23 ++++++++----------- ...eaderEnricherFunctionApplicationTests.java | 2 -- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/function/header-enricher-function/src/main/java/org/springframework/cloud/fn/header/enricher/HeaderEnricherFunctionConfiguration.java b/function/header-enricher-function/src/main/java/org/springframework/cloud/fn/header/enricher/HeaderEnricherFunctionConfiguration.java index 20e4da31..1f05aaf5 100644 --- a/function/header-enricher-function/src/main/java/org/springframework/cloud/fn/header/enricher/HeaderEnricherFunctionConfiguration.java +++ b/function/header-enricher-function/src/main/java/org/springframework/cloud/fn/header/enricher/HeaderEnricherFunctionConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2020-2020 the original author or authors. + * Copyright 2020-2022 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. @@ -22,13 +22,12 @@ import java.util.Map; import java.util.Properties; import java.util.function.Function; +import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Scope; import org.springframework.integration.transformer.HeaderEnricher; import org.springframework.integration.transformer.support.ExpressionEvaluatingHeaderValueMessageProcessor; import org.springframework.messaging.Message; @@ -37,6 +36,7 @@ import org.springframework.messaging.Message; * @author Gary Russell * @author Christian Tzolov * @author Soby Chacko + * @author Artem Bilan */ @AutoConfiguration @EnableConfigurationProperties(HeaderEnricherFunctionProperties.class) @@ -47,28 +47,25 @@ public class HeaderEnricherFunctionConfiguration { private HeaderEnricherFunctionProperties properties; @Bean - public Function, Message> headerEnricherFunction() { - return headerEnricher()::transform; + public Function, Message> headerEnricherFunction(HeaderEnricher headerEnricher) { + return headerEnricher::transform; } @Bean - public HeaderEnricher headerEnricher() { + public HeaderEnricher headerEnricher(BeanFactory beanFactory) { Map> headersToAdd = new HashMap<>(); Properties props = this.properties.getHeaders(); Enumeration enumeration = props.propertyNames(); while (enumeration.hasMoreElements()) { String propertyName = (String) enumeration.nextElement(); - headersToAdd.put(propertyName, processor(props.getProperty(propertyName))); + ExpressionEvaluatingHeaderValueMessageProcessor headerValueMessageProcessor = + new ExpressionEvaluatingHeaderValueMessageProcessor<>(props.getProperty(propertyName), null); + headerValueMessageProcessor.setBeanFactory(beanFactory); + headersToAdd.put(propertyName, headerValueMessageProcessor); } HeaderEnricher headerEnricher = new HeaderEnricher(headersToAdd); headerEnricher.setDefaultOverwrite(this.properties.isOverwrite()); return headerEnricher; } - @Bean - @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) // Need a new processor for each header - public ExpressionEvaluatingHeaderValueMessageProcessor processor(String expression) { - return new ExpressionEvaluatingHeaderValueMessageProcessor<>(expression, null); - } - } diff --git a/function/header-enricher-function/src/test/java/org/springframework/cloud/fn/header/enricher/HeaderEnricherFunctionApplicationTests.java b/function/header-enricher-function/src/test/java/org/springframework/cloud/fn/header/enricher/HeaderEnricherFunctionApplicationTests.java index d19ee4f2..bf81d8ec 100644 --- a/function/header-enricher-function/src/test/java/org/springframework/cloud/fn/header/enricher/HeaderEnricherFunctionApplicationTests.java +++ b/function/header-enricher-function/src/test/java/org/springframework/cloud/fn/header/enricher/HeaderEnricherFunctionApplicationTests.java @@ -18,7 +18,6 @@ package org.springframework.cloud.fn.header.enricher; import java.util.function.Function; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -42,7 +41,6 @@ import static org.hamcrest.Matchers.equalTo; "header.enricher.headers=foo='bar' \\n baz='fiz' \\n buz=payload \\n jaz=@value", "header.enricher.overwrite = true" }) @DirtiesContext -@Disabled public class HeaderEnricherFunctionApplicationTests { @Autowired