Today, I am looking the source code of tomcat
in the Bootstrap.init() method, I found it used reflection to create an instance of org.apache.catalina.startup.Catalina, and use invoke() to set the ClassLoader
Like the following code
Class<?> startupClass = catalinaLoader.loadClass("org.apache.catalina.startup.Catalina");
Object startupInstance = startupClass.getConstructor().newInstance();
String methodName = "setParentClassLoader";
Method method =
startupInstance.getClass().getMethod(methodName, paramTypes);
method.invoke(startupInstance, paramValues);
I found that many frameworks use reflections to create an instance, even though the class and method can be determined
Just like the above code, use String to determine the target.
Is it still necessary to use reflection?