编写一个SpringBoot 工程的start 模块

编写一个SpringBoot 工程的start 模块

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

工程目录结构
data-common-start 工程

配置
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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值