本文整理汇总了Java中org.springframework.aop.TargetSource.getTargetClass方法的典型用法代码示例。如果您正苦于以下问题:Java TargetSource.getTargetClass方法的具体用法?Java TargetSource.getTargetClass怎么用?Java TargetSource.getTargetClass使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.aop.TargetSource
的用法示例。
在下文中一共展示了TargetSource.getTargetClass方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getProxy
import org.springframework.aop.TargetSource; //导入方法依赖的package包/类
/**
* Create a proxy for the specified {@code TargetSource} that extends
* the target class of the {@code TargetSource}.
* @param targetSource the TargetSource that the proxy should invoke
* @return the proxy object
*/
public static Object getProxy(TargetSource targetSource) {
if (targetSource.getTargetClass() == null) {
throw new IllegalArgumentException("Cannot create class proxy for TargetSource with null target class");
}
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setTargetSource(targetSource);
proxyFactory.setProxyTargetClass(true);
return proxyFactory.getProxy();
}
示例2: invokeMethod
import org.springframework.aop.TargetSource; //导入方法依赖的package包/类
/**
* spring注入对象的私有方法调用
*
* @param owner
* 注入的对象
* @param methodName
* 私有方法名
* @param parameterTypes
* 私有方法参数类型
* @param parameters
* 私有方法参数
* @return 私有方法返回值
*/
@SuppressWarnings({ "rawtypes" })
public static Object invokeMethod(final Object owner,
final String methodName, final Class[] parameterTypes,
final Object[] parameters) throws Exception {
// get class
final Class ownerclass = owner.getClass();
// get property
try {
@SuppressWarnings("unchecked")
final Method getTargetClass = ownerclass
.getMethod("getTargetSource");
final TargetSource target = (TargetSource) getTargetClass.invoke(
owner, new Object[] {});
final Class targetClass = target.getTargetClass();
@SuppressWarnings("unchecked")
final Method method = targetClass.getDeclaredMethod(methodName,
parameterTypes);
if (!method.isAccessible()) {
method.setAccessible(true);
}
final Object targetInstance = target.getTarget();
return method.invoke(targetInstance, parameters);
} catch (NoSuchMethodException e) {
return invokeMethod(owner, 0, methodName, parameterTypes,
parameters);
// e.printStackTrace();
}
}