汪的爱琪 2023-08-21 17:48 采纳率: 67.5%
浏览 8
已结题

SSM基于注解的aop为什么执行两遍?

我在service定义一个目标类,用于输出"您登录了"
我又定义了一个切面类,里面通知为输出"登陆前检查!",通过切入点@before
现在我在service层的目标类中织入这个切面类的通知,在controller层里面调用service层的输入,理想状态下是输出
"登陆前检查!"
"您登陆了"
但是现在它执行了两次,这是为什么呀?
切面类

package com.experience.aspect;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAspect {
    @Pointcut("execution(void com.experience.service.LoginService.login())")
    private void method(){}
    @Before("method() ")
    public void show(){
        System.out.println("登录前检查!");
    }
}


controller层


package com.experience.controller;

import com.experience.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class LoginTest {
    @Autowired
    LoginService loginService;
    @RequestMapping(value = "/denglu")
    public void login()
    {
        loginService.login();
    }
}

service代码(目标类)

package com.experience.service;

import org.springframework.stereotype.Service;

@Service
public class LoginService {
    public void login(){
        System.out.println("您登录了!");
    }
}


输出结果

img


配置文件'

<?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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       https://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--    开启注解扫描,扫描包-->
    <context:component-scan base-package="com.experience.service"/>
    <context:component-scan base-package="com.experience.aspect"/>
    <aop:aspectj-autoproxy/>
</beans>

  • 写回答

2条回答 默认 最新

  • 小明爱吃火锅 2023-09-05 10:52
    关注

    切点表达式的问题了
    切点表达式是"execution(void com.experience.service.LoginService.login())"。这个表达式只匹配返回类型为void的"login()"方法
    但是LoginService有个子类,也会被扫描到,所以就会调用两次login
    你可以改成"execution(void com.experience.service.LoginServiceImpl.login())" 或者 "execution(* com.experience.service.LoginService.login())"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 2月23日
  • 已采纳回答 2月15日
  • 创建了问题 8月21日

悬赏问题

  • ¥15 有人会用py或者r画这种图吗
  • ¥15 MOD04_3K图像预处理
  • ¥15 [VASP]关于超胞大小和k 点网格的收敛性测试
  • ¥15 pip下载paddle2onnx离谱错误
  • ¥60 db2move nlzxams import 导出db2备份数据报错
  • ¥15 关于#python#的问题:全文总结功能咨询
  • ¥15 俄罗斯方块中无法同时消除多个满行
  • ¥15 使用gojs3.0,如何在nodeDataArray设置好text的位置,再go.TextBlock alignment中进行相应的改变
  • ¥15 psfusion图像融合指标很低
  • ¥15 银河麒麟linux系统如何修改/etc/hosts权限为777