Add another hack for Spring Boot 2.0

This can be reverted in 1.1. The compiler relies on being able to
bind itself to @ConfigurationProperties and the APIs changed for that
in Spring Boot 2.0.

Fixes gh-177
This commit is contained in:
Dave Syer
2018-06-04 14:49:32 +01:00
parent 1fd83527a7
commit d322643433
4 changed files with 26 additions and 8 deletions

View File

@@ -20,6 +20,7 @@ import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
@@ -41,6 +42,8 @@ import org.springframework.context.support.StaticApplicationContext;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
/**
* @author Mark Fisher
@@ -117,9 +120,7 @@ public class FunctionProxyApplicationListener
private void bind(ConfigurableApplicationContext context) {
ConfigurationPropertiesBindingPostProcessor post = new ConfigurationPropertiesBindingPostProcessor();
post.setBeanFactory(new DefaultListableBeanFactory());
post.setEnvironment(context.getEnvironment());
post.setApplicationContext(new StaticApplicationContext());
maybeSetBeanFactory(context, post);
try {
post.afterPropertiesSet();
}
@@ -129,6 +130,23 @@ public class FunctionProxyApplicationListener
post.postProcessBeforeInitialization(this, getClass().getName());
}
private void maybeSetBeanFactory(ConfigurableApplicationContext context,
ConfigurationPropertiesBindingPostProcessor post) {
StaticApplicationContext other = new StaticApplicationContext();
other.setEnvironment(context.getEnvironment());
if (ReflectionUtils.findMethod(ConfigurationPropertiesBindingPostProcessor.class,
"setBeanFactory", BeanFactory.class) != null) {
post.setBeanFactory(new DefaultListableBeanFactory());
post.setEnvironment(context.getEnvironment());
}
else {
String name = "org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata";
other.registerSingleton(name, ClassUtils.resolveClassName(name, null));
other.setParent(context);
}
post.setApplicationContext(other);
}
private void registerByteCodeLoadingProxy(String name, String type, Resource resource,
DefaultListableBeanFactory beanFactory) {
Class<?> proxyClass = null;

View File

@@ -13,14 +13,14 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.11.RELEASE</version>
<version>2.0.2.RELEASE</version>
<relativePath/>
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud-function.version>1.0.0.BUILD-SNAPSHOT</spring-cloud-function.version>
<spring-cloud-stream.version>Ditmars.RELEASE</spring-cloud-stream.version>
<spring-cloud-stream.version>Elmhurst.RELEASE</spring-cloud-stream.version>
<reactor.version>3.1.2.RELEASE</reactor.version>
<wrapper.version>1.0.10.RELEASE</wrapper.version>
</properties>

View File

@@ -19,10 +19,10 @@ package com.example;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;

View File

@@ -19,10 +19,10 @@ package com.example;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
@@ -44,7 +44,7 @@ public class SampleCompiledFunctionTests {
public void lowercase() {
assertThat(new TestRestTemplate().postForObject(
"http://localhost:" + port + "/test", "it works", String.class))
.isEqualTo("it works!!!");
.contains("it works!!!");
}
}