@Service
public class ReflectionService {
@Resource
private ApplicationContext applicationContext;
private static final List<Class> WRAP_CLASS = Arrays.asList(Integer.class, Boolean.class, Double.class,Byte.class,Short.class, Long.class, Float.class, Double.class, BigDecimal.class, String.class);
/**
* 反射调用spring bean方法的入口
* @param classz 类名
* @param methodName 方法名
* @param paramMap 实际参数
* @throws Exception
*/
public void invokeService(String classz, String methodName, Map<String,Object> paramMap) throws Exception {
if(!applicationContext.containsBean(classz)) {
throw new RuntimeException("Spring找不到对应的Bean");
}
// 从Spring中获取代理对象(可能被JDK或者CGLIB代理)
Object proxyObject = applicationContext.getBean(classz);
// 获取代理对象执行的方法
Method method = getMethod(proxyObject.getClass(), methodName);
// 获取
SpringBoot 从入门到精通- Spring通过反射调用bean中方法
最新推荐文章于 2024-05-14 19:38:58 发布