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 |
【推荐】2025 HarmonyOS 鸿蒙创新赛正式启动,百万大奖等你挑战
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】开源 Linux 服务器运维管理面板 1Panel V2 版本正式发布
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 干翻 Typora!MilkUp:完全免费的桌面端 Markdown 编辑器!
· 那些年我们一起追过的Java技术,现在真的别再追了!
· 记一次.NET MAUI项目中绑定Android库实现硬件控制的开发经历
· 从WebApi迁移到Minimal API?有了这个神器,小白也能10分钟搞定!
· 抛开官方库,手撸一个轻量级 MCP 服务端