编写一个SpringBoot 工程的start 模块

it2023-07-26  69

编写一个SpringBoot 工程的start 模块

其实就是讲将打包的的jar工程(被依赖的工程)里的Bean的创建交给当前工程的Spring IOC,一般情况下Spring是不会扫描jar中的包的所以就无法将jar中的Bean 正常的加载到当前的Spring IOC中,但是SpringBoot 提供了一种方式在resources\META-INF\spring.factories 配置次jar的javaConfig 就可以实现将jar包中的Bean加载到当前Spring 环境中去

工程目录结构

配置 application.properties

#如果不放在config 目录下 模块被引用就无法读到此模块的配置文件 common.start=true common.annotation.start=true

spring.factories

#告诉spring那个配置类需要注入 自动注入的实现原理 org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.study.datacommonstart.DataCommonConfig #多个的配置方式 #org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.study.datacommonstart.DataCommonConfig,\ # com.study.datacommonstart.DataCommonConfig

gradle 主要的是 这两个配置,其他的配置按照个人的需求就ok

jar.enabled=true bootJar.enabled=false plugins { id 'org.springframework.boot' version '2.2.10.RELEASE' id 'io.spring.dependency-management' version '1.0.10.RELEASE' } dependencies { implementation 'org.springframework.boot:spring-boot-starter' testImplementation('org.springframework.boot:spring-boot-starter-test') { exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' } // implementation 'org.springframework.boot:spring-boot-configuration-processor' } //这两个得写 不然 被引用 打包回报错 jar.enabled=true bootJar.enabled=false jar { baseName("data-common-start") version("0.0.1") } //test { // useJUnitPlatform() //} import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.core.annotation.AliasFor; import org.springframework.stereotype.Component; import java.lang.annotation.*; //根据条件判断创建不创建Bean的注解 @Component @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @ConditionalOnProperty(prefix = "common.annotation",name = "start",havingValue = "true") public @interface CommDataComponent { @AliasFor(annotation = Component.class) String value() default ""; } import com.study.datacommonstart.annotation.CommDataComponent; import org.springframework.boot.context.properties.ConfigurationProperties; @CommDataComponent @ConfigurationProperties(prefix = "common.annotation") public class AnnotationConfigDemo { private boolean start; public boolean isStart() { return start; } public void setStart(boolean start) { this.start = start; } } import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @ConfigurationProperties(prefix = "common") @Component public class DemoConfig { private boolean start; public boolean isStart() { return start; } public void setStart(boolean start) { this.start = start; } }

javaConfig 类

import org.springframework.boot.SpringBootConfiguration; import org.springframework.context.annotation.ComponentScan; //正常情况下 SpringBoot不会扫描jar 但是可以同过在resources\META-INF\spring.factories 中配置配置类来进行 @SpringBootConfiguration @ComponentScan public class DataCommonConfig { public DataCommonConfig(){ System.out.println("DataCommonConfig auto start"); } }

完了 大部分的说明基本在注释中,可以自己看

最新回复(0)