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

Why is reflection used in the PropertiesUtil method? #11231

Closed
stone-98 opened this issue Oct 8, 2023 · 2 comments · Fixed by #11240
Closed

Why is reflection used in the PropertiesUtil method? #11231

stone-98 opened this issue Oct 8, 2023 · 2 comments · Fixed by #11240
Labels
kind/enhancement Category issues or prs related to enhancement.

Comments

@stone-98
Copy link
Contributor

stone-98 commented Oct 8, 2023

public static <T> T handleSpringBinder(Environment environment, String prefix, Class<T> targetClass)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, ClassNotFoundException {
Class<?> binderClass = Class.forName("org.springframework.boot.context.properties.bind.Binder");
Method getMethod = binderClass.getDeclaredMethod("get", Environment.class);
Method bindMethod = binderClass.getDeclaredMethod("bind", String.class, Class.class);
Object binderObject = getMethod.invoke(null, environment);
String prefixParam = prefix.endsWith(".") ? prefix.substring(0, prefix.length() - 1) : prefix;
Object bindResultObject = bindMethod.invoke(binderObject, prefixParam, targetClass);
Method resultGetMethod = bindResultObject.getClass().getDeclaredMethod("get");
return (T) resultGetMethod.invoke(bindResultObject);
}

Wouldn't it be better to replace it this way?

    public static <T> T handleSpringBinder(Environment environment, String prefix, Class<T> targetClass)
            throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, ClassNotFoundException {
        String prefixParam = prefix.endsWith(".") ? prefix.substring(0, prefix.length() - 1) : prefix;
        Binder binder = Binder.get(environment);
        return binder.bind(prefixParam, targetClass).orElse(null);
    }
@KomachiSion
Copy link
Collaborator

应该是旧版本的spring没有暴露出来这个接口,或者是这个方法直接调用会有校验导致报错,所以通过反射来操作。

如果新版本spring可以直接使用这个方法了, 可以进行优化修改。建议修改完之后做一个完整的测试。

目前会使用这个方法的主要是插件体系,可以拿鉴权插件来修改试一下。

@KomachiSion KomachiSion added the kind/enhancement Category issues or prs related to enhancement. label Oct 9, 2023
@stone-98
Copy link
Contributor Author

stone-98 commented Oct 9, 2023

@i will solve it@

stone-98 added a commit to stone-98/nacos that referenced this issue Oct 9, 2023
lowezheng added a commit to lowezheng/nacos that referenced this issue Nov 12, 2023
* develop: (137 commits)
  Develop refactor topn (alibaba#11352)
  Develop fill ut common (alibaba#11335)
  Optimize MapperManager (alibaba#11195)
  Simplify the validate method for serviceinfo (alibaba#11312)
  add startup conditions (alibaba#11305)
  Fix some typos (alibaba#11269)
  fix: rename tar name (alibaba#11281)
  优化节点显示,添加mode (alibaba#11275)
  [ISSUE alibaba#11253]To fix the triggering of the listener upon failover con… (alibaba#11254)
  Add description for new plugin. (alibaba#11268)
  Upgrade to 2.3.0-BETA. (alibaba#11262)
  [ISSUE alibaba#11255]Update PathEncoderManagerTest#testEncodeWithNonExistOs. (alibaba#11256)
  Fill ut for common module (alibaba#11247)
  Optimize ThreadPoolManager (alibaba#11206)
  Fix npe when setup ack response in GrpcClient (alibaba#11210)
  [ISSUE#11192] batchRegisterInstance add recalculateRevision (alibaba#11232)
  feat(alibaba#11236): Remove invalid assertion in com.alibaba.nacos.config.server.service.ConfigCacheService#dumpChange. (alibaba#11237)
  feat(11115): http support cas publish. (alibaba#11120)
  [ISSUE alibaba#11231]Optimize the handleSpringBinder method in PropertiesUtil. (alibaba#11240)
  ISSUE alibaba#11212 (alibaba#11213)
  ...

# Conflicts:
#	console-ui/package-lock.json
#	console-ui/package.json
#	console-ui/src/pages/Login/Login.jsx
#	core/src/main/java/com/alibaba/nacos/core/listener/StartingApplicationListener.java
#	pom.xml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement Category issues or prs related to enhancement.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants