初探spring boot Enable能力

       spring boot 由于其超强的整合能力和强大的社区生态,已经渐渐成为java项目开发必备框架。

       使用过springboot的同学们应该都很清楚,很多第三方组件与springboot集成时只需要添加一个@EnableXXX注解,就可以在项目中使用。下面使用springboot的Enable来实现一个记录方法调用次数的例子。

       第一步. 定义EnableMethod注解类

@Import(MethodConfiguration.class)
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface EnableMethod {
}
@Import(MethodConfiguration.class) 这行代码告诉springboot需要云解析MethodConfiguration这个类。

第二步. MethodConfiguration

@Configurable
@Aspect
public class MethodConfiguration {

    static Map<String, Integer> map = new HashMap<>();

    @Pointcut("@annotation(com.zsc.spring.method.annotions.MethodStatistics)")
    public void pointCut() {
    }

    @Before("pointCut()")
    public void before(JoinPoint point) {
        String methodName = point.getSignature().getName();
        System.out.println(String.format("invoke method %s", methodName));
        Integer count = map.get(methodName);
        if(null == count) {
            map.put(methodName, 1);
        } else {
            map.put(methodName, ++count);
        }
        System.out.println(map.get(methodName));
    }
}

@Configuration:告诉springboot该类作为一个配置类注入到spring容器中

@Aspect: 将MethodConfiguration定义成一个切面类,拦截所有添加MethodStatistics注解方法,最终注入到spring容器中

第三步:将该工程使用maven 打包成jar包,以便使用者引入

第四步:需要使用springboot项目中引入

<dependency>
            <groupId>com.zsc.spring.method</groupId>
            <artifactId>spring-method</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

第五步:Application类添加@EnableMethod注解

@SpringBootApplication
@EnableMethod
public class Application {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(Application.class);
        app.run(args);
    }
}

第六步:添加测试接口,并添加@MethodStatistics注解

@RestController
public class HelloWorldController {

    @RequestMapping(value = "hello")
    @MethodStatistics
    public String hello() {
        return "hello spring boot enable";
    }
}

第七步:启动项目,访问http://localhost:8080/hello接口,观察控制台信息

回答: 当子工程中的spring-boot-starter-web报红时,可能有几个原因。首先,确保你的子工程的pom.xml文件中引入了正确的依赖。根据引用\[2\],当引入了spring-boot-starter-web的依赖后,就无需再引入spring-boot-starter的依赖了。所以请检查你的pom.xml文件中是否有以下依赖配置:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>\[3\]。如果依赖配置正确,但仍然报红,可能是由于IDE的缓存问题。你可以尝试重新加载项目或清除IDE的缓存来解决这个问题。如果问题仍然存在,可能是由于其他原因导致的,你可以进一步检查错误提示信息或查阅相关文档来解决问题。 #### 引用[.reference_title] - *1* [springboot初探---spring-boot-starter-web究竟干了啥](https://blog.csdn.net/weixin_35990136/article/details/113999995)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [spring-boot-starter-web(Web启动器)](https://blog.csdn.net/qq_35366269/article/details/125077830)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [如何解决gateway与spring-boot-starter-web冲突问题](https://blog.csdn.net/weixin_72889743/article/details/127330395)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值