博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
黄老师架构师课程笔记(一)反射
阅读量:4326 次
发布时间:2019-06-06

本文共 3569 字,大约阅读时间需要 11 分钟。

起源:

Java刚开始没有现在这么火,Java成功的今天,完全是因为Spring的全部系列,和独有的反射机制。

起初,所有的对象都要new(),当项目越堆越大时,问题来了。

于是有了工厂模式,需要什么对象,就进行使用工厂进行生产。

后来,人们觉得,虽然对象的耦合度低了,但是,工厂的耦合度又高了。

最终,我们生产出一套机制,利用反射进行

getFields与getDeclareFields的区别:

在书写框架时,我们如果使用getDeclareFields,只能拿到类中所有成员,而不能拿到父类的

在使用getFields的时候,我们可以都拿到

解决方案:(递归打印)

public static void printAllFields(Class
clazz){ Field[] fields=clazz.getDeclaredFields(); System.out.println(Arrays.toString(fields)); if(!clazz.getSuperclass().equals(Object.class)){ printAllFields(clazz.getSuperclass()); } }

 

手写一个注入:

public class UserController {    private UserService userService;    public UserService getUserService() {        return userService;    }    public void setUserService(UserService userService) {        this.userService = userService;    }}

//用代码进行注入userService

@Test    public void testField() throws Exception {        UserController userController=new UserController();        Class
clazz=userController.getClass();// Field[] declaredFields=clazz.getDeclaredFields();// Arrays.asList(declaredFields).stream().forEach(System.out::println); Field userServiceField=clazz.getDeclaredField("userService"); UserService userService=new UserService(); userServiceField.setAccessible(true); userServiceField.set(userController,userService); //获取get,set的名字 String name=userServiceField.getName(); name=name.substring(0,1).toUpperCase()+name.substring(1,name.length()); String getMethodName="get"+name; String setMethodName="set"+name; Method method=clazz.getMethod(setMethodName,UserService.class); //在userController注入userService method.invoke(userController,userService); System.out.println(userController.getUserService()); }

//使用注解进行注入

package littlepage.test;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.FIELD})public @interface Autowired {}

//开始注入

package littlepage.test;import org.junit.Test;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.Arrays;import java.util.stream.Stream;public class DemoTest {    @Test    public void testField() throws Exception {        UserController userController=new UserController();        Class
clazz=userController.getClass(); Stream.of(clazz.getDeclaredFields()).forEach(field->{ String name=field.getName(); Autowired annotation=field.getAnnotation(Autowired.class); if(annotation!=null){ field.setAccessible(true); Class
type=field.getType(); try { /** * 这个对象直接newInstance出来的 * 在spring中这个bean是定义在注解或者xml中的,如何找到? */ Object object=type.getConstructor(null).newInstance(); field.set(userController,object); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } }); }}

 

碎觉!明天再刷

 

转载于:https://www.cnblogs.com/littlepage/p/11186695.html

你可能感兴趣的文章
iOS开发网络篇—XML数据的解析
查看>>
[BZOJ4303]数列
查看>>
一般处理程序在VS2012中打开问题
查看>>
C语言中的++和--
查看>>
thinkphp3.2.3入口文件详解
查看>>
POJ 1141 Brackets Sequence
查看>>
Ubuntu 18.04 root 使用ssh密钥远程登陆
查看>>
Servlet和JSP的异同。
查看>>
虚拟机centOs Linux与Windows之间的文件传输
查看>>
ethereum(以太坊)(二)--合约中属性和行为的访问权限
查看>>
IOS内存管理
查看>>
middle
查看>>
[Bzoj1009][HNOI2008]GT考试(动态规划)
查看>>
Blob(二进制)、byte[]、long、date之间的类型转换
查看>>
OO第一次总结博客
查看>>
day7
查看>>
iphone移动端踩坑
查看>>
vs无法加载项目
查看>>
Beanutils基本用法
查看>>
玉伯的一道课后题题解(关于 IEEE 754 双精度浮点型精度损失)
查看>>