栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring 自定义注解(aop实现)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring 自定义注解(aop实现)


spring 自定义注解(aop实现)

        

                

                                

使用示例

      

自定义注解标注在service层的方法上,统计标注了注解的方法执行时间

                        

           

CustomAnnotation

@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomAnnotation {
}

       

HelloService

public interface HelloService {

    String hello();
    String hello2();
}

      

HelloServiceImpl

@Service
public class HelloServiceImpl implements HelloService {

    @Override
    @CustomAnnotation
    public String hello() {
        return "hello";
    }

    @Override
    public String hello2(){
        return "hello2";
    }
}

      

CustomAspect

@Aspect
@Component
public class CustomAspect {

    @Pointcut("@annotation(com.example.demo.annotation.CustomAnnotation)")
    public void fun(){

    }

    @Around("fun()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();

        Object result = null;
        long startTime = System.currentTimeMillis();
        try {
            result = joinPoint.proceed();
        }catch (Throwable e){
            e.fillInStackTrace();
            throw e;
        }
        finally {
            long spendTime = (System.currentTimeMillis() - startTime);
            System.out.println(method.getDeclaringClass().getName()+"."+method.getName()+"执行耗时:"+spendTime+"ms");
        }

        return result;
    }
}

       

HelloController

@RestController
public class HelloController {

    @Resource
    private HelloService helloService;

    @RequestMapping("/hello")
    public String hello(){
        return helloService.hello();
    }

    @RequestMapping("/hello2")
    public String hello2(){
        return helloService.hello2();
    }
}

      

            

                                

使用测试

  

localhost:8080/hello,控制台输出:

2022-04-25 14:13:13.054  INFO 2139 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 880 ms
2022-04-25 14:13:13.386  INFO 2139 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-04-25 14:13:13.398  INFO 2139 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 1.557 seconds (JVM running for 2.005)
2022-04-25 14:13:15.410  INFO 2139 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-04-25 14:13:15.411  INFO 2139 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2022-04-25 14:13:15.412  INFO 2139 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
com.example.demo.service.impl.HelloServiceImpl.hello执行耗时:9ms

                  

localhost:8080/hello,控制台无输出

      

           

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/837313.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号