jdk动态代理实现通用日志记录—KQC 0921

导读:本篇文章讲解 jdk动态代理实现通用日志记录—KQC 0921,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

KQC要求:

jdk动态代理实现通用日志记录:
-------------------------------------
1.创建员工业务类接口,员工业务实现类,
   并定义添加员工修改员工方法,方法内部输出提示信息
2.创建商品业务类接口,商品业务实现类
    并定义添加商品修改商品方法,方法内部输出提示信息
3.通过jdk动态代理,实现在调用业务方法之前和之后记录日志
  效果如下
===========================
日志:开始业务处理...
添加员工
日志:业务处理完成....

日志:开始业务处理...
修改员工
日志:业务处理完成....

日志:开始业务处理...
添加商品
日志:业务处理完成....

日志:开始业务处理...
修改商品
日志:业务处理完成....

参考:java动态代理
https://blog.csdn.net/qq_34609889/article/details/85317582

①aop注释的方式实现:

布局:

在这里插入图片描述

代码:

pom.xml

<?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>org.example</groupId>
  <artifactId>0921_KQC</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

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

  <properties>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
    <servlet.version>4.0.1</servlet.version>
    <jsp.version>2.2</jsp.version>
    <jstl.version>1.2</jstl.version>
    <spring.version>5.3.14</spring.version>
    <commons-dbcp.version>1.4</commons-dbcp.version>
    <mybatis.version>3.4.6</mybatis.version>
    <mybatis-spring.version>1.3.3</mybatis-spring.version>
    <mysql-connector-java.version>8.0.11</mysql-connector-java.version>
    <fastjson.version>1.2.78</fastjson.version>
  </properties>
  <dependencies>
    <!--  添加javaEE支持  -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>${servlet.version}</version>
      <!--  provided只在编译时支持,发布时不拷贝文件  -->
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>${jsp.version}</version>
      <!--  provided只在编译时支持,发布时不拷贝文件  -->
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>${jstl.version}</version>
    </dependency>
    <!-- 引入spring基础模块 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <!--dbcp连接池  -->
    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>${commons-dbcp.version}</version>
    </dependency>
    <!--mybatis-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>${mybatis.version}</version>
    </dependency>
    <!--  mybatis spring整合  -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>${mybatis-spring.version}</version>
    </dependency>
    <!-- mybatis插件PageHelper -->
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>5.1.0</version>
    </dependency>
    <!--通用mapper插件-->
    <dependency>
      <groupId>com.github.abel533</groupId>
      <artifactId>mapper</artifactId>
      <version>3.0.1</version>
    </dependency>
    <!--  mysql驱动类  -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>${mysql-connector-java.version}</version>
    </dependency>
    <!-- fastjson处理json数据 -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>${fastjson.version}</version>
    </dependency>
    <!-- lombok 简化实体内容 -->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.24</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.11.2</version>
    </dependency>
    <dependency>
      <groupId>com.sun.mail</groupId>
      <artifactId>javax.mail</artifactId>
      <version>1.5.6</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.30</version>
    </dependency>
    <dependency>
      <groupId>com.github.xuwei-k</groupId>
      <artifactId>html2image</artifactId>
      <version>0.1.0</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.9.8</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-simple</artifactId>
      <version>1.7.25</version>
    </dependency>
    <dependency>
      <groupId>cglib</groupId>
      <artifactId>cglib</artifactId>
      <version>3.1</version>
    </dependency>
  </dependencies>

</project>

spring-2.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:context="http://www.springframework.org/schema/context"
       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/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="userService" class="com.service.impl.UserServiceImpl"/>
    <bean id="goodsService" class="com.service.impl.GoodsServiceImpl"/>

    <context:component-scan base-package="com.advise"/>
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    <!--    <aop:aspectj-autoproxy/>-->

</beans>

LogAdvise :

package com.advise;

import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class LogAdvise {
    @Pointcut("execution(* com.service.impl.*.*(..))")
    public void pc(){

    }

    //@Before("execution(* com.service.impl.*.*(..))")
    @Before("pc()")
    public void bf(){
        System.out.println("日志:开始业务处理...");
    }

    //@AfterReturning("execution(* com.service.impl.*.*(..))")
    @AfterReturning("pc()")
    public void af(){
        System.out.println("日志:业务处理完成....");
    }


}


IGoodsService :

package com.service;

public interface IGoodsService {
    void cj();
    void xg();
}

IUserService :

package com.service;

public interface IUserService {
    void cj();
    void xg();
}

GoodsServiceImpl :

package com.service.impl;

import com.service.IGoodsService;

public class GoodsServiceImpl implements IGoodsService {
    @Override
    public void cj() {
        System.out.println("创建商品");
    }

    @Override
    public void xg() {
        System.out.println("修改商品");
    }
}

UserServiceImpl :

package com.service.impl;

import com.service.IUserService;

public class UserServiceImpl implements IUserService {
    @Override
    public void cj() {
        System.out.println("创建用户");
    }

    @Override
    public void xg() {
        System.out.println("修改用户");
    }
}

Test :

package com.test;

import com.service.IGoodsService;
import com.service.IUserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring-2.xml");
        IGoodsService iGoodsService=ac.getBean(IGoodsService.class);
        iGoodsService.cj();
        iGoodsService.xg();

        IUserService iUserService=ac.getBean(IUserService.class);
        iUserService.cj();
        iUserService.xg();

    }
}

运行结果:

在这里插入图片描述

②动态代理的方式实现:

布局:

在这里插入图片描述

代码:

pom.xml:

<?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>org.example</groupId>
  <artifactId>0921_KQC</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

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

  <properties>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
  </properties>
  
  <dependencies>
    <dependency>
      <groupId>cglib</groupId>
      <artifactId>cglib</artifactId>
      <version>3.1</version>
    </dependency>
  </dependencies>

</project>

IService :

package com.test2;

public interface IService {
    void xinJian();
    void shanChu();
}

Goods :

package com.test2;

public class Goods implements IService{
    @Override
    public void xinJian() {
        System.out.println("添加商品");
    }

    @Override
    public void shanChu() {
        System.out.println("删除商品");
    }
}

User :

package com.test2;

public class User implements IService{
    @Override
    public void xinJian() {
        System.out.println("添加员工");
    }

    @Override
    public void shanChu() {
        System.out.println("删除员工");
    }
}

HandleObj :

package com.test2;

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

public class HandleObj implements InvocationHandler {
    private Object target;

    public HandleObj(Object target) {
        this.target = target;
    }

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

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("日志:开始业务处理...");
        Object res=method.invoke(target,args);
        System.out.println("日志:业务处理完成....");
        return res;
    }
}

Test :

package com.test2;

import com.service.IUserService;

public class Test {
    public static void main(String[] args) {
        Goods goods=new Goods();
        HandleObj handleObj=new HandleObj(goods);
        IService iService=(IService) handleObj.getProxyObj();
        iService.xinJian();
        iService.shanChu();

        User user=new User();
        HandleObj handleObj1=new HandleObj(user);
        IService iService1=(IService) handleObj1.getProxyObj();
        iService1.xinJian();
        iService1.shanChu();
    }
}

运行结果:

在这里插入图片描述

②(标准)动态代理的方式实现:

布局:

在这里插入图片描述

代码:

pom.xml:

<?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>org.example</groupId>
  <artifactId>0921_KQC</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

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

  <properties>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
  </properties>
  
  <dependencies>
    <dependency>
      <groupId>cglib</groupId>
      <artifactId>cglib</artifactId>
      <version>3.1</version>
    </dependency>
  </dependencies>

</project>

IGoodsService :

package com.test3;

public interface IGoodsService {
    void xinJian();
    void shanChu();
}


IUserService :

package com.test3;

public interface IUserService {
    void xinJian();
    void shanChu();
}


GoodsServiceImpl :

package com.test3;

public class GoodsServiceImpl implements IGoodsService {
    @Override
    public void xinJian() {
        System.out.println("添加商品");
    }

    @Override
    public void shanChu() {
        System.out.println("删除商品");
    }
}


UserServiceImpl :

package com.test3;

public class UserServiceImpl implements IUserService {
    @Override
    public void xinJian() {
        System.out.println("添加员工");
    }

    @Override
    public void shanChu() {
        System.out.println("删除员工");
    }
}


HandleObj :

package com.test3;

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

public class HandleObj implements InvocationHandler {
    private Object target;

    public HandleObj(Object target) {
        this.target = target;
    }

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

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("日志:开始业务处理...");
        Object res=method.invoke(target,args);
        System.out.println("日志:业务处理完成....");
        return res;
    }
}

Test :

package com.test3;

public class Test {
    public static void main(String[] args) {
        GoodsServiceImpl goods=new GoodsServiceImpl();
        HandleObj handleObj=new HandleObj(goods);
        IGoodsService iService=(IGoodsService) handleObj.getProxyObj();
        iService.xinJian();
        iService.shanChu();

        UserServiceImpl user=new UserServiceImpl();
        HandleObj handleObj1=new HandleObj(user);
        IUserService iUserService=(IUserService) handleObj1.getProxyObj();
        iUserService.xinJian();
        iUserService.shanChu();
    }
}


运行结果:

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/118026.html

(0)
seven_的头像seven_bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!