Live2D

IDEA创建maven的spring boot项目,【极简版】切面编程+swagger调用

为一次项目创建做记录,用最少的类展示切面编程_AOP

1、项目结构:

----src

--------main

--------|---java

--------|---|----com.demo

--------|---|---|---bean

--------|---|---|---|----Caculate

--------|---|---|---config

--------|---|---|---|----LoggingAspect

--------|---|---|---|----SwaggerConfig

--------|---|---|---controller

--------|---|---|---|----TestController

--------|---|---|---|----DemoApplication

--------|---resources

--------|---|----application.yml

2、各个类展示

  1)基础类(包含要调用的函数——提供方)

@Component
public class Caculate{
    
    public int add(int a,int b){
        return a+b;
    }
}

 

  2)切面类(用于定义切面和操作)

1
2
3
4
5
6
7
8
9
10
@Aspect
@Component
public class LoggingAspect{
    @Before("execution(* com.demo.controller.*.*(..))")
    public void logBeforeMethodCall(){
        String name=joinPoint.getSignature().getName();
        System.out.println("Calling method:"+name);
    }
 
}

  

  3)调用类(调用方)

1
2
3
4
5
6
7
8
9
10
11
12
13
@RestController
public class TestController{
     
    @AutoWired
    Caculate caculate;
     
    @GetMapping("/run")
    public void fun(){
        int result=caculate.add(5,3);
        System.out.println("result:"+result);
    }
 
}

  

  4)swagger配置类(用于页面调试,可不使用)

1
2
3
4
5
6
7
8
9
10
11
@Configuration
public class SwaggerConfig{
    @Bean
    public Docket restfultApi(){
    return new Docket(DocumentationType.SWAGGER_2)
    .genericModelSubstitutes(ResponseEntity.class)
    .select()
    .apis(RequestHandlerSelectors.basePackage("com.demo.controller"))
    .build();
    }
}

  

  5)启动类

1
2
3
4
5
6
7
8
@SpringBootApplication
@EnableAspectJAutoProxy
@EnableSwagger2
public class DemoApplication{
    public static void main(String[] args){
        SpringApplication.run(DemoApplication.class);
}
}

  

  6)项目配置文件

1
2
server:
  port: 8888

  

 

3、结果展示

1
Calling method: funresult:9

  

 

posted @   -涂涂-  阅读(51)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
点击右上角即可分享
微信分享提示