当前位置: 代码网 > it编程>编程语言>Java > SpringBoot AOP导致service注入后是null的问题

SpringBoot AOP导致service注入后是null的问题

2024年10月31日 Java 我要评论
springboot aop导致service注入后是null1.由于业务需求需要记录用户操作日志,无疑需要使用到springaop。2.先引入springboot的aop maven 依赖 <

springboot aop导致service注入后是null

1.由于业务需求需要

记录用户操作日志,无疑需要使用到springaop。

2.先引入springboot的aop maven 依赖

 <dependency>
      <groupid>org.springframework.boot</groupid>
       <artifactid>spring-boot-starter-aop</artifactid>
 </dependency>

3.实现这个日志操作可以有很多种方法

比如写拦截器,或者基于注解形式,在或者在原来写好的代码中添加也行,但是最后的一种无疑会污染原来的代码,同时会对原来的逻辑有一定污染,而现在的业务场景,所有的逻辑代码都已经编写完成,自测完成,而且查询接口的数量明显大于(增删改),而且用户的操作日志我们最关心的无疑是对数据的操作。所以选择基于注解的形式实现。

4.注解编写

上代码

@retention(retentionpolicy.runtime)//元注解,定义注解被保留策略,一般有三种策略
//1、retentionpolicy.source 注解只保留在源文件中,在编译成class文件的时候被遗弃
//2、retentionpolicy.class 注解被保留在class中,但是在jvm加载的时候北欧抛弃,这个是默认的声明周期
//3、retentionpolicy.runtime 注解在jvm加载的时候仍被保留
@target({elementtype.method}) //定义了注解声明在哪些元素之前
@documented
public @interface systemoperalog {
    //定义成员
    string descrption() default "" ;//描述
    string actiontype() default "添加" ;//操作的类型,1、添加 2、修改 3、删除
}

5.下面需要对springboot aop 中一些注解

了解一下

  • @aspect:描述一个切面类,定义切面类的时候需要打上这个注解
  • @configuration:spring-boot配置类
  • @pointcut:声明一个切入点,切入点决定了连接点关注的内容,使得我们可以控制通知什么时候执行。spring aop只支持spring bean的方法执行连接点。所以你可以把切入点看做是spring bean上方法执行的匹配。一个切入点声明有两个部分:一个包含名字和任意参数的签名,还有一个切入点表达式,该表达式决定了我们关注那个方法的执行。

注:作为切入点签名的方法必须返回void 类型

spring aop支持在切入点表达式中使用如下的切入点指示符:    

  • execution - 匹配方法执行的连接点,这是你将会用到的spring的最主要的切入点指示符。
  • within - 限定匹配特定类型的连接点(在使用spring aop的时候,在匹配的类型中定义的方法的执行)。
  • this - 限定匹配特定的连接点(使用spring aop的时候方法的执行),其中bean reference(spring aop 代理)是指定类型的实例。
  • target - 限定匹配特定的连接点(使用spring aop的时候方法的执行),其中目标对象(被代理的应用对象)是指定类型的实例。
  • args - 限定匹配特定的连接点(使用spring aop的时候方法的执行),其中参数是指定类型的实例。
  • @target - 限定匹配特定的连接点(使用spring aop的时候方法的执行),其中正执行对象的类持有指定类型的注解。
  • @args - 限定匹配特定的连接点(使用spring aop的时候方法的执行),其中实际传入参数的运行时类型持有指定类型的注解。
  • @within - 限定匹配特定的连接点,其中连接点所在类型已指定注解(在使用spring aop的时候,所执行的方法所在类型已指定注解)。
  • @annotation - 限定匹配特定的连接点(使用spring aop的时候方法的执行),其中连接点的主题持有指定的注解。

其中execution使用最频繁,即某方法执行时进行切入。定义切入点中有一个重要的知识,即切入点表达式,我们一会在解释怎么写切入点表达式。

切入点意思就是在什么时候切入什么方法,定义一个切入点就相当于定义了一个“变量”,具体什么时间使用这个变量就需要一个通知。

即将切面与目标对象连接起来。

如例子中所示,通知均可以通过注解进行定义,注解中的参数为切入点。

spring aop支持的通知:

  • @before:前置通知:在某连接点之前执行的通知,但这个通知不能阻止连接点之前的执行流程(除非它抛出一个异常)。
  • @afterreturning :后置通知:在某连接点正常完成后执行的通知,通常在一个匹配的方法返回的时候执行。

6.了解完以上步骤

需要做的是记录用户正常请求,以及异常请求需要记录的信息,对应一下实体操作

@data
@equalsandhashcode(callsuper = false)
@accessors(chain = true)
@apimodel(value="sysoperalog对象", description="")
public class sysoperalog implements serializable {

    private static final long serialversionuid = 1l;

    @tableid(value = "log_id", type = idtype.auto)
    private integer logid;

    @apimodelproperty(value = "操作类型")
    private string type;

    @apimodelproperty(value = "访问资源路径")
    private string url;

    @apimodelproperty(value = "操作人员")
    private string operauser;

    @apimodelproperty(value = "方法名称")
    private string methodname;

    @apimodelproperty(value = "访问携带参数")
    private string params;

    @apimodelproperty(value = "远程ip地址")
    private string ipaddress;

    @apimodelproperty(value = "访问时间")
    @tablefield("operatime")
    @jsonformat(timezone = "gmt+8", pattern = "yyyy-mm-dd hh:mm:ss")
    private localdatetime operatime;

    @apimodelproperty(value = "访问总时长单位毫秒")
    @tablefield("timelong")
    private long timelong;

    @apimodelproperty(value = "描述")
    private string des;

    @apimodelproperty(value = "访问状态 0 访问成功 -1 访问失败")
    private integer visitstate;

    @apimodelproperty(value = "异常信息")
    private string exceptiondetail;


}

7.用户正常请求

在用户正常请求头部加上自定义的注解

    @postmapping("/save")
    @systemoperalog(descrption = "新增车位信息")
    public result saveparklot(@apiparam(name = "parklotsdto", value = "车位信息") @requestbody parklotsdto parklotsdto, bindingresult bindingresult) {
        // 现在表示执行的验证出现错误
        if (bindingresult.haserrors()) {
            // 获取全部错误信息
            list<objecterror> allerrors = bindingresult.getallerrors();
            string errormsg = "";
            if (!collectionutils.isempty(allerrors)) {
                errormsg = allerrors.get(0).getdefaultmessage();
            }
            return resultsupport.fail(errormsg);
        } else {
            parklotsservice.save(parklotsdto);
            return resultsupport.savesuccess();
        }
    }

8.在aop中拦截进入该方法的前置操作以及异常操作

@aspect
@configuration
@slf4j
public class systemoperalogaop {

    @autowired
    private sysoperalogmapper sysoperalogmapper;

    /***
     * 定义controller切入点拦截规则,拦截systemcontrollerlog注解的方法
     */
    @pointcut("@annotation(cn.hayll.parking.local.common.annotation.systemoperalog)")
    public void controlleraspect(){}

    /***
     * 拦截控制层的操作日志
     * @param joinpoint
     * @return
     * @throws throwable
     */
    @around("controlleraspect()")
    public object recordlog(proceedingjoinpoint joinpoint) throws throwable {
        //1、开始时间
        long begintime = system.currenttimemillis();
        //利用requestcontextholder获取requst对象
        servletrequestattributes requestattr = (servletrequestattributes)requestcontextholder.currentrequestattributes();
        string uri = requestattr.getrequest().getservletpath();
        //访问目标方法的参数 可动态改变参数值
        object[] args = joinpoint.getargs();
        //方法名获取
        string methodname = joinpoint.getsignature().getname();
        //可能在反向代理请求进来时,获取的ip存在不正确行 这里直接摘抄一段来自网上获取ip的代码
        authentication authentication = securitycontextholder.getcontext().getauthentication();
        sysuserdetails sysuserdetails = (sysuserdetails) authentication.getprincipal();
        signature signature = joinpoint.getsignature();
        if(!(signature instanceof methodsignature)) {
            throw new illegalargumentexception("暂不支持非方法注解");
        }
        //调用实际方法
        object object = joinpoint.proceed();
        //获取执行的方法
        methodsignature methodsign = (methodsignature) signature;
        method method = methodsign.getmethod();
        //判断是否包含了 无需记录日志的方法
        long endtime = system.currenttimemillis();
        //模拟异常
        //system.out.println(1/0);
        sysoperalog systemlogdto = new sysoperalog();
        systemlogdto.settype(getannontationmethoddescription(joinpoint,1));
        systemlogdto.seturl(uri);
        systemlogdto.setoperauser(sysuserdetails.getusername());
        systemlogdto.setmethodname(methodname);
        systemlogdto.setipaddress(getipaddr(requestattr.getrequest()));
        list list = collectionutils.arraytolist(args);
        list arraylist = new arraylist(list);
        //移除操作需要将数组转换的集合类型在此转换为集合类型
        iterator iterator = arraylist.iterator();
        while (iterator.hasnext()){
            if(iterator.next().tostring().contains("beanpropertybindingresult")){
                iterator.remove();
                break;
            }
        }
        systemlogdto.setparams(arraylist.tostring());
        systemlogdto.setoperatime(localdatetime.now());
        systemlogdto.setdes(getannontationmethoddescription(joinpoint,0));
        systemlogdto.settimelong(endtime - begintime);
        sysoperalogmapper.insert(systemlogdto);
        return object;
    }

    //异常处理
    @afterthrowing(pointcut = "controlleraspect()",throwing="e")
    public void doafterthrowing(joinpoint joinpoint, throwable e) throws throwable{
        //1、开始时间
        long begintime = system.currenttimemillis();
        //利用requestcontextholder获取requst对象
        servletrequestattributes requestattr = (servletrequestattributes)requestcontextholder.currentrequestattributes();
        string uri = requestattr.getrequest().getservletpath();
        //访问目标方法的参数 可动态改变参数值
        object[] args = joinpoint.getargs();
        //方法名获取
        string methodname = joinpoint.getsignature().getname();
        //可能在反向代理请求进来时,获取的ip存在不正确行 这里直接摘抄一段来自网上获取ip的代码
        authentication authentication = securitycontextholder.getcontext().getauthentication();
        sysuserdetails sysuserdetails = (sysuserdetails) authentication.getprincipal();
        signature signature = joinpoint.getsignature();
        if(!(signature instanceof methodsignature)) {
            throw new illegalargumentexception("暂不支持非方法注解");
        }
        //获取执行的方法
        methodsignature methodsign = (methodsignature) signature;
        method method = methodsign.getmethod();
        //判断是否包含了 无需记录日志的方法
        long endtime = system.currenttimemillis();
        //模拟异常
        sysoperalog systemlogdto = new sysoperalog();
        systemlogdto.seturl(uri);
        systemlogdto.settype(getannontationmethoddescription(joinpoint,1));
        systemlogdto.setoperauser(sysuserdetails.getusername());
        systemlogdto.setmethodname(methodname);
        systemlogdto.setipaddress(getipaddr(requestattr.getrequest()));
        list list = collectionutils.arraytolist(args);
        list arraylist = new arraylist(list);
        //移除操作需要将数组转换的集合类型在此转换为集合类型
        iterator iterator = arraylist.iterator();
        while (iterator.hasnext()){
            if(iterator.next().tostring().contains("beanpropertybindingresult")){
                iterator.remove();
                break;
            }
        }
        systemlogdto.setparams(arraylist.tostring());
        systemlogdto.setoperatime(localdatetime.now());
        systemlogdto.settimelong(endtime - begintime);
        systemlogdto.setdes(getannontationmethoddescription(joinpoint,0));
        systemlogdto.setvisitstate(-1);
        systemlogdto.setexceptiondetail(e.getmessage());
        sysoperalogmapper.insert(systemlogdto);
    }

    public static string getipaddr(httpservletrequest request) {
        string ipaddress = null;
        try {
            ipaddress = request.getheader("x-forwarded-for");
            if (ipaddress == null || ipaddress.length() == 0 || "unknown".equalsignorecase(ipaddress)) {
                ipaddress = request.getheader("proxy-client-ip");
            }
            if (ipaddress == null || ipaddress.length() == 0 || "unknown".equalsignorecase(ipaddress)) {
                ipaddress = request.getheader("wl-proxy-client-ip");
            }
            if (ipaddress == null || ipaddress.length() == 0 || "unknown".equalsignorecase(ipaddress)) {
                ipaddress = request.getremoteaddr();
                if (ipaddress.equals("127.0.0.1")) {
                    // 根据网卡取本机配置的ip
                    inetaddress inet = null;
                    try {
                        inet = inetaddress.getlocalhost();
                    } catch (unknownhostexception e) {
                        log.error("获取ip异常:{}" ,e.getmessage());
                        e.printstacktrace();
                    }
                    ipaddress = inet.gethostaddress();
                }
            }
            // 对于通过多个代理的情况,第一个ip为客户端真实ip,多个ip按照','分割
            if (ipaddress != null && ipaddress.length() > 15) {
                // = 15
                if (ipaddress.indexof(",") > 0) {
                    ipaddress = ipaddress.substring(0, ipaddress.indexof(","));
                }
            }
        } catch (exception e) {
            ipaddress = "";
        }
        return ipaddress;
    }

    /***
     * 获取controller的操作信息
     * @param point
     * @return
     */
    public string getannontationmethoddescription(joinpoint point,integer type) throws  exception{
        //获取连接点目标类名
        string targetname = point.gettarget().getclass().getname() ;
        //获取连接点签名的方法名
        string methodname = point.getsignature().getname() ;
        //获取连接点参数
        object[] args = point.getargs() ;
        //根据连接点类的名字获取指定类
        class targetclass = class.forname(targetname);
        //获取类里面的方法
        method[] methods = targetclass.getmethods() ;
        string description="" ;
        for (method method : methods) {
            if (method.getname().equals(methodname)){
                class[] clazzs = method.getparametertypes();
                if (clazzs.length == args.length){
                    if(type==0){
                        description = method.getannotation(systemoperalog.class).descrption();
                        break;
                    }else{
                        description = method.getannotation(systemoperalog.class).actiontype();
                        break;
                    }

                }
            }
        }
        return description ;
    }

9.说一下踩的一个坑

现在日志已经可以正常工作了,但是业务代码却失效了,service注入的时候是空的(部分代码),

一顿百度以后发现,原来aop只能对public 和provide 生效,如果你的方法限制是private,那么service注入就为空,在springboot 中默认使用的是cglib来代理操作对象,首先,私有方法是不会出现在代理类中,这也就是为什么代理对象无法对private操作的根本原因

  • jdk是代理接口,私有方法必然不会存在在接口里,所以就不会被拦截到; 
  • cglib是子类,private的方法照样不会出现在子类里,也不能被拦截。 

10.解决的根本办法

不是强制使用cglib来代理,而是要将你的controller中的方法不设置私有属性,以上仅仅代表个人观点哟。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

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

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com