1. 工具类
@Component
public class SpringBeanUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringBeanUtils.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static T getBean(Class clazz) {
return applicationContext.getBean(clazz);
}
public static Object getBean(String name) {
return applicationContext.getBean(name);
}
public static Map getBeansOfType(Class clazz) {
return applicationContext.getBeansOfType(clazz);
}
public static List getBeans() {
String[] beanDefinitionNames = getApplicationContext().getBeanDefinitionNames();
List beanNames = Arrays.stream(beanDefinitionNames)
.filter(str -> !str.contains("."))
.collect(Collectors.toList());
beanNames.sort(String::compareTo);
return beanNames;
}
}
2. 获取 Bean 和方法
@RestController
public class SpringBeansController {
@RequestMapping("/getBeans")
public List getBeans() {
return SpringBeanUtils.getBeans();
// return JSON.toJSONString(beanNames, SerializerFeature.PrettyFormat);
}
@RequestMapping("/getBeanByName")
public EnvBean getBeanByName(String name) {
return (EnvBean) SpringBeanUtils.getBean(name);
}
@RequestMapping("/getBeanByClass")
public String getBeanByClass() {
// expected single matching bean but found 2: bizEnvBean,sysEnvBean
// EnvBean bean = SpringBeanUtils.getBean(EnvBean.class);
Map beansOfType = SpringBeanUtils.getBeansOfType(EnvBean.class);
return JSON.toJSONString(beansOfType);
}
/**
* 测试 Bean 名称
*/
@RequestMapping("/getBeanName")
public String getBeanName(String name) {
return Introspector.decapitalize(name);
}
@RequestMapping("/getMethods")
public List getMethods(String beanName) {
List list = new ArrayList();
Object beanObj = SpringBeanUtils.getBean(beanName);
Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(beanObj.getClass());
for (Method method : methods) {
String methodSign = method.toString();
list.add(method.getName() + methodSign.substring(methodSign.indexOf("("), methodSign.indexOf(")") + 1));
}
return list;
}
}
@Service
// @Service("greetService")
public class GreetServiceImpl implements GreetService {
@Override
public String greeting(UserDTO userDTO, String env) {
return "greeting:" + userDTO.toString() + "; env=" + env;
}
@Override
public String greet() {
return "greet...";
}
@Override
public String greet(String name) {
return "Hello " + name;
}
}
3. BeanUtils.populate() 方法
commons-beanutils commons-beanutils1.9.4
/**
* 方法调用
*/
@RequestMapping("/methodDemo")
public String methodDemo() throws Exception {
String beanName = "greetServiceImpl";
String methodName = "greeting(com.example.concrete.common.domain.UserDTO,java.lang.String)";
Map paramMap = new HashMap();
paramMap.put("com.example.concrete.common.domain.UserDTO", "{"name":"Tinyspot", "age":20}");
paramMap.put("java.lang.String", "dev");
Object proxyObj = SpringBeanUtils.getBean(beanName);
Method proxyMethod = ApacheBeanUtils.getMethod(proxyObj.getClass(), methodName);
List
工具类
public class ApacheBeanUtils {
private static final List PRIMARY_CLASS = Arrays.asList(Integer.class, Boolean.class,
Double.class, Byte.class, Short.class, Long.class, Float.class, BigDecimal.class, String.class);
/**
* @param proxyObject
* @param methodName 带参数类型的方法名,比如 greet(java.lang.String)
* @return
*/
public static Method getMethod(Class> proxyObject, String methodName) {
Method[] methods = proxyObject.getMethods();
for (Method method : methods) {
String methodSign = method.toString();
if (methodSign.contains(methodName)) {
return method;
}
}
return null;
}
/**
* 获取方法实际参数
*/
public static List
4. ReflectionUtils 工具类
@RequestMapping("/invokeMethod")
public String invokeMethod() {
Object beanObj = SpringBeanUtils.getBean("greetServiceImpl");
// org.springframework.util.ReflectionUtils;
// 无参方法
Method greet = ReflectionUtils.findMethod(beanObj.getClass(), "greet");
Object greetResult = ReflectionUtils.invokeMethod(greet, beanObj);
// 有参方法
Method method = ReflectionUtils.findMethod(beanObj.getClass(), "greet", String.class);
Object result = ReflectionUtils.invokeMethod(method, beanObj, "Tinyspot");
return greetResult.toString() + result.toString();
}
其他待定
【信息由网络或者个人提供,如有涉及版权请联系COOY资源网邮箱处理】
© 版权声明
部分内容为互联网分享,若有侵权请联系站长删除。
THE END
暂无评论内容