【JAVAWEB学习笔记】25_基础加强:类加载器、注解 @xxx和动态代理

基础加强

学习目标

案例 - 自定义单元测试 @MyTest

案例 - 全局的编码的解决

 

 

一、类加载器

1.什么是类加载器,作用是什么?

类加载器就加载字节码文件 (.class)

 

2.类加载器的种类

类加载器有三种,不同类加载器加载不同的

 

1)BootStrap:引导类加载器:加载都是最基础的文件

2)ExtClassLoader:扩展类加载器:加载都是基础的文件

3)AppClassLoader:应用类加载器:三方 jar 包和自己编写 java 文件

 

怎么获得类加载器?(重点)

ClassLoader 字节码对象.getClassLoader();

public class Demo {

public static void main(String[] args) {

  </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获得Demo字节码文件的类加载器</span>
Class clazz = Demo.class;//获得 Demo 的字节码对象 ClassLoader classLoader = clazz.getClassLoader();//获得类加载器
  </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">getResource的参数路径相对classes(src)

  </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获得classes(src)下的任何的资源</span>
String path = classLoader.getResource("com/itheima/classloader/jdbc.properties").getPath();
  </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">classLoader.getResourceAsStream("");</span>
System.out.println(path);

}

}

 

 

 

二、注解 @xxx

1.什么是注解,注解作用

注解就是符合一定格式的语法 @xxxx

注解作用:

注释:在阅读程序时清楚 ---- 给程序员看的

注解:给 jvm 看的,给机器看的

 

注解在目前而言最主流的应用:代替配置文件

关于配置文件与注解开发的优缺点:

注解优点:开发效率高 成本低 

注解缺点:耦合性大 并且不利于后期维护

           企业的趋势:混合使用,不经常修改的可以用注解的形式。

 

2.jdk5 提供的注解

@Override:告知编译器此方法是覆盖父类的

@Deprecated:标注过时

@SuppressWarnings:压制警告

      deprecation, 忽略过时

      unused, 忽略不使用

      rawtypes, 忽略类型安全

all, 忽略所有

      …..

发现的问题:

不同的注解只能在不同的位置使用 (方法上、字段上、类上)

 

3.自定义注解(了解)

1)怎样去编写一个自定义的注解

2)怎样去使用注解

3)怎样去解析注解 ----- 使用反射知识

 

(1) 编写一个注解

关键字:@interface

注解的属性:

语法:返回值 名称 ();

注意:如果属性的名字是 value,并且注解的属性值有一个 那么在使用注解时可以省略 value

注解属性类型只能是以下几种

1. 基本类型

                 2.String

                 3. 枚举类型

                 4. 注解类型

                 5.Class 类型

                 6. 以上类型的一维数组类型

(2) 使用注解

在类 / 方法 / 字段 上面是 @XXX

(3) 解析使用了注解的类

介入一个概念:元注解:代表修饰注解的注解,作用:限制定义的注解的特性

@Retention

SOURCE: 注解在源码级别可见

CLASS:注解在字节码文件级别可见

RUNTIME:注解在整个运行阶段都可见

@Target

代表注解修饰的范围:类上使用,方法上使用,字段上使用

FIELD: 字段上可用此注解

METHOD: 方法上可以用此注解

TYPE: 类 / 接口上可以使用此注解

注意:要想解析使用了注解的类 , 那么该注解的Retention 必须设置成 Runtime

 

关于注解解析的实质:从注解中解析出属性值

 

字节码对象存在于获得注解相关的方法

isAnnotationPresent(Class<? extends Annotation> annotationClass) 判断该字节码对象身上是否使用该注解了

getAnnotation(Class<A> annotationClass) :获得该字节码对象身上的注解对象

 

4.案例 - 自定义单元测试 @MyTest

