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 {};
}
启动类
@Spr