Skip to content

Commit

Permalink
Merge pull request #317 from charle004/master
Browse files Browse the repository at this point in the history
 @space property configurable supported
  • Loading branch information
CorvusYe authored Sep 4, 2024
2 parents 21f1870 + e8140c7 commit 2946f93
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 3 deletions.
36 changes: 36 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,42 @@ This source code is licensed under Apache 2.0 License.
Route-Tag: abc
```
- feat: @Space annotation supports dynamic configuration.
> @Space 注解的 name 属性值可通过 spring 配置文件自定义配置。
- example:
```yaml
app:
person:
space: PERSON_SPACE
```
```java
@Space(name = "${nebula.space}")
@Table(name = "person")
public class Person {
@Id
private String vid;
private String name;

public String getVid() {
return vid;
}

public void setVid(String vid) {
this.vid = vid;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
```

# 1.2.2

## Bugfix
Expand Down
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
<name>Ozjq</name>
<email>[email protected]</email>
</developer>
<developer>
<name>charle004</name>
<email>[email protected]</email>
</developer>
</developers>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private void setBeans(ConfigurableListableBeanFactory beanFactory) {
}

public MapperContext mapperContext(NebulaPool nebulaPool) {
DaoResourceLoader daoBasicResourceLoader = new DaoResourceLoader(parseCfgProps);
DaoResourceLoader daoBasicResourceLoader = new DaoResourceLoader(parseCfgProps, this.context);
MapperContext context = MapperContext.newInstance();
context.setResourceRefresh(parseCfgProps.isResourceRefresh());
context.setNgbatisConfig(nebulaJdbcProperties.getNgbatis());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.nebula.contrib.ngbatis.config.ParseCfgProps;
import org.nebula.contrib.ngbatis.exception.ResourceLoadException;
import org.nebula.contrib.ngbatis.proxy.NebulaDaoBasic;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.Resource;

/**
Expand All @@ -31,6 +32,10 @@ public DaoResourceLoader(ParseCfgProps parseConfig) {
super(parseConfig);
}

public DaoResourceLoader(ParseCfgProps parseConfig, ApplicationContext applicationContext) {
super(parseConfig, applicationContext);
}

/**
* 加载基类接口所需 nGQL 模板
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.nebula.contrib.ngbatis.utils.ReflectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.data.repository.query.Param;
Expand All @@ -63,6 +64,7 @@ public class MapperResourceLoader extends PathMatchingResourcePatternResolver {

private static Logger log = LoggerFactory.getLogger(MapperResourceLoader.class);
protected ParseCfgProps parseConfig;
protected ApplicationContext applicationContext;

private MapperResourceLoader() {
super();
Expand All @@ -72,6 +74,11 @@ public MapperResourceLoader(ParseCfgProps parseConfig) {
this.parseConfig = parseConfig;
}

public MapperResourceLoader(ParseCfgProps parseConfig,ApplicationContext applicationContext) {
this.parseConfig = parseConfig;
this.applicationContext = applicationContext;
}

/**
* 加载多个开发者自建的 XXXDao.xml 资源。
*
Expand Down Expand Up @@ -148,14 +155,35 @@ private void setClassModelBySpaceAnnotation(ClassModel cm) {
}
String spaceClassName = genericTypes[0].getTypeName();
Space annotation = Class.forName(spaceClassName).getAnnotation(Space.class);
if (null != annotation && !annotation.name().equals("")) {
cm.setSpace(annotation.name());
if(null != annotation){
String spaceName = tryResolvePlaceholder(annotation.name());
if (!spaceName.equals("")) {
cm.setSpace(spaceName);
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

/**
* 利用Spring Environment 解析注解的值,用于 @Space 的 name 属性解析
* @param value 需要解析的值,可能是带占位符的 ${xx.xx} ,也可以是固定的字符串
* @return resolveResult 解析结果
* @throws IllegalArgumentException 当配置了 ${xx.xx} 占位符,且spring配置文件中未指定该配置时抛出
*/
private String tryResolvePlaceholder(String value){
String resolveResult = value;
if (null != applicationContext) {
try {
resolveResult = applicationContext.getEnvironment().resolveRequiredPlaceholders(value);
} catch (IllegalArgumentException e) {
throw new ResourceLoadException("name ( "+ value +" ) of @Space missing configurable value");
}
}
return resolveResult;
}

/**
* 解析 一个 XXXDao 的多个方法。
*
Expand Down

0 comments on commit 2946f93

Please sign in to comment.