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

DS注解切换数据源,对于Spel表达式支持bug #199

Closed
zhoukun921 opened this issue May 18, 2020 · 5 comments
Closed

DS注解切换数据源,对于Spel表达式支持bug #199

zhoukun921 opened this issue May 18, 2020 · 5 comments

Comments

@zhoukun921
Copy link

DynamicDataSourceAnnotationInterceptor.determineDatasource

private String determineDatasource(MethodInvocation invocation) throws Throwable { Method method = invocation.getMethod(); DS ds = method.isAnnotationPresent(DS.class) ? (DS)method.getAnnotation(DS.class) : (DS)AnnotationUtils.findAnnotation(RESOLVER.targetClass(invocation), DS.class); String key = ds.value(); return !key.isEmpty() && key.startsWith("#") ? this.dsProcessor.determineDatasource(invocation, key) : key; }
对表达式检查是否有#前缀

DsSpelExpressionProcessor.doDetermineDatasource

Object value = PARSER.parseExpression(key).getValue(context);
转换表达式时,未指定表达式前缀,导致传递了以#前缀的表达式无法编译通过
更换为如下方式即可解决问题
Object value = PARSER.parseExpression(key, ParserContext.TEMPLATE_EXPRESSION).getValue(context);

@huayanYu
Copy link
Member

不太理解, 测试没有问题啊。 你是要传递什么样的?

@zhoukun921
Copy link
Author

@ds("#{T(java.lant.String).format("datasource_%s",1)}")

你可以试下这个表达式,用最新版本测试这个会报Spel表达式错误,但是如果不用#开头,又不会将这个串做为表达式解析

@huayanYu
Copy link
Member

你这用法太高级。。。 我现在没法改,对其他人影响太大了。你本地打包发布到公司私服用吧。

@ls9527
Copy link
Collaborator

ls9527 commented May 19, 2020

不会啊, 你加一个成员变量, 把 ParserContext.TEMPLATE_EXPRESSION 作为一个对象配置进去就行了。 默认值是null, 这样就可以兼容 #paramObj 和 #{#paramObj} 两种从方法的参数取值的方式了

@huayanYu
Copy link
Member

提交PR吧。我来测

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants