Spring之AOP

1.代理模式

代理模式是AOP的底层

代理模式的分类

静态代理

动态代理

静态代理

抽象角色:一般会使用接口或者抽象类来解决

真是角色:被代理的角色

代理角色:代理真实角色

客户:访问代理对象的人

优点:可以是真实角色操作更加纯粹,不用关注公共yw

​ 公共也就交给代理角色,实现业务分工

​ 拓展业务,方便集中管理

缺点

一个真实橘色产生一个代理角色,代码量会加大

代码步骤:

接口

public interface Rent {
    public void rent();
}

真实角色

public class Host implements Rent{
    public void rent() {
        System.out.println("出租房子");
    }
}

代理角色

public class PROXY implements Rent {
    private Host host;

    public PROXY() {
    }

    public PROXY(Host host) {
        this.host = host;
    }

    public void rent() {
        host.rent();
        hetong();
    }
    public void hetong(){
        System.out.println("看房");
    }
}

客户

public class Client {
    public static void main(String[] args) {
        Host host = new Host();
        PROXY proxy = new PROXY(host);
        proxy.rent();
    }
}

动态代理

动态代理和静态代理角色一样

动态代理类是动态生成的,不是直接写好的

动态代理分为:基于接口,基于类的动态代理

​ 基于接口----JDK动态代理

​ 基于类:cglib

​ java字节码:javasist

学习两个类:proxy,invocationHandler

​ 接口

public interface Rent {
    public void rent();
}

真是角色

public class Host implements Rent, com.yuan.demo1.Rent {
    public void rent() {
        System.out.println("出租房子");
    }
}

生代理类

//使用这个类生成代理类
public class ProxyInvocationHandler implements InvocationHandler {

   private Rent rent;

    public void setRent(Rent rent) {
        this.rent = rent;
    }

    public  Object getproxy(){
     return Proxy.newProxyInstance(this.getClass().getClassLoader(),rent.getClass().getInterfaces(),this);
    }

    //处理代理实例,返回结果
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
      //动态代理本质就是采用反射机制
        Object result = method.invoke(rent,args);
        return result;

    }
}

测试类

public class Client {
    public static void main(String[] args) {
        Host host = new Host();//真实角色
        ProxyInvocationHandler p  = new ProxyInvocationHandler();//还未有代理角色
        //通过调用程序处理角色来处理我们要调用的接口的对象
        p.setRent(host);
        Rent proxy = (Rent) p.getproxy();
        proxy.rent();

    }
}
AOP
实现方式一:

接口

public interface User {
    public void add();
    public void del();
    public void update();
    public void query();
}

实现类

public class UserImpl implements User {
    public void add() {
        System.out.println("jia");
    }

    public void del() {
        System.out.println("shan");

    }

    public void update() {
        System.out.println("gai");

    }

    public void query() {
        System.out.println("cha");
    }
}

applicationCOntext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--注册bean-->
    <bean id="user" class="com.yuan.pojo.UserImpl"/>
    <bean id="log" class="com.yuan.log.log"/>
    <bean id="afterlog" class="com.yuan.log.log"/>
    <!--配置aop-->
    <aop:config>
        <!-- 切入点:expression="excution"(要执行的位置!* * * * )-->
        <aop:pointcut id="pointcut" expression="execution(* com.yuan.pojo.UserImpl.*(..))"/>
        <!--执行环绕增加-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterlog" pointcut-ref="pointcut"/>
    </aop:config>

</beans>

测试类

public class Mytest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //动态代理的是接口
        User user = (User) classPathXmlApplicationContext.getBean("user");
        user.add();
    }
}
方式二

接口

public interface User {
    public void add();
    public void del();
    public void update();
    public void query();
}

实现类

public class UserImpl implements User {
    public void add() {
        System.out.println("jia");
    }

    public void del() {
        System.out.println("shan");

    }

    public void update() {
        System.out.println("gai");

    }

    public void query() {
        System.out.println("cha");
    }
}

applicationContext.xml

<bean id="div" class="com.yuan.div.div"/>
    <aop:config>
        <!--ref要引用的类-->
        <aop:aspect ref="div">
            <!--切入点-->
         <aop:pointcut id="pointcut" expression="execution(* com.yuan.pojo.UserImpl.*(..))"/>
            <aop:before method="before" pointcut-ref="pointcut"/>
            <aop:after method="after" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>

测试类

public class Mytest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //动态代理的是接口
        User user = (User) classPathXmlApplicationContext.getBean("user");
        user.add();
    }
}
public class div {
    public void before(){
        System.out.println("执行前");
    }
    public void after(){
        System.out.println("执行后");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值