springAop-1

使用springboot的springAop简单使用

引入spring-boot-start-aop包,测试引入spring-boot-start-test

@Aspect@Compent 类上注解,

@Pointcut(“@annotation(注解全路径)”) 要切的方法

     execution(public void com.likui.service.*insert(..) throws java.lang.IllegalAccessException)

  匹配方法的全名,*表示任意,..任意多个,也可以指定抛出的异常

  。。。。还有很多种匹配不过感觉execution已经可以完成所有的工作了

@before() 前置拦截 

 

@Aspect
@Component
public class springAopTest {

@Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
public void matchAnno(){}

@Pointcut("execution(public String com.lik.demo.mybatis.controller.TestController.firstStart(String) throws java.lang.IllegalAccessException)")
public void matchAllRoute(){}

@Before("matchAllRoute()")
public void beforeInsert(){

System.out.println("前置拦截");

}
}
测试使用
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringAopTest {

@Autowired
private TestController testController;

@Test
public void testBefore(){
String result = testController.firstStart("小王");
System.out.println(result);
}
}



 

posted @ 2018-05-07 21:16  fantastiLi  阅读(95)  评论(0)    收藏  举报