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

源码参见: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 
  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值