Java 为什么我们使用反射创建对象
我正在浏览反射API,但无法讲述在实时场景中使用反射创建对象/在实时应用程序中使用用例。是否有人使用反射来创建使用反射的ObjeCt?在哪个用例中?以下是几个用例: 1如果您希望实现的灵活性,并且不希望使用依赖项注入框架,则可以使用在应用程序启动时定义的系统属性来指定要使用的实现类。JAXP为此使用了许多系统属性,如javax.xml.parsers.DocumentBuilderFactory 例如,如果您有一个名为AutoRideService的接口/抽象类,并且希望能够配置运行时使用的实现,那么您可以得到如下结果:Java 为什么我们使用反射创建对象,java,reflection,Java,Reflection,我正在浏览反射API,但无法讲述在实时场景中使用反射创建对象/在实时应用程序中使用用例。是否有人使用反射来创建使用反射的ObjeCt?在哪个用例中?以下是几个用例: 1如果您希望实现的灵活性,并且不希望使用依赖项注入框架,则可以使用在应用程序启动时定义的系统属性来指定要使用的实现类。JAXP为此使用了许多系统属性,如javax.xml.parsers.DocumentBuilderFactory 例如,如果您有一个名为AutoRideService的接口/抽象类,并且希望能够配置运行时使用的实现
private static AutoRideService lookupAutoRideService() {
String className = System.getProperty("autorideservice.classname");
try {
@SuppressWarnings("unchecked")
Class<AutoRideService> clazz = (Class<AutoRideService>) Class.forName(className);
return clazz.newInstance();
} catch (Exception ex) {
throw new IllegalStateException("Failed to lookup auto ride service using class name: " + className, ex);
}
}
2如果您需要在同一程序中使用不同版本和不兼容的类。在这里,您可以使用多个类装入器和反射
例如,我在数据库中存储了一些Oauth令牌,它们是由旧版本的SpringSecurityOAuth2创建的。我想升级SpringSecurityOAuth2版本和存储的令牌,但新版本对反序列化令牌所需的类进行了重大的向后不兼容的更改。下面是一段代码:
/**
* This takes serialization data of the old authentication object and then
* reads it as an object using a separate class loader which has the old version of the spring and spring security classes
* We then use reflection to access the compatible fields and then use this data to construct the new class version object
* @param oldAuthData The serialized data created with the old version of the classes
* @return The deserialized OAuth2Authentication object constructed with the new class versions
* @throws IOException
* @throws ClassNotFoundException
* @throws NoSuchMethodException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
private OAuth2Authentication deserializeOAuth2Authentication(byte[] oldAuthData) throws IOException, ClassNotFoundException, NoSuchMethodException,
IllegalAccessException, InvocationTargetException {
OAuth2Authentication auth = null;
ObjectInputStream ois = new CustomObjectInputStream(new ByteArrayInputStream(oldAuthData), this.deserialisationClassLoader);
try {
Object obj = ois.readObject();
// the instance of this is the old OAuth2Authentication however we cannot cast
// so have to use reflection to access the fields and data
// and then construct a new OAuth2Authentication from this
Object oldAuthorizationRequest = MethodUtils.invokeMethod(obj, "getAuthorizationRequest", NO_ARGS);
Object authentication = MethodUtils.invokeMethod(obj, "getUserAuthentication", NO_ARGS);
Object principal = MethodUtils.invokeMethod(authentication, "getPrincipal", NO_ARGS);
Object credentials = MethodUtils.invokeMethod(authentication, "getCredentials", NO_ARGS);
Collection<GrantedAuthority> authorities = convertAuthorities((Collection<?>) MethodUtils.invokeMethod(authentication, "getAuthorities", NO_ARGS));
// now construct the oauth authentication object with the new auth and request
Authentication authToken = new UsernamePasswordAuthenticationToken(principal, credentials, authorities);
AuthorizationRequest authReq = convertAuthorizationRequest(oldAuthorizationRequest);
auth = new OAuth2Authentication(authReq, authToken);
} finally {
ois.close();
}
return auth;
}
标记反射有14652个问题,所以我猜有人在某些上下文中使用了这个api。。。只需检查标记的问题。Object o=clazz.getConstructor.newInstance