一、获取对象类名
Class<?> handlerClass = ClassUtils.getUserClass(handler);
二、查询一个方法上是否有某注解
RequestMapping mapping = AnnotationUtils.findAnnotation(method, RequestMapping.class);
三、 获取实现某一接口的类
BeanFactoryUtils.beansOfTypeIncludingAncestors(context, HandlerMapping.class, true, false);
四、 获取某字符串对应的类对象
Class<?> clazz = ClassUtils.forName(className, this.getClass().getClassLoader());
五、使用spring上下文创建受托管bean
protected Object createDefaultStrategy(ApplicationContext context, Class<?> clazz) {
return context.getAutowireCapableBeanFactory().createBean(clazz);
}
六、 获取某传入对象的类属性,并据此在spring容器中查找它的注册对象名称beanName
getApplicationContext().getBeanNamesForType(Object.class)
七、 根据传入对象类的父代来查询所有spring容器中注册的beanName
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(getApplicationContext(), Object.class)
八、 在spring容器中根据beanName查找出此对象上的对应某注解,注意不是所有注解
ApplicationContext context = getApplicationContext();
RequestMapping mapping = context.findAnnotationOnBean(beanName, RequestMapping.class);
九、 spring用beanName找出此对象的类类型
Class<?> handlerType = context.getType(beanName);
Method methodToInvoke = BridgeMethodResolver.findBridgedMethod(initBinderMethod);
ReflectionUtils.makeAccessible(methodToInvoke);
十、 获取有某注解的对象
String[] beanNames = (this.detectHandlersInAncestorContexts ?
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(getApplicationContext(), Object.class) :
getApplicationContext().getBeanNamesForType(Object.class));//获取所有beanName
for (String beanName : beanNames) {
Class<?> handlerType = context.getType(beanName);
ApplicationContext context = getApplicationContext();
if (AnnotationUtils.findAnnotation(handlerType, Controller.class) != null) {
}
}
十一、获取classpath下的文件
File cfgFile = ResourceUtils.getFile("classpath:test.txt");
或者
org.springframework.core.io.Resource fileRource = new ClassPathResource("test.txt");
获取文件:fileRource.getFile();
获取文件流:fileRource.getInputStream();
十三、资源文件在jar中
org.springframework.core.io.Resource fileRource = new ClassPathResource("test.txt");
获取文件流:fileRource.getInputStream();
十四、Spring collections静态方法
CollectionUtils.mergeArrayIntoCollection