public class MyTestParster {

public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {

  </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获得TestDemo</span>
Class clazz = TestDemo.class;
  </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获得所有的方法</span>
Method[] methods = clazz.getMethods();
  </span><span style="color: rgba(0, 0, 255, 1)">if</span>(methods!=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){

     </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获得注解使用了@MyTest的方法</span>

     <span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)">(Method method:methods){

        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">判断该方法是否使用了@MyTest注解</span>

        <span style="color: rgba(0, 0, 255, 1)">boolean</span> annotationPresent = method.isAnnotationPresent(MyTest.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);

        </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(annotationPresent){

            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">该方法使用MyTest注解了</span>
method.invoke(clazz.newInstance(), null);
        }

     }

  } 

}

}

 

三、动态代理

1.什么是代理 (中介)

目标对象 / 被代理对象 ------ 房主:真正的租房的方法

代理对象 ------- 黑中介:有租房子的方法(调用房主的租房的方法)

执行代理对象方法的对象 ---- 租房的人

 

流程:我们要租房 -----> 中介(租房的方法)------> 房主(租房的方法)

抽象:调用对象 -----> 代理对象 ------> 目标对象

2.动态代理

动态代理:不用手动编写一个代理对象,不需要一一编写与目标对象相同的方法,这个过程,在运行时 的内存中动态生成代理对象。------ 字节码对象级别的代理对象

 

动态代理的 API:

在 jdk 的 API 中存在一个 Proxy 中存在一个生成动态代理的的方法 newProxyInstance

static Object

newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)

 

返回值:Object 就是代理对象

参数:loader:代表与目标对象相同的类加载器 ------- 目标对

象.getClass().getClassLoader()

interfaces:代表与目标对象实现的所有的接口字节码对象数组

h:具体的代理的操作,InvocationHandler 接口

 

注意:JDK 的 Proxy 方式实现的动态代理 目标对象必须有接口 没有接口不能实现 jdk 版动态代理

 

例子一:

public class ProxyTest {

@Test

public void test1(){

  </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获得动态的代理对象----在运行时 在内存中动态的为Target创建一个虚拟的代理对象

  </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">objProxy是代理对象 根据参数确定到底是谁的代理对象</span>
TargetInterface objProxy = (TargetInterface) Proxy.newProxyInstance(
        Target.</span><span style="color: rgba(0, 0, 255, 1)">class</span>.getClassLoader(), <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">与目标对象相同的类加载器</span>

        <span style="color: rgba(0, 0, 255, 1)">new</span> Class[]{TargetInterface.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">},

        </span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> InvocationHandler() {

            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">invoke 代表的是执行代理对象的方法</span>
@Override
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">method:代表目标对象的方法字节码对象

            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">args:代表目标对象的响应的方法的参数</span>

            <span style="color: rgba(0, 0, 255, 1)">public</span> Object invoke(Object proxy, Method method, Object[] args) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> Throwable {

               System.out.println(</span>"目标方法前的逻辑"<span style="color: rgba(0, 0, 0, 1)">);

               </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">执行目标对象的方法</span>
Object invoke = method.invoke(new Target(), args);
               System.out.println(</span>"目标方法后的逻辑"<span style="color: rgba(0, 0, 0, 1)">);

               </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> invoke;

            }

        }

     );

 

  objProxy.method1();

  String method2 </span>=<span style="color: rgba(0, 0, 0, 1)"> objProxy.method2();

  System.out.println(method2);

}

}

 

 

例子二:

public class ProxyTest2 {

public static void main(String[] args) {

  </span><span style="color: rgba(0, 0, 255, 1)">final</span> Target target = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Target();

 

  </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">动态创建代理对象</span>
  TargetInterface proxy </span>=<span style="color: rgba(0, 0, 0, 1)"> (TargetInterface) Proxy.newProxyInstance(

        target.getClass().getClassLoader(),

        target.getClass().getInterfaces(),

        </span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> InvocationHandler() {

            @Override

            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">被执行几次?------- 看代理对象调用方法几次

            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">代理对象调用接口相应方法 都是调用invoke</span>

            <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">

             * proxy:是代理对象

             * method:代表的是目标方法的字节码对象

             * args:代表是调用目标方法时参数

             </span><span style="color: rgba(0, 128, 0, 1)">*/</span>

            <span style="color: rgba(0, 0, 255, 1)">public</span> Object invoke(Object proxy, Method method, Object[] args) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> Throwable {

               </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">反射知识点</span>
Object invoke = method.invoke(target, args);//目标对象的相应方法
               </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">retrun返回的值给代理对象</span>

               <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> invoke;

            }

        }

     );

 

  proxy.method1();</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">调用invoke---Method:目标对象的method1方法  args:null  返回值null</span>
String method2 = proxy.method2();//调用 invoke---Method: 目标对象的 method2 方法 args:null 返回值 method2
  <span style="color: rgba(0, 0, 255, 1)">int</span> method3 = proxy.method3(100);<span style="color: rgba(0, 128, 0, 1)">////</span><span style="color: rgba(0, 128, 0, 1)">调用invoke-----Method:目标对象的method3方法 args:Object[]{100}  返回值100</span>
  System.out.println(method2);

  System.out.println(method3);

}
}

 

 

3.案例 - 全局的编码的解决

 

public class EncodingFilter implements Filter{

@Override

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)

