-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* [ISSUE #315] Support spring 6.x * Support native-image
- Loading branch information
1 parent
e8cae2c
commit d4543ec
Showing
29 changed files
with
1,649 additions
and
27 deletions.
There are no files selected for viewing
This file contains 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,38 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>com.alibaba.nacos</groupId> | ||
<artifactId>nacos-spring-parent</artifactId> | ||
<version>${revision}</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
|
||
<groupId>com.alibaba.nacos</groupId> | ||
<artifactId>nacos-spring-context-aot</artifactId> | ||
<version>${revision}</version> | ||
<name>Alibaba Nacos :: Spring :: Context :: Aot</name> | ||
<packaging>jar</packaging> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-beans</artifactId> | ||
<version>${spring6.framework.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-core</artifactId> | ||
<version>${spring6.framework.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba.nacos</groupId> | ||
<artifactId>nacos-spring-context</artifactId> | ||
<version>${revision}</version> | ||
<optional>true</optional> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
76 changes: 76 additions & 0 deletions
76
...c/main/java/com/alibaba/nacos/spring/aot/NacosAnnotationBeanRegistrationAotProcessor.java
This file contains 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,76 @@ | ||
/* | ||
* | ||
* * Licensed to the Apache Software Foundation (ASF) under one or more | ||
* * contributor license agreements. See the NOTICE file distributed with | ||
* * this work for additional information regarding copyright ownership. | ||
* * The ASF licenses this file to You 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 | ||
* * | ||
* * http://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 com.alibaba.nacos.spring.aot; | ||
|
||
import com.alibaba.nacos.api.annotation.NacosInjected; | ||
import com.alibaba.nacos.api.config.annotation.NacosValue; | ||
import org.springframework.aot.generate.GenerationContext; | ||
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.support.RegisteredBean; | ||
import org.springframework.util.ReflectionUtils; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* {@link NacosInjected} and {@link NacosValue} AotProcessor | ||
* The fields annotated with {@link NacosInjected} or {@link NacosValue} must be added to the reflect-config.json | ||
* @author SuperZ1999 | ||
*/ | ||
public class NacosAnnotationBeanRegistrationAotProcessor implements BeanRegistrationAotProcessor { | ||
@Override | ||
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) { | ||
Class<?> beanClass = registeredBean.getBeanClass(); | ||
List<Field> fields = new ArrayList<>(); | ||
ReflectionUtils.doWithFields(beanClass, field -> { | ||
NacosInjected injectedAnnotation = field.getDeclaredAnnotation(NacosInjected.class); | ||
NacosValue nacosValueAnnotation = field.getDeclaredAnnotation(NacosValue.class); | ||
if (injectedAnnotation != null || nacosValueAnnotation != null) { | ||
fields.add(field); | ||
} | ||
}); | ||
if (fields.isEmpty()) { | ||
return null; | ||
} | ||
return new AotContribution(fields); | ||
} | ||
|
||
private static class AotContribution implements BeanRegistrationAotContribution { | ||
private final List<Field> fields; | ||
|
||
public AotContribution() { | ||
this.fields = new ArrayList<>(); | ||
} | ||
|
||
public AotContribution(List<Field> fields) { | ||
this.fields = fields; | ||
} | ||
|
||
@Override | ||
public void applyTo(GenerationContext generationContext, BeanRegistrationCode beanRegistrationCode) { | ||
for (Field field : fields) { | ||
generationContext.getRuntimeHints().reflection().registerField(field); | ||
} | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...sources/META-INF/native-image/com.alibaba.nacos/nacos-spring-context/resource-config.json
This file contains 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,15 @@ | ||
{ | ||
"resources": { | ||
"includes": [ | ||
{ | ||
"pattern": "\\QMETA-INF/spring.handlers\\E" | ||
}, | ||
{ | ||
"pattern": "\\QMETA-INF/spring.schemas\\E" | ||
}, | ||
{ | ||
"pattern": "\\QMETA-INF/schemas/nacos.xsd\\E" | ||
} | ||
] | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
nacos-spring-context-aot/src/main/resources/META-INF/spring/aot.factories
This file contains 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,2 @@ | ||
org.springframework.beans.factory.aot.BeanRegistrationAotProcessor=\ | ||
com.alibaba.nacos.spring.aot.NacosAnnotationBeanRegistrationAotProcessor |
Oops, something went wrong.