制作SpringBoot Starter
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.1</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.charkey</groupId> <artifactId>digest-spring-boot-starter</artifactId> <version>0.0.1-SNAPSHOT</version> <name>digest-spring-boot-starter</name> <description>digest-spring-boot-starter</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.15</version> </dependency> </dependencies> <build> <plugins> <!-- <plugin>--> <!-- <groupId>org.springframework.boot</groupId>--> <!-- <artifactId>spring-boot-maven-plugin</artifactId>--> <!-- </plugin>--> </plugins> </build> </project>
application.properties
spring.application.name=digest-spring-boot-starter
digest.type=sha
Digest.java
package com.charkey.digest; public interface Digest { String digest(String raw); }
Md5Digest.java
package com.charkey.digest.impl; import com.charkey.digest.Digest; import org.apache.commons.codec.digest.DigestUtils; public class Md5Digest implements Digest { @Override public String digest(String raw) { System.out.println("使用MD5生成。。。"); return DigestUtils.md2Hex(raw); } }
ShaDigest.java
package com.charkey.digest.impl; import com.charkey.digest.Digest; import org.apache.commons.codec.digest.DigestUtils; public class ShaDigest implements Digest { @Override public String digest(String raw) { System.out.println("使用SHA生成。。。"); return DigestUtils.sha1Hex(raw); } }
Settings.java
package com.charkey.digest.conf; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "digest") public class Settings { private String type; public String getType() { return type; } public void setType(String type) { this.type = type; } }
Config.java
package com.charkey.digest.conf; import com.charkey.digest.Digest; import com.charkey.digest.impl.Md5Digest; import com.charkey.digest.impl.ShaDigest; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @EnableConfigurationProperties(Settings.class) public class Config { @Bean @ConditionalOnProperty(prefix = "digest",name = "type",havingValue = "md5") public Digest md5Digest(){ System.out.println("MD5 Config init loading ..."); return new Md5Digest(); } @Bean @ConditionalOnProperty(prefix = "digest",name = "type",havingValue = "sha") public Digest shaDigest(){ System.out.println("SHA Config init loading ..."); return new ShaDigest(); } }
DigestSpringBootStarterApplication.java
package com.charkey.digest; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DigestSpringBootStarterApplication { public static void main(String[] args) { SpringApplication.run(DigestSpringBootStarterApplication.class, args); } }
}
创建 resources/META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.charkey.digest.conf.Config
引用: