springboot自定义注解,项目启动时扫描注解

本文介绍了一个SpringBoot项目中自定义注解的使用,项目启动时自动扫描带有@Section注解的类,并将信息存储在Map中。注解包含id、remark和version属性,扫描结果会进一步整理成Map<String, SectionConfigObj>,方便后续流程配置引用。" 108651080,5769462,Flutter自定义导航与UI管理,"['Flutter开发', 'UI设计', '导航栏', '颜色管理', '字体设置']
源码参见:https://gitee.com/tcyj/DefineAnnotations.git

1、需求:

自定义注解,项目启动时进行注解扫描并存储,便于后面生成对象。类似于@Controller,@Service,自定义的原因是,整个项目会初始化很多流程,每个流程由好多个小模块(对象)串联起来。同一个类会被创建多个对象,被使用在不同的流程里面。以后会根据流程的配置对应切换对象。

2、项目结构

在这里插入图片描述

3、代码和配置

pom.xml
  <parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.9.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
  <dependencies>
 
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
  </dependencies>
application.yml
server:
  port: 8083
注解类
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Target(ElementType.TYPE)
public @interface Section {
   
    //在类上面写 @Section(id = "xxx", remark = "xxx") 类似于@Controller的使用

    String id();
    String remark();
	String v() default "1";

}
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Target(ElementType.TYPE)
@Import({
   
   SectionRegister.class})
public @interface SectionScan {
   
    // 启动类上面@SectionScan(basePackages={"扫描的类路径"}) 类似@ComponentScan(basePackages = {"com.migu.*"})

	String[] basePackages() default {
   
   };
}
启动类
@SpringBootApplication
@ComponentScan(basePackages = {
   
   "com.migu.*"}) // springboot 扫描指定包下面的注解
@SectionScan(basePackages={
   
   "com.migu.video.process.section.*","com.migu.video.process.sectionV2.*"}) // 扫描2个指定包的下的注解

public class Application {
   
   
	
	public static void main(Strin
Spring Boot是一个用于构建独立的、生产级别的Spring应用程序的框架。它提供了许多便捷的功能和特性,其中包括自定义注解配置。 在Spring Boot中,我们可以通过自定义注解来实现一些特定的配置。下面是一个简单的示例来介绍如何自定义注解配置: 1. 首先,创建一个自定义注解类,使用`@interface`关键字来定义注解。例如,我们创建一个名为`@CustomAnnotation`的注解: ```java import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface CustomAnnotation { String value() default ""; } ``` 2. 在需要使用该注解的类上添加注解。例如,我们创建一个名为`CustomClass`的类,并在类上添加`@CustomAnnotation`注解: ```java @CustomAnnotation("customValue") public class CustomClass { // 类的具体实现 } ``` 3. 在Spring Boot的配置类中,使用`@ComponentScan`注解扫描带有自定义注解的类,并进行相应的配置。例如: ```java @SpringBootApplication @ComponentScan(basePackages = "com.example") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 4. 在需要使用自定义注解的地方,可以通过反射获取注解,并进行相应的处理。例如,在某个Service类中使用自定义注解: ```java @Service public class CustomService { @Autowired private ApplicationContext applicationContext; public void processCustomAnnotation() { Map<String, Object> customBeans = applicationContext.getBeansWithAnnotation(CustomAnnotation.class); for (Object bean : customBeans.values()) { CustomAnnotation customAnnotation = bean.getClass().getAnnotation(CustomAnnotation.class); String value = customAnnotation.value(); // 处理自定义注解的逻辑 } } } ``` 这样,我们就可以通过自定义注解来实现一些特定的配置。在上述示例中,我们通过自定义注解`@CustomAnnotation`来标记需要进行特定处理的类,并在`CustomService`中通过反射获取带有该注解的类,并进行相应的处理。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值