Day23-反射

发布时间:2026/7/28 16:10:21
Day23-反射 Day23总结JUnit4单元测试jar包的导入和导出资源文件解析设计模式ThreadLocal反射总结我是最棒的基础不牢地动山摇JUnit4单元测试测试类命名测试类Test测试方法命名test测试方法名,修饰符为public返回值类型为void.在方法上面加上Test注解运行时Run as JUnit4其他Before 每次测试一个功能都会先执行After 每次测试一个功能都会最后执行jar包的导入和导出导入在根目录下创建一个lib文件夹然后将jar包拖入lib中再右键点击jar包add to build path红色感叹号代表可能包出错解决方案是全部删除再重新导入导出使用eclipse带的export导出jar文件资源文件解析配置文件配置文件一般用来解决硬编码问题写死的代码主要有xmlproperties等一般先在根目录下创建配置文件的source文件夹传统方式读取配置文件使用Properties类new的方式创建流对象可能因为路径问题导致读取不到配置文件所以我们现在基本不用packagecn.itsource.configuration;importjava.io.FileReader;importjava.util.Properties;publicclassPropertiesTest{publicstaticvoidmain(String[]args){Properties pnewProperties();try(FileReader frnewFileReader(mysql.properties);){p.load(fr);System.out.println(p.getProperty(username));System.out.println(p.getProperty(password));}catch(Exceptione){e.printStackTrace();}}}//控制台打印报错信息因为路径没有写完/* java.io.FileNotFoundException: mysql.properties (系统找不到指定的文件。) at java.io.FileInputStream.open0(Native Method) at java.io.FileInputStream.open(FileInputStream.java:195) at java.io.FileInputStream.init(FileInputStream.java:138) at java.io.FileInputStream.init(FileInputStream.java:93) at java.io.FileReader.init(FileReader.java:58) at cn.itsource.configuration.PropertiesTest.main (PropertiesTest.java:11) */try(FileReader frnewFileReader(db/mysql.properties);)//将路径改成db/mysql.properties就可以读取配置文件中的信息//控制台打印//root//root改进的三种方式通过当前类的字节码文件的方式这种方式需要在路径前面加一个///核心代码PropertiesTest2.class.getResourceAsStream(/mysql.properties);完整代码packagecn.itsource.configuration;importjava.io.InputStream;importjava.util.Properties;publicclassPropertiesTest2{publicstaticvoidmain(String[]args){Properties pnewProperties();try{InputStream isPropertiesTest2.class.getResourceAsStream(/mysql.properties);p.load(is);System.out.println(p.getProperty(username));System.out.println(p.getProperty(password));}catch(Exceptione){e.printStackTrace();}}}通过当前类的加载器的方式这种方式直接写上路径即可//核心代码PropertiesTest4.class.getClassLoader().getResourceAsStream(mysql.properties);完整代码packagecn.itsource.configuration;importjava.io.IOException;importjava.io.InputStream;importjava.util.Properties;/** * 通过字节码对象的类加载器读取配置文件 */publicclassPropertiesTest3{publicstaticvoidmain(String[]args){Properties pnewProperties();InputStream isnull;try{isPropertiesTest4.class.getClassLoader().getResourceAsStream(mysql.properties);p.load(is);System.out.println(p.getProperty(username));System.out.println(p.getProperty(password));}catch(Exceptione){e.printStackTrace();}finally{try{if(is!null){is.close();}}catch(IOExceptione){e.printStackTrace();}}}}通过当前线程类的加载器方式这种方式写上路径即可//核心代码获取当前线程类的加载器Thread.currentThread().getContextClassLoader().getResourceAsStream(mysql.properties);完整代码packagecn.itsource.configuration;importjava.io.IOException;importjava.io.InputStream;importjava.util.Properties;/** * 通过字节码对象的类加载器读取配置文件 */publicclassPropertiesTest4{publicstaticvoidmain(String[]args){Properties pnewProperties();InputStream isnull;try{isThread.currentThread().getContextClassLoader().getResourceAsStream(mysql.properties);p.load(is);System.out.println(p.getProperty(username));System.out.println(p.getProperty(password));}catch(Exceptione){e.printStackTrace();}finally{try{if(is!null){is.close();}}catch(IOExceptione){e.printStackTrace();}}}}设计模式单例模式常用来做一些初始化工作比如日志文件等饿汉模式packagecn.itsource.designpattern;/** * 用单例模式一般都用饿汉模式线程安全并且获取对象效率高 * 可以使用静态代码块的方式来对它进行优化 ,让它不会一直占用空间 */publicclassSingleton{publicstaticfinalinta5;privatestaticfinalSingleton INSTANCE;privateSingleton(){}static{System.out.println(666);INSTANCEnewSingleton();}publicstaticSingletongetInstance(){returnINSTANCE;}}懒汉模式packagecn.itsource.designpattern;publicclassSingletonLazy{privatestaticSingletonLazy instance;/** * 私有化构造方法 */privateSingletonLazy(){}/** * 双重校验锁解决懒汉模式线程安全问题 * return */publicstaticSingletonLazygetInstance(){if(instancenull){synchronized(SingletonLazy.class){if(instancenull){instancenewSingletonLazy();}}}returninstance;}}装饰者模式在不改变原有功能的基础上写一个子类继承他并且加强它的原有功能简单工厂模式优点创建对象和使用对象的功能分离缺点扩展性弱如果添加了新产品则需要修改工厂方法适用于产品较少并且不经常变动适配器模式解决了两个功能的协同工作扩展性强ThreadLocal解决线程安全问题packagecn.itsource.threadlocal;publicclassThreadLocalTest{staticInteger a1;//有线程安全的代码publicstaticvoidmain(String[]args){// new Thread(new Runnable() {// Override// public void run() {// //创建一个ThreadLocal对象将有线程安全的变量类型传入// ThreadLocalInteger local new ThreadLocalInteger();// a 66;// //将有线程安全的变量保存到ThreadLocal对象// local.set(a);// while (true) {// try {// Thread.sleep(200);// } catch (InterruptedException e) {// e.printStackTrace();// }// //取出ThreadLocal对象中保存的变量// System.out.println(新线程local.get());// }// }// }).start();newThread(()-{//创建一个ThreadLocal对象将有线程安全的变量类型传入ThreadLocalIntegerlocalnewThreadLocalInteger();a66;//将有线程安全的变量保存到ThreadLocal对象local.set(a);while(true){try{Thread.sleep(200);}catch(InterruptedExceptione){e.printStackTrace();}//取出ThreadLocal对象中保存的变量System.out.println(新线程local.get());}}).start();ThreadLocalIntegerlocalnewThreadLocalInteger();a99;local.set(a);while(true){try{Thread.sleep(200);}catch(InterruptedExceptione){e.printStackTrace();}System.out.println(main线程local.get());}}}反射通过一个全限定类名动态的获取该类的字节码文件优点提高开发灵活度提高程序的扩展性框架提高开发效率的别人封装好的代码底层都是使用反射技术缺点​ 破坏封装性性能低下如果以后可以不用反射技术就不用反射中常用的方法//获取字节码文件ClassclazzClass.forName(全限定类名);通过字节码文件获取构造方法/* Constructor[] constructors clazz.getConstructors() 获取public修饰的构造方法数组 Constructor[] constructors clazz.getDeclaredConstructors() 获取所有造方法数组 Constructor constructor clazz.getConstructor(Class 参数字节码根据参数类型获取指定的的构造方法 *///通过构造方法对象去用构造方法创建对象 相当于new 一个对象Object instanceconstructor.newInstance(Object 实参);通过字节码文件获取普通方法/* Method[] methods clazz.getMethods() 获取public修饰的普通方法数组 Method[] methods clazz.getDeclaredMethods() 获取所有普通方法数组 Method method clazz.getMethod(String methodName,Class... 参数字节码根据方法名和参数类型获取指定的的方法,如果方法没有形参则Class可变参数不用写 *///调用方法若为静态则对象为null。不为静态对象就是用构造方法创建的。实参根据调用的方法本身来定method.invoke(Object 对象实参);通过字节码文件获取字段/* Field[] fields clazz.getFields() 获取public修饰的字段数组 Field[] fields clazz.getDeclaredFields() 获取所有权限的字段数组 Field field clazz.getDeclearField(String fieldName) 根据字段名获取指定字段 Field field clazz.getField(String fieldName根据字段名获取指定public修饰字段 *///如果字段是private修饰那么我们可以通过破坏封装性来改变它的字段值field.setAccessible(true);//通过当前的字段对象给某一个字段赋值取值field.get(Object obj);field.set(Object obj,Object value);