markdownaop简单使用(代码片段)

author author     2022-12-13     382

关键词:

If we want to use aop to record which method is being called

1. build the aop annotation
```
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AopExample 
  String methodName();

```
2. put it on your method
```
  @AopExample(methodName = "appendName")
  public String appendName(String param)
    return "param is : "+param;
  
```

3. intercept method by aop
we build the AopDemo class
```
@Aspect
@Component
public class AopDemo 

  @AfterReturning(
      value =
          "@annotation(com.demo.bean.aop.AopExample))
  public Object recordMethod(JoinPoint joinPoint) 
    MethodSignature signature = (MethodSignature) joinPoint.getSignature();
    AopExample aop = signature.getMethod().getAnnotation(AopExample.class);
    String theMethod = aop.methodName();
    System.out.println(theMethod);
    return null;
  

```
we got the method appendName now

How to catch the parameter when we invoke appendName?
just appoint the method name on interceopter
```
  @AfterReturning(
      value =
          "@annotation(com.demo.bean.aop.AopExample) && execution(* com.demo.service.AppendService.appendName(..)) && args(param)")
  public Object recordMethod(JoinPoint joinPoint, String param) 
    MethodSignature signature = (MethodSignature) joinPoint.getSignature();
    AopExample aop = signature.getMethod().getAnnotation(AopExample.class);
    String theMethod = aop.methodName();
    System.out.println(theMethod);

    System.out.println(param);
    return null;
  
```
you should notice that this interceptor only intercept the method **appendName** now, so the **recordMethod** only service **appendName** 

and if you want to get return value
```
  @AfterReturning(
      value =
          "@annotation(com.demo.bean.aop.AopExample) && execution(* com.demo.service.AppendService.appendName(..)) && args(param)"
          , returning = "returnValue")
  public Object recordMethod(JoinPoint joinPoint, String param, String returnValue) 
    MethodSignature signature = (MethodSignature) joinPoint.getSignature();
    AopExample aop = signature.getMethod().getAnnotation(AopExample.class);
    String theMethod = aop.methodName();
    System.out.println(theMethod);

    System.out.println(param);
    
    System.out.println(returnValue);
    return null;
  
```

java小吃吧简单使用(代码片段)

查看详情

java小吃吧简单使用(代码片段)

查看详情

java小吃吧简单使用(代码片段)

查看详情

java简单线程池使用(代码片段)

查看详情

使用httpservlet简单实例(代码片段)

使用HttpServlet简单实例packagecom.kettas.servlet;importjavax.servlet.*;importjavax.servlet.http.*;importjava.io.*;publicclassLoginServletextendsHttpServlet@Overridepublicvoidservice(HttpServletRequestreque 查看详情

html简单地使用jquery(代码片段)

查看详情

elasticjob简单使用(代码片段)

ElasticJob单点使用任务类publicclassBackupJobimplementsSimpleJobpublicvoidexecute(ShardingContextshardingContext)StringselectSql="select*fromresumewherestate=\'未归档\'limit1";List<Map<String,Object>& 查看详情

vuex的简单使用(代码片段)

引入什么的都不说了,前面说过了,下面简单列出今天简单使用的情况:store.jsimportVuefrom"vue";importVuexfrom"vuex";Vue.use(Vuex);exportdefaultnewVuex.Store(state:pageName:‘签到须知‘,getters:pageName(state)returnstate.pageName;,mutat 查看详情

hibernate-简单使用(代码片段)

Hibernate-简单使用主流ORM框架ObjectRelationMapping对象关系映射将面向对象映射成面向关系Java类映射成表tablecustomer( idchar, namechar)tableorders( id, name, cidfkeyid(course))【1】使用(1)导入相关依赖(2)创建Hibernate配置 查看详情

php使用简单的soap服务(代码片段)

查看详情

python使用flask上传简单图像(代码片段)

查看详情

html使用sweetalert进行简单验证(代码片段)

查看详情

html使用jquery简单点击事件(代码片段)

查看详情

python使用numba的简单示例。(代码片段)

查看详情

php使用简单功能重定向页面(代码片段)

查看详情

javascript使用一个类简单匹配高度(代码片段)

查看详情

html探索d3-使用简单数据(代码片段)

查看详情

python简单的twisted使用例子。(代码片段)

查看详情