Skip to content
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

@Space property configurable supported #317

Merged
merged 3 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading