Use Binder directly instead of relying on CPBPP

Fizes gh-351 a bit more thoroughly
This commit is contained in:
Dave Syer
2019-04-01 14:59:26 +01:00
parent 5d51c6187f
commit bb2f78cb36

View File

@@ -24,9 +24,9 @@ import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.context.event.ApplicationPreparedEvent; import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor; import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.cloud.function.compiler.ConsumerCompiler; import org.springframework.cloud.function.compiler.ConsumerCompiler;
import org.springframework.cloud.function.compiler.FunctionCompiler; import org.springframework.cloud.function.compiler.FunctionCompiler;
import org.springframework.cloud.function.compiler.SupplierCompiler; import org.springframework.cloud.function.compiler.SupplierCompiler;
@@ -38,7 +38,6 @@ import org.springframework.cloud.function.compiler.proxy.LambdaCompilingFunction
import org.springframework.cloud.function.compiler.proxy.LambdaCompilingSupplier; import org.springframework.cloud.function.compiler.proxy.LambdaCompilingSupplier;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@@ -85,7 +84,7 @@ public class FunctionProxyApplicationListener
ConfigurableApplicationContext context = event.getApplicationContext(); ConfigurableApplicationContext context = event.getApplicationContext();
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) context DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) context
.getBeanFactory(); .getBeanFactory();
bind(context); bind(context, beanFactory);
for (Map.Entry<String, Object> entry : this.compile.entrySet()) { for (Map.Entry<String, Object> entry : this.compile.entrySet()) {
String name = entry.getKey(); String name = entry.getKey();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@@ -116,26 +115,10 @@ public class FunctionProxyApplicationListener
} }
} }
private void bind(ConfigurableApplicationContext context) { private void bind(ConfigurableApplicationContext application,
ConfigurationPropertiesBindingPostProcessor post = new ConfigurationPropertiesBindingPostProcessor(); DefaultListableBeanFactory context) {
maybeSetBeanFactory(context, post); Binder.get(application.getEnvironment()).bind("spring.cloud.function",
try { Bindable.ofInstance(this));
post.afterPropertiesSet();
}
catch (Exception e) {
throw new IllegalStateException("Cannot bind properties", e);
}
post.postProcessBeforeInitialization(this, getClass().getName());
}
private void maybeSetBeanFactory(ConfigurableApplicationContext context,
ConfigurationPropertiesBindingPostProcessor post) {
StaticApplicationContext other = new StaticApplicationContext();
other.setEnvironment(context.getEnvironment());
other.registerSingleton(ConfigurationBeanFactoryMetadata.class.getName(),
ConfigurationBeanFactoryMetadata.class);
other.setParent(context);
post.setApplicationContext(other);
} }
private void registerByteCodeLoadingProxy(String name, String type, Resource resource, private void registerByteCodeLoadingProxy(String name, String type, Resource resource,