     </span><span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> IOException, ServletException {

 

  </span><span style="color: rgba(0, 0, 255, 1)">final</span> HttpServletRequest req =<span style="color: rgba(0, 0, 0, 1)"> (HttpServletRequest) request;

 

  </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">使用动态代理完成全局编码</span>
HttpServletRequest enhanceRequset = (HttpServletRequest) Proxy.newProxyInstance(
        req.getClass().getClassLoader(),

        req.getClass().getInterfaces(),

        </span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> InvocationHandler() {

            @Override

            </span><span style="color: rgba(0, 0, 255, 1)">public</span> Object invoke(Object proxy, Method method, Object[] args) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> Throwable {

               </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">对getParameter方法进行增强</span>
String name = method.getName();//获得目标对象的方法名称
               <span style="color: rgba(0, 0, 255, 1)">if</span>("getParameter"<span style="color: rgba(0, 0, 0, 1)">.equals(name)){

                  String invoke </span>= (String) method.invoke(req, args);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">乱码

                  </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">转码</span>
invoke = new String(invoke.getBytes("iso8859-1"),"UTF-8");
                  </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> invoke;
               }

               </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> method.invoke(req, args);
            }
        }
     );


  chain.doFilter(enhanceRequset, response);


  </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">request.setCharacterEncoding("UTF-8");

 

  </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">在传递request之前对request的getParameter方法进行增强</span>

  <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">

   * 装饰者模式(包装)

   *

   * 1、增强类与被增强的类要实现统一接口

   * 2、在增强类中传入被增强的类

   * 3、需要增强的方法重写 不需要增强的方法调用被增强对象的

   *

   </span><span style="color: rgba(0, 128, 0, 1)">*/</span>

 

  <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">被增强的对象

  </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">HttpServletRequest req = (HttpServletRequest) request;

  </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">增强对象

  </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">EnhanceRequest enhanceRequest = new EnhanceRequest(req);


  </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">chain.doFilter(enhanceRequest, response);</span>

}

@Override

public void destroy() {
}
@Override

public void init(FilterConfig filterConfig) throws ServletException {
}
}

class EnhanceRequest extends HttpServletRequestWrapper{

private HttpServletRequest request;

public EnhanceRequest(HttpServletRequest request) {

  </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">(request);

  </span><span style="color: rgba(0, 0, 255, 1)">this</span>.request =<span style="color: rgba(0, 0, 0, 1)"> request;

}

//对 getParaameter 增强

@Override

public String getParameter(String name) {

  String parameter </span>= request.getParameter(name);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">乱码</span>

  <span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {

     parameter </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> String(parameter.getBytes("iso8859-1"),"UTF-8"<span style="color: rgba(0, 0, 0, 1)">);

  } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (UnsupportedEncodingException e) {

     e.printStackTrace();

  }

  </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> parameter;

}

}