execution()
2222222222222222222222222222222222222222
第四步:演示测试结果:
@RunWith(SpringRunner.class)
@SpringBootTest
public class ExecutionDemoApplicationTests {
@Autowired
ProductService productService;
@Autowired
SubService subService;
@Autowired
LogService logService;
@Test
public void test() {
System.out.println("###begin test###");
productService.findById(1L);
productService.findByTwoArgs(1L,"hello");
productService.getName();
subService.demo();
try {
productService.exDemo();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
第一步:需要添加的依赖:
aop
test
第二步:子包下的service
SubService:
@Component
public class SubService extends ProductService{
public void demo(){
System.out.println("execute sub service method");
}
}
第三步:
/**
* //匹配任何公共方法
@Pointcut("execution(public * com.imooc.service.*.*(..))")
//匹配com.imooc包及子包下Service类中无参方法
@Pointcut("execution(* com.imooc..*Service.*())")
//匹配com.imooc包及子包下Service类中的任何只有一个参数的方法
@Pointcut("execution(* com.imooc..*Service.*(*))")
//匹配com.imooc包及子包下任何类的任何方法
@Pointcut("execution(* com.imooc..*.*(..))")
//匹配com.imooc包及子包下返回值为String的任何方法
@Pointcut("execution(String com.imooc..*.*(..))")
//匹配异常
execution(public * com.imooc.service.*.*(..) throws java.lang.IllegalAccessException)
* Created by cat on 2017-02-19.
*/
@Aspect
@Component
public class ExecutionAspectConfig {
@Pointcut("execution(public * com.imooc.service..*Service.*(..) throws java.lang.IllegalAccessException)")
public void matchCondition(){}
@Before("matchCondition()")//before代表要执行插入的逻辑
public void before(){
System.out.println("");
System.out.println("###before");
}
}
execution表达式
execution(<修改符模式>?<返回类型模式><方法名模式>(<参数模式>)<异常模式>?)格式
除了返回类型模式、方法名模式和参数模式外,其它项都是可选的
execution示例
execution(<修改符模式>?<返回类型模式><方法名模式>(<参数模式>)<异常模式>?)格式
除了返回类型模式、方法名模式和参数模式外,其它项都是可选的
execution()格式——由前到后含义
修饰符,返回值,描述包名,描述方法名(描述方法参数),匹配方法抛出的异常
@Pointcut("execution(public * com.imooc.service..*Service.*(..))")
截图中 ?表示是可以省略的
execution 表达式的格式
execution表达式
execution()
修饰符,返回值,描述包名,描述方法名(描述方法参数),匹配方法抛出的异常
?表示是可以省略的
拦截子包的话 需要
@Pointcut("execution(public * com.imooc.service..*Service.*(..))")
匹配方法 execution
演示项目 excution-demo
包:
service被拦截的类
excution写法
?的可以省略
修饰符
返回值
描述包名
方法名(方法参数)
匹配方法抛出的异常
execution()格式
exexution表达式