Home
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 设计模式
  • JavaSE
  • JVM
  • JUC
  • Netty
  • CPP
  • QT
  • UE
  • Go
  • Gin
  • Gorm
  • HTML
  • CSS
  • JavaScript
  • vue2
  • TypeScript
  • vue3
  • react
  • Spring
  • SpringMVC
  • Mybatis
  • SpringBoot
  • SpringSecurity
  • SpringCloud
  • Mysql
  • Redis
  • 消息中间件
  • RPC
  • 分布式锁
  • 分布式事务
  • 个人博客
  • 弹幕视频平台
  • API网关
  • 售票系统
  • 消息推送平台
  • SaaS短链接系统
  • Linux
  • Docker
  • Git
GitHub (opens new window)
Home
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 设计模式
  • JavaSE
  • JVM
  • JUC
  • Netty
  • CPP
  • QT
  • UE
  • Go
  • Gin
  • Gorm
  • HTML
  • CSS
  • JavaScript
  • vue2
  • TypeScript
  • vue3
  • react
  • Spring
  • SpringMVC
  • Mybatis
  • SpringBoot
  • SpringSecurity
  • SpringCloud
  • Mysql
  • Redis
  • 消息中间件
  • RPC
  • 分布式锁
  • 分布式事务
  • 个人博客
  • 弹幕视频平台
  • API网关
  • 售票系统
  • 消息推送平台
  • SaaS短链接系统
  • Linux
  • Docker
  • Git
GitHub (opens new window)
  • starter开发
  • 自动装配原理
  • SpringBoot启动流程
  • SpringBoot
rui
2023-06-22

starter开发

# starter开发

案例:加密

创建springboot工程,导入加密所需依赖:

<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.15</version>
</dependency>
1
2
3
4
5

配置类指定类型:

digest.type=sha
1

接口与实现类:

public interface Digest {
    public String digest(String raw);
}
1
2
3

Md5Digest

public class Md5Digest implements Digest {
    @Override
    public String digest(String raw) {
        System.out.println("使用md5算法生成摘要");
        return DigestUtils.md5Hex(raw);
    }
}
1
2
3
4
5
6
7

ShaDigest

public class ShaDigest implements Digest {
    @Override
    public String digest(String raw) {
        System.out.println("使用sha256算法生成摘要");
        return DigestUtils.sha256Hex(raw);
    }
}
1
2
3
4
5
6
7

读取配置类信息:

@Component
@ConfigurationProperties(prefix = "digest")/*加载配置文件指定前缀的配置项*/
public class Settings {

    private String type;//配置项属性自动注入其中

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

容器启动时加载读取到的配置类信息:

@Configuration
@EnableConfigurationProperties(Settings.class)/*启动容器时就能拿到配置属性*/
public class Config {

    @Bean
    @ConditionalOnProperty(prefix="digest",name="type",havingValue="md5")/*按需加载*/
    public Digest md5Digest(){
        System.out.println("Loading md5");
        return new Md5Digest();
    }

    @Bean
    @ConditionalOnProperty(prefix="digest",name="type",havingValue="sha")
    public Digest shaDigest(){
        System.out.println("Loading sha");
        return new ShaDigest();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

测试:

@SpringBootTest
class ConfigTest {

    @Resource
    private Digest digest;

    @Test
    public void testMd5(){
        System.out.println(digest.digest("rui"));
    }

}
1
2
3
4
5
6
7
8
9
10
11
12

新需求:该starter给其它项目使用

新建保存元数据的目录:资源目录下新建META-INF.spring.factories,目的使该项目的启动类失效,其它项目使用时直接在其IOC容器中加载starter的Config;

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.rui.digest.conf.Config
1

maven项目打包:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>com.rui.digest.DigestSpringBootStartApplication</mainClass>
            </configuration>
            <executions>
                <execution>
                    <id>repackage</id>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

其它项目引用时,配置文件配置:

digest:
	type: md5
1
2
自动装配原理

自动装配原理→

Theme by Vdoing | Copyright © 2021-2024
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式