自定义spring boot start
自定义spring boot的start,是在spring boot的基础上,删除主启动和yml文件,并在resource目录下新建META-INF文件夹,在文件夹下新建spring.factories文件。
因为我自定义的start需要读取配置文件,因此pom.xml中引入下面的依赖。
//这个类是为了自动装配 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>2.1.0.RELEASE</version> </dependency> //下面链接是官网对于这个以来的解释 //https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/html/configuration-metadata.html#configuration-metadata-annotation-processor //这个依赖是为了读取配置文件 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <version>2.1.0.RELEASE</version> <optional>true</optional> </dependency>自定义一个类,让他实现CommandLineRunner,并在类上加上@Compoent注解,使项目在启动的过程中会自启动。重写run方法,在run方法中执行具体的处理过程。
如下:

在spring.factories文件中,指向自定义的类,如下:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.landspace.demo.filter.StartBeanPostProcessor,\
com.landspace.demo.runner.QueueStartUp
多个类以反斜杠区分。
这样在idea上执行maven--install,生成对应的jar包,在其他springboot项目的pom文件中引入我们自定义的start就可以正常使用了。
浙公网安备 33010602011771号