为了账号安全,请及时绑定邮箱和手机立即绑定

@Before、@After注解的方法都不执行

@Aspect
@Component
public class AspectConfig {
	@Pointcut("args(Integer) && within(com.imooc.miaosha.controller.*)")
	public void matchArgs() {}
	@Before("matchArgs()")
	public void before_1() {
        	System.out.println("####before1");
        }
        @After("matchArgs()")
        public void after_1() {
        	System.out.println("####after1");
        }
}
package com.imooc.miaosha.controller;
@Controller
@RequestMapping("/demo")
public class SampleController {
        @RequestMapping("/hello")
        @ResponseBody
        public Result<String> home() {
            System.out.println("hello");
            printInteger(100);
            return Result.success("Hello,world");
        }
            private void printInteger(Integer i) {
               	System.out.println("this is "+i);
            }
        }

运行结果:访问http://localhost:8080/demo/hello,正常运行,没有报异常,但是AspectConfig类中的before_1、after_1方法都没有执行

正在回答

3 回答

你将private void printInteger(Integer i)方法写到service层,类上加上注解@Service,然后再从Controller层用@Autowried注入调用就行了.

匹配表达式改成within(com.imooc.miaosha.service.*)

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

@Before、@After注解的方法都不执行

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信