aop和动态代理

jdk的动态代理:

1.目标对象

package com.intheima.proxy.jdk;

public class Target implements TargetInterface {
    @Override
    public void save() {
        System.out.println("save running.....");
    }
}

2.接口

package com.intheima.proxy.jdk;

public interface TargetInterface {
    public void save();
}

3.增强的方法

package com.intheima.proxy.jdk;

public class Advice {

    public void before()
    {
        System.out.println("前置增强...");
    }
    public void afterReturning()
    {
        System.out.println("后置增强...");
    }
}

4.测试jdk动态代理

package com.intheima.proxy.jdk.Test;

import com.intheima.proxy.jdk.Advice;
import com.intheima.proxy.jdk.Target;
import com.intheima.proxy.jdk.TargetInterface;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyTest {
    public static void main(String[] args) {
          //创建目标对象
       final Target target=new Target();
        //增强对象
        Advice advice=new Advice();
        //返回值就是动态生成的代理对象
          TargetInterface proxy=(TargetInterface) Proxy.newProxyInstance(
                target.getClass().getClassLoader(),     //目标对象类加载器
                target.getClass().getInterfaces(),     //目标地下相同的接口字节码对象数组
                new InvocationHandler() {
                    //调用代理对象的任何方法 实质执行的都是invoke方法
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        //前置增强
                        advice.before();
                        Object invoke = method.invoke(target, args);//执行目标方法
                        //后置增强
                        advice.afterReturning();
                        return invoke;
                    }
                }
        );
        //调用代理对象的方法
        proxy.save();
    }
}

cglib动态代理

1.目标对象

package com.intheima.proxy.cglib;
public class Target  {

    public void save() {
        System.out.println("save running.....");
    }
}

2.增强的方法

package com.intheima.proxy.cglib;

public class Advice {

    public void before()
    {
        System.out.println("前置增强...");
    }
    public void afterReturning()
    {
        System.out.println("后置增强...");
    }
}

3.方法的测试

package com.intheima.proxy.cglib.Test;

import com.intheima.proxy.jdk.Advice;
import com.intheima.proxy.jdk.Target;
import com.intheima.proxy.jdk.TargetInterface;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyTest {
    public static void main(String[] args) {
          //创建目标对象
       final Target target=new Target();
        //增强对象
        Advice advice=new Advice();
       //返回值就是动态生成懂得代理对象 基本cglib
        //1.创建增强器
        Enhancer enhancer=new Enhancer();
        //2.设置父类(目标)
        enhancer.setSuperclass(Target.class);
        //3.设置回调
        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object proxy, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                //执行前置
                 advice.before();
                //执行目标
                Object invoke = method.invoke(target, args);
                //执行后置
                advice.afterReturning();
                return invoke;
            }
        });
        //4.创建代理对象
        Target proxy = (Target) enhancer.create();

        proxy.save();
    }
}

spring框架Aop配置文件xml的实现

1.目标接口


package com.intheima.aop;

public interface TargetInterface {
    public void save();
}

2.目标对象

package com.intheima.aop;

public class Target implements TargetInterface {
    @Override
    public void save() {
        System.out.println("save running aop.....");
    }
}

3.定义切面类

package com.intheima.aop;
//切面类
public class MyAspect {
    public void before()
    {
        System.out.println("前置增强........");
    }
}

4.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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--目标对象-->
    <bean id="target" class="com.intheima.aop.Target"></bean>

    <!--切面对象-->
    <bean id="myAspect" class="com.intheima.aop.MyAspect"></bean>

    <!--配置织入:告诉spring框架 那些 方法(切面)需要进行增强(前置,后置)-->
    <aop:config>
        <!--声明切面-->
        <aop:aspect ref="myAspect">
            <!--切面:切点+通知-->
            <aop:before method="before" pointcut="execution(public void com.intheima.aop.Target.save())"></aop:before>
        </aop:aspect>
    </aop:config>
</beans>

5.测试

package com.itheima.Test;


import com.intheima.aop.TargetInterface;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {

    @Autowired
    private TargetInterface target;
    @Test
    public void test1()
    {
        target.save();
    }

}

6.pom的依赖

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.itheima</groupId>
  <artifactId>spring_aop</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>spring_aop Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.5.RELEASE</version>
    </dependency>
   <!--aspectj的依赖-->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.4</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <!--spring的测试包-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>5.0.5.RELEASE</version>
    </dependency>
  </dependencies>


</project>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值