SpringAop学习

Spring Aop (jdk动态代理和cglib代理)

 

Aop 的概念

aop即面向切面编程,一般解决具有横切面性质的体统(事务,缓存,安全)

JDK动态代理:

可以使用实现proxy 类,实现jdk的动态代理

步骤

1.创建目标接口

 

1 package com;
2 
3 public interface IPerson {
4 
5     public void print();
6 }
View Code

 

2.实现目标接口

 

package com;

public class Person implements IPerson {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Person() {
        super();
    }

    public Person(String name) {
        super();
        this.name = name;
    }

    @Override
    public void print() {
        System.out.println("Person.print()");
    }

}
View Code

 

3.创建代理工厂类

1.实现实现 invcationHandler 接口

2,申明一个对象作为代理的属性

3.申明一个createObject方法 返回一个代理对象 (Proxy.newProxyInstance("获得属性对象的类加载",“获得属性对象的接口结合”,this 当前对象));

4.invoke方法,对此对象进行处理 参数(Proxy ,method,Object o)注:Proxy代理对象,method 对象的方法,Object 为对象的参数列表

 1 package com;
 2 
 3 import java.lang.reflect.InvocationHandler;
 4 import java.lang.reflect.Method;
 5 import java.lang.reflect.Proxy;
 6 
 7 public class Proxys implements InvocationHandler{
 8 
 9     private Object p;
10     public Object createObject(Object p){
11         this.p=p;
12         return Proxy.newProxyInstance(p.getClass().getClassLoader(), p.getClass().getInterfaces(), this);
13     }
14     
15     @Override
16     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
17         Person person=(Person)p;
18         Object obj=null;
19         if(person.getName()!=null){
20             System.out.println(person.getName());
21             obj=method.invoke(p, args);
22         }else{
23             System.out.println("名字为空,被拦截");
24         }
25         return obj;
26     }
27 
28 }
View Code

 一个简单的

public interface OtherBean{

  void f1();

}

public class OtherBeanService implements OtherBean{

  private Object obj;

  public OtherBeanService(Object obj){

    this.obj=obj;

  }

  public Object invoke(Proxy proxy,Method method,Object args){

    System.out.println("代理方法");

    method.invoke(obj,args)

    return null;

  }

}

public class Test{

  public static void main(String args[]){

    

    OtherBean otherBean=new OtherBeanService();

    ProxyUser proxyUser=new ProxyUser(otherBean);

    OtherBean ob=(OtherBean)Proxy.newProxyInsance(otherbean.getClass.getClassLoader,otherBean.getClass.Interfaces,proxyUser);

    ob.print();

  }

}

利用CGLIB实现AOP功能

 此方式不用有接口,导入jar包

1.先导入这两个jar 包

asm-3.3.1.jar
cglib-2.2.2.jar

2.写业务类

public class Student{

  private String name;

  public Student(String name){

    this.name=name; 

  }

  public void print(){

    System.out.println("print");

  }

}

//

public gig implement MethodIntercepter{

  private Object obj;

  public gig(Object obj){

    this.obj=obj;

  }

  

  public Object intercept(Method method,Object [] obj,object args){//具体参数不记得了

    System.out.println("代理方法!")

  }

}

然后测试:省略!

spring 的aop进行登录,并且获取登录用时

 

贴代码

 

posted on 2016-12-31 19:36  老邱2  阅读(180)  评论(0编辑  收藏  举报

导航