From f150589b8500ea0796a82d9a57dee8f472396392 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Thu, 9 Apr 2020 14:21:11 +0200 Subject: [PATCH] Remove SpEL from Jafu samples Closes gh-72 --- .../jafu-webmvc/pom.xml | 37 +++++++++++-- .../jafu/Target_SpringApplication.java | 18 ++++++ .../META-INF/native-image/reflect-config.json | 19 ------- spring-graal-native-samples/jafu/compile.sh | 3 + spring-graal-native-samples/jafu/pom.xml | 22 ++++++++ .../jafu/Target_SpringApplication.java | 18 ++++++ .../GenericApplicationContextWithoutSpel.java | 55 +++++++++++++++++++ .../META-INF/native-image/reflect-config.json | 3 - .../internal/svm/RemoveSpelSupport.java | 12 ++++ 9 files changed, 159 insertions(+), 28 deletions(-) create mode 100644 spring-graal-native-samples/jafu-webmvc/src/main/java/com/example/jafu/Target_SpringApplication.java create mode 100644 spring-graal-native-samples/jafu/src/main/java/com/example/jafu/Target_SpringApplication.java create mode 100644 spring-graal-native-samples/jafu/src/main/java/org/springframework/context/support/GenericApplicationContextWithoutSpel.java delete mode 100644 spring-graal-native-samples/jafu/src/main/resources/META-INF/native-image/reflect-config.json create mode 100644 spring-graal-native-substitutions/src/main/java/org/springframework/internal/svm/RemoveSpelSupport.java diff --git a/spring-graal-native-samples/jafu-webmvc/pom.xml b/spring-graal-native-samples/jafu-webmvc/pom.xml index 3e7ef7df3..4e51a19d7 100644 --- a/spring-graal-native-samples/jafu-webmvc/pom.xml +++ b/spring-graal-native-samples/jafu-webmvc/pom.xml @@ -26,18 +26,43 @@ org.springframework.boot spring-boot-starter + + + org.yaml + snakeyaml + + + + + org.springframework.fu + spring-fu-jafu + 0.3.0.M2 + + + org.springframework + spring-context + + + org.springframework + spring-expression + + + + + org.springframework + spring-webmvc + + + org.springframework + spring-expression + + org.graalvm.nativeimage graal-hotspot-library 20.0.0 provided - true - - - org.springframework.fu - spring-fu-jafu - 0.3.0.M2 org.springframework.boot diff --git a/spring-graal-native-samples/jafu-webmvc/src/main/java/com/example/jafu/Target_SpringApplication.java b/spring-graal-native-samples/jafu-webmvc/src/main/java/com/example/jafu/Target_SpringApplication.java new file mode 100644 index 000000000..9b59986c1 --- /dev/null +++ b/spring-graal-native-samples/jafu-webmvc/src/main/java/com/example/jafu/Target_SpringApplication.java @@ -0,0 +1,18 @@ +package com.example.jafu; + + +import com.oracle.svm.core.annotate.Substitute; +import com.oracle.svm.core.annotate.TargetClass; + +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.support.ServletWebServerApplicationContextWithoutSpel; + +@TargetClass(className = "org.springframework.boot.SpringApplication") +public final class Target_SpringApplication { + + // Use the application context without SpEL and avoid reflection + @Substitute + protected ConfigurableApplicationContext createApplicationContext() { + return new ServletWebServerApplicationContextWithoutSpel(); + } +} diff --git a/spring-graal-native-samples/jafu-webmvc/src/main/resources/META-INF/native-image/reflect-config.json b/spring-graal-native-samples/jafu-webmvc/src/main/resources/META-INF/native-image/reflect-config.json index 7693957c6..fd7b6882a 100644 --- a/spring-graal-native-samples/jafu-webmvc/src/main/resources/META-INF/native-image/reflect-config.json +++ b/spring-graal-native-samples/jafu-webmvc/src/main/resources/META-INF/native-image/reflect-config.json @@ -63,20 +63,6 @@ "allDeclaredConstructors": true, "allDeclaredMethods": true }, - { - "name": "org.springframework.web.context.ConfigurableWebApplicationContext", - "allDeclaredConstructors": true, - "allDeclaredMethods": true - }, - { - "name": "org.springframework.web.context.WebApplicationContext", - "allDeclaredConstructors": true, - "allDeclaredMethods": true - }, - { - "name": "org.springframework.context.ConfigurableApplicationContext", - "allDeclaredConstructors": true - }, { "name": "org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory$StaticResourceConfigurer", "allDeclaredConstructors": true, @@ -97,11 +83,6 @@ "allDeclaredConstructors": true, "allDeclaredMethods": true }, - { - "name": "org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext", - "allDeclaredConstructors": true, - "allDeclaredMethods": true - }, { "name": "org.springframework.boot.web.server.WebServerFactoryCustomizerBeanPostProcessor", "allDeclaredConstructors": true, diff --git a/spring-graal-native-samples/jafu/compile.sh b/spring-graal-native-samples/jafu/compile.sh index 8365acd3f..e470e0a4d 100755 --- a/spring-graal-native-samples/jafu/compile.sh +++ b/spring-graal-native-samples/jafu/compile.sh @@ -22,6 +22,9 @@ cd target/native-image jar -xvf ../$JAR >/dev/null 2>&1 cp -R META-INF BOOT-INF/classes +# Avoids clashing substitutions from this project deps and the feature deps +rm BOOT-INF/lib/svm-20.*.jar + LIBPATH=`find BOOT-INF/lib | tr '\n' ':'` CP=BOOT-INF/classes:$LIBPATH:$FEATURE diff --git a/spring-graal-native-samples/jafu/pom.xml b/spring-graal-native-samples/jafu/pom.xml index 1b91e4c11..f189d63d9 100644 --- a/spring-graal-native-samples/jafu/pom.xml +++ b/spring-graal-native-samples/jafu/pom.xml @@ -22,12 +22,34 @@ org.springframework.boot spring-boot-starter + + + org.yaml + snakeyaml + + org.springframework.fu spring-fu-jafu 0.3.0.M2 + + org.springframework + spring-context + + + org.springframework + spring-expression + + + + + org.graalvm.nativeimage + graal-hotspot-library + 20.0.0 + provided + org.springframework.boot spring-boot-starter-test diff --git a/spring-graal-native-samples/jafu/src/main/java/com/example/jafu/Target_SpringApplication.java b/spring-graal-native-samples/jafu/src/main/java/com/example/jafu/Target_SpringApplication.java new file mode 100644 index 000000000..f4ef1e9da --- /dev/null +++ b/spring-graal-native-samples/jafu/src/main/java/com/example/jafu/Target_SpringApplication.java @@ -0,0 +1,18 @@ +package com.example.jafu; + + +import com.oracle.svm.core.annotate.Substitute; +import com.oracle.svm.core.annotate.TargetClass; + +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.support.GenericApplicationContextWithoutSpel; + +@TargetClass(className = "org.springframework.boot.SpringApplication") +public final class Target_SpringApplication { + + // Use the application context without SpEL and avoid reflection + @Substitute + protected ConfigurableApplicationContext createApplicationContext() { + return new GenericApplicationContextWithoutSpel(); + } +} diff --git a/spring-graal-native-samples/jafu/src/main/java/org/springframework/context/support/GenericApplicationContextWithoutSpel.java b/spring-graal-native-samples/jafu/src/main/java/org/springframework/context/support/GenericApplicationContextWithoutSpel.java new file mode 100644 index 000000000..5da338160 --- /dev/null +++ b/spring-graal-native-samples/jafu/src/main/java/org/springframework/context/support/GenericApplicationContextWithoutSpel.java @@ -0,0 +1,55 @@ +package org.springframework.context.support; + +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.beans.support.ResourceEditorRegistrar; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.context.ApplicationEventPublisherAware; +import org.springframework.context.EmbeddedValueResolverAware; +import org.springframework.context.EnvironmentAware; +import org.springframework.context.MessageSourceAware; +import org.springframework.context.ResourceLoaderAware; +import org.springframework.core.io.ResourceLoader; + +// Skip StandardBeanExpressionResolver and LoadTimeWeaver +public class GenericApplicationContextWithoutSpel extends GenericApplicationContext { + + @Override + protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) { + // Tell the internal bean factory to use the context's class loader etc. + beanFactory.setBeanClassLoader(getClassLoader()); + beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment())); + + // Configure the bean factory with context callbacks. + beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this)); + beanFactory.ignoreDependencyInterface(EnvironmentAware.class); + beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class); + beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class); + beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class); + beanFactory.ignoreDependencyInterface(MessageSourceAware.class); + beanFactory.ignoreDependencyInterface(ApplicationContextAware.class); + + // BeanFactory interface not registered as resolvable type in a plain factory. + // MessageSource registered (and found for autowiring) as a bean. + beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory); + beanFactory.registerResolvableDependency(ResourceLoader.class, this); + beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this); + beanFactory.registerResolvableDependency(ApplicationContext.class, this); + + // Register early post-processor for detecting inner beans as ApplicationListeners. + beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this)); + + // Register default environment beans. + if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) { + beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment()); + } + if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) { + beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties()); + } + if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) { + beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment()); + } + } +} \ No newline at end of file diff --git a/spring-graal-native-samples/jafu/src/main/resources/META-INF/native-image/reflect-config.json b/spring-graal-native-samples/jafu/src/main/resources/META-INF/native-image/reflect-config.json deleted file mode 100644 index aedc6d3cd..000000000 --- a/spring-graal-native-samples/jafu/src/main/resources/META-INF/native-image/reflect-config.json +++ /dev/null @@ -1,3 +0,0 @@ -[ -{ "name":"org.springframework.context.support.GenericApplicationContext", "methods": [ { "name": "", "parameterTypes": [ ] } ] } -] diff --git a/spring-graal-native-substitutions/src/main/java/org/springframework/internal/svm/RemoveSpelSupport.java b/spring-graal-native-substitutions/src/main/java/org/springframework/internal/svm/RemoveSpelSupport.java new file mode 100644 index 000000000..eaf655492 --- /dev/null +++ b/spring-graal-native-substitutions/src/main/java/org/springframework/internal/svm/RemoveSpelSupport.java @@ -0,0 +1,12 @@ +package org.springframework.internal.svm; + +import java.util.function.BooleanSupplier; + +public class RemoveSpelSupport implements BooleanSupplier { + + @Override + public boolean getAsBoolean() { + return Boolean.valueOf(System.getProperty("spring.graal.remove-spel-support", "false")); + } + +}