1. jAVA反射
1.1 获取class的方式
//object.class
Class c = student.class;
//class.forname("")
Class c1 = class.forname("src.student")
// 对象.class
Student s = new student();
Class c2 = s.getclass();
1.2 获取构造器
//获取所有的构造器(包括私有的)
Class c = student.class;
Constructor[] constructors = c.getDeclaredConstructors();
//获取无参构造器
Constructor constructor = c.getDeclaredConstructor();
//获取指定参数的构造器
Constructor constructor_parm = c.getConstructor(String.class, int.class);
//使用构造器
Student student = (Student)constructor.newInstance();
1.3 获取方法
public class testMethod {
@Test
public void testmethod() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Class c = student.class;
Method[] methods = c.getMethods();
for (Method method : methods) {
System.out.println(method.getName() + "---->" + method.getReturnType());
}
//获取run的操纵资格
student s1 = new student("小刘",3);
Method run = c.getDeclaredMethod("run");
Method smile = c.getMethod("smile", int.class);
smile.invoke(s1,2);
run.setAccessible(true);
run.invoke(s1);
}
}
1.4 Java反射Demo
完成saveObject 作用是将一个未知的类的信息打印到文件中
public class ObjectFrame {
public static void saveObject(Object o) throws FileNotFoundException, IllegalAccessException {
PrintStream ps = new PrintStream(new FileOutputStream("src/data.txt", true));
Class c = o.getClass();
ps.println("------------------" + c.getSimpleName() +"--------------------");
Field[] fields = c.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
String name = field.getName();
String value = field.get(o) + "";
ps.println(name + ": "+ value);
}
ps.close();
}
public static void main(String[] args) throws FileNotFoundException, IllegalAccessException {
student s1 = new student("小刘",2);
teacher t1 = new teacher("张老师","2","语文");
saveObject(s1);
saveObject(t1);
}
}
2 . JAVA 注解
2.1 Demo 注解
实现我们只运行注解的程序
@Target(ElementType.METHOD)//标记可以在什么上注解
@Retention(RetentionPolicy.RUNTIME) //表示可以一直生效
public @interface MyTest {
}
public class AnnotationTest {
@MyTest
public void test1(){
System.out.println("test1");
}
@MyTest
public void test2(){
System.out.println("test2");
}
//@MyTest
public void test3(){
System.out.println("test3");
}
@MyTest
public void test4(){
System.out.println("test4");
}
public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
AnnotationTest annotationTest = new AnnotationTest();
Class c = AnnotationTest.class;
Method[] methods = c.getMethods();
for (Method method : methods) {
if(method.isAnnotationPresent(MyTest.class)){
method.invoke(annotationTest);
}
}
}
}
3:JAVA 动态代理
这里的具体细节没有掌握,只是学会了如何简单的增强一个方法
public class ProxyUtil {
public static Star createProxy(BigStar bigStar){
//第一个参数是类加载器固定
//第二个参数是Star.class 代表我们要代理那些方法
//第三个参数是控制如何代理invoke
// BigStar s = new BigStar("刘亦菲");
// Star sproxy = ProxyUtil.createProxy(s);
// sproxy.dance();
//当调用sproxy.dance时会转为调用invoke方法 第一个参数就是proxy即sproxy 第二个参数method 即为dance 第三个参数args即dance里传的参数
Star starproxy = (Star) Proxy.newProxyInstance(ProxyUtil.class.getClassLoader(), new Class[]{Star.class}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if(method.getName().equals("sing")) System.out.println("准备唱歌,先收钱20万");
if(method.getName().equals("dance")) System.out.println("准备跳舞,先收钱200万");
return method.invoke(bigStar, args );
}
});
return starproxy;
}
}
3.1 JAVA动态代理Demo
实现了 JAVA统计 用户登录操作耗时的统计
public class ProxyUtil {
public static UserService createProxy(IUserServiceImpl userService){
UserService userproxy = (UserService) Proxy.newProxyInstance(ProxyUtil.class.getClassLoader(), new Class[]{UserService.class},
new InvocationHandler() {
@Override
public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
long starttime = System.currentTimeMillis();
method.invoke(userService, objects);
long endtime = System.currentTimeMillis();
System.out.println("您本次操作耗时为" + (endtime- starttime));
return null;
}
});
return userproxy;
}
}