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 鸿蒙创新赛正式启动,百万大奖等你挑战!
【推荐】电信天翼云 2025 云上钜惠,爆款云主机2核4G只要1.5折起