-
Notifications
You must be signed in to change notification settings - Fork 1.1k
GH-3828: Initial Spring AOT support #3832
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
151 changes: 151 additions & 0 deletions
151
...-integration-core/src/main/java/org/springframework/integration/aot/CoreRuntimeHints.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* | ||
* Copyright 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.integration.aot; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.HashMap; | ||
import java.util.Hashtable; | ||
import java.util.Properties; | ||
import java.util.UUID; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Stream; | ||
|
||
import org.springframework.aop.SpringProxy; | ||
import org.springframework.aop.framework.Advised; | ||
import org.springframework.aot.hint.MemberCategory; | ||
import org.springframework.aot.hint.ProxyHints; | ||
import org.springframework.aot.hint.ReflectionHints; | ||
import org.springframework.aot.hint.RuntimeHints; | ||
import org.springframework.aot.hint.RuntimeHintsRegistrar; | ||
import org.springframework.aot.hint.SerializationHints; | ||
import org.springframework.aot.hint.TypeReference; | ||
import org.springframework.beans.factory.config.BeanExpressionContext; | ||
import org.springframework.context.SmartLifecycle; | ||
import org.springframework.core.DecoratingProxy; | ||
import org.springframework.integration.context.IntegrationContextUtils; | ||
import org.springframework.integration.core.GenericSelector; | ||
import org.springframework.integration.core.Pausable; | ||
import org.springframework.integration.dsl.IntegrationFlow; | ||
import org.springframework.integration.gateway.MethodArgsHolder; | ||
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler; | ||
import org.springframework.integration.handler.DelayHandler; | ||
import org.springframework.integration.handler.GenericHandler; | ||
import org.springframework.integration.history.MessageHistory; | ||
import org.springframework.integration.json.JsonPathUtils; | ||
import org.springframework.integration.message.AdviceMessage; | ||
import org.springframework.integration.routingslip.ExpressionEvaluatingRoutingSlipRouteStrategy; | ||
import org.springframework.integration.store.MessageGroupMetadata; | ||
import org.springframework.integration.store.MessageHolder; | ||
import org.springframework.integration.store.MessageMetadata; | ||
import org.springframework.integration.support.MutableMessage; | ||
import org.springframework.integration.support.MutableMessageHeaders; | ||
import org.springframework.integration.support.management.ManageableSmartLifecycle; | ||
import org.springframework.integration.transformer.GenericTransformer; | ||
import org.springframework.messaging.MessageHeaders; | ||
import org.springframework.messaging.support.ErrorMessage; | ||
import org.springframework.messaging.support.GenericMessage; | ||
|
||
/** | ||
* {@link RuntimeHintsRegistrar} for Spring Integration core module. | ||
* | ||
* @author Artem Bilan | ||
* | ||
* @since 6.0 | ||
*/ | ||
class CoreRuntimeHints implements RuntimeHintsRegistrar { | ||
|
||
@Override | ||
public void registerHints(RuntimeHints hints, ClassLoader classLoader) { | ||
ReflectionHints reflectionHints = hints.reflection(); | ||
Stream.of( | ||
GenericSelector.class, | ||
GenericTransformer.class, | ||
GenericHandler.class, | ||
Function.class, | ||
Supplier.class, | ||
BeanExpressionContext.class, | ||
IntegrationContextUtils.class, | ||
MethodArgsHolder.class, | ||
AbstractReplyProducingMessageHandler.RequestHandler.class, | ||
ExpressionEvaluatingRoutingSlipRouteStrategy.RequestAndReply.class, | ||
Pausable.class, | ||
ManageableSmartLifecycle.class) | ||
.forEach(type -> | ||
reflectionHints.registerType(type, | ||
builder -> builder.withMembers(MemberCategory.INVOKE_PUBLIC_METHODS))); | ||
|
||
reflectionHints.registerType(JsonPathUtils.class, | ||
builder -> | ||
builder.onReachableType(TypeReference.of("com.jayway.jsonpath.JsonPath")) | ||
.withMembers(MemberCategory.INVOKE_PUBLIC_METHODS)); | ||
|
||
// For #xpath() SpEL function | ||
reflectionHints.registerTypeIfPresent(classLoader, "org.springframework.integration.xml.xpath.XPathUtils", | ||
builder -> builder.withMembers(MemberCategory.INVOKE_PUBLIC_METHODS)); | ||
|
||
Stream.of( | ||
"kotlin.jvm.functions.Function0", | ||
"kotlin.jvm.functions.Function1", | ||
"kotlin.Unit") | ||
.forEach(type -> | ||
reflectionHints.registerTypeIfPresent(classLoader, type, | ||
builder -> builder.withMembers(MemberCategory.INVOKE_PUBLIC_METHODS))); | ||
|
||
hints.resources().registerPattern("META-INF/spring.integration.properties"); | ||
|
||
SerializationHints serializationHints = hints.serialization(); | ||
Stream.of( | ||
String.class, | ||
Number.class, | ||
Long.class, | ||
Date.class, | ||
ArrayList.class, | ||
HashMap.class, | ||
Properties.class, | ||
Hashtable.class, | ||
Exception.class, | ||
UUID.class, | ||
GenericMessage.class, | ||
ErrorMessage.class, | ||
MessageHeaders.class, | ||
AdviceMessage.class, | ||
MutableMessage.class, | ||
MutableMessageHeaders.class, | ||
MessageGroupMetadata.class, | ||
MessageHolder.class, | ||
MessageMetadata.class, | ||
MessageHistory.class, | ||
MessageHistory.Entry.class, | ||
DelayHandler.DelayedMessageWrapper.class) | ||
.forEach(serializationHints::registerType); | ||
|
||
ProxyHints proxyHints = hints.proxies(); | ||
|
||
registerSpringJdkProxy(proxyHints, AbstractReplyProducingMessageHandler.RequestHandler.class); | ||
registerSpringJdkProxy(proxyHints, IntegrationFlow.class, SmartLifecycle.class); | ||
} | ||
|
||
private static void registerSpringJdkProxy(ProxyHints proxyHints, Class<?>... proxiedInterfaces) { | ||
proxyHints | ||
.registerJdkProxy(builder -> | ||
builder.proxiedInterfaces(proxiedInterfaces) | ||
.proxiedInterfaces(SpringProxy.class, Advised.class, DecoratingProxy.class)); | ||
} | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
...in/java/org/springframework/integration/aot/GatewayProxyBeanRegistrationAotProcessor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.integration.aot; | ||
|
||
import java.util.function.Predicate; | ||
|
||
import org.springframework.aop.SpringProxy; | ||
import org.springframework.aop.framework.Advised; | ||
import org.springframework.aot.generate.GenerationContext; | ||
import org.springframework.beans.factory.FactoryBean; | ||
import org.springframework.beans.factory.aot.BeanRegistrationAotContribution; | ||
import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor; | ||
import org.springframework.beans.factory.aot.BeanRegistrationCode; | ||
import org.springframework.beans.factory.aot.BeanRegistrationCodeFragments; | ||
import org.springframework.beans.factory.support.RegisteredBean; | ||
import org.springframework.beans.factory.support.RootBeanDefinition; | ||
import org.springframework.core.DecoratingProxy; | ||
import org.springframework.integration.gateway.GatewayProxyFactoryBean; | ||
import org.springframework.javapoet.CodeBlock; | ||
|
||
/** | ||
* {@link BeanRegistrationAotProcessor} for registering proxy interfaces of the {@link GatewayProxyFactoryBean} beans. | ||
* | ||
* @author Artem Bilan | ||
* | ||
* @since 6.0 | ||
*/ | ||
class GatewayProxyBeanRegistrationAotProcessor implements BeanRegistrationAotProcessor { | ||
|
||
@Override | ||
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) { | ||
if (GatewayProxyFactoryBean.class.isAssignableFrom(registeredBean.getBeanClass())) { | ||
return BeanRegistrationAotContribution | ||
.ofBeanRegistrationCodeFragmentsCustomizer(GatewayProxyBeanRegistrationCodeFragments::new); | ||
} | ||
return null; | ||
} | ||
|
||
private static class GatewayProxyBeanRegistrationCodeFragments extends BeanRegistrationCodeFragments { | ||
|
||
GatewayProxyBeanRegistrationCodeFragments(BeanRegistrationCodeFragments codeFragments) { | ||
super(codeFragments); | ||
} | ||
|
||
@Override | ||
public CodeBlock generateSetBeanDefinitionPropertiesCode(GenerationContext generationContext, | ||
BeanRegistrationCode beanRegistrationCode, RootBeanDefinition beanDefinition, | ||
Predicate<String> attributeFilter) { | ||
|
||
Class<?> serviceInterface = (Class<?>) beanDefinition.getAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE); | ||
generationContext.getRuntimeHints().proxies() | ||
.registerJdkProxy(serviceInterface, SpringProxy.class, Advised.class, DecoratingProxy.class); | ||
|
||
return super.generateSetBeanDefinitionPropertiesCode(generationContext, beanRegistrationCode, | ||
beanDefinition, FactoryBean.OBJECT_TYPE_ATTRIBUTE::equals); | ||
} | ||
|
||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
spring-integration-core/src/main/java/org/springframework/integration/aot/package-info.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* Provides classes to support Spring AOT. | ||
*/ | ||
@org.springframework.lang.NonNullApi | ||
@org.springframework.lang.NonNullFields | ||
package org.springframework.integration.aot; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand this comment. What is the problem there?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See the method signature:
setHandler(Object)
.It works well with regular Java, but in the native image it just doesn't see this method: perhaps it tries to match the setter with the type we provide for the property.
And unfortunately that type is not always just
MessageHandler
. It can be aReactiveMessageHandler
.I cannot have a couple setters because Java Beans introspector cannot guess what setter I'd like to use for XML bean definition parser.