编写自己的Spring Boot Starter
2020-06-29 18:19:04 809
1.新建一个maven项目
命名规则统一是xxx-spring-boot-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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.22xcode</groupId>
<artifactId>mystarter-spring-boot-starter</artifactId>
<version>1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
</project>
2.功能实现类
package com.test;
/**
* 功能实现类
*/
public class GetHashCodeClass {
private String target;
public GetHashCodeClass(String target) {
this.target = target;
}
public String getHashCode() {
return String.valueOf(this.target.hashCode());
}
}
3.配置读取类
package com.test;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* AutoConfigurationProperties
*
* @since 2020/06/29 13:57
*/
@ConfigurationProperties("target.string")
public class AutoConfigurationProperties {
private String target;
public String getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
}
4.自动配置类
package com.test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* AutoConfigurationClass
*
* @since 2020/06/29 14:01
*/
@Configuration
@EnableConfigurationProperties(AutoConfigurationProperties.class)
@ConditionalOnClass(GetHashCodeClass.class)
public class AutoConfigurationClass {
@Autowired
private AutoConfigurationProperties autoConfigurationProperties;
@ConditionalOnMissingBean
@Bean
public GetHashCodeClass getHashCodeClass() {
return new GetHashCodeClass(autoConfigurationProperties.getTarget());
}
}
5.spring.factories
在resources目录下,新建 META-INF/spring.factories
文件内容
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.test.AutoConfigurationClass
6.application.yml
target:
string:
target: nihao
7.完整工程目录
8.打包
工程目录下
mvn install
9.测试starter
再新建一个maven工程
完整maven依赖
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>testStarter</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.22xcode</groupId>
<artifactId>mystarter-spring-boot-starter</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
</dependencies>
</project>
10.启动类
package test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* test.Test
*
* @since 2020/06/29 16:25
*/
@SpringBootApplication
public class Test {
public static void main(String[] args) {
SpringApplication.run(Test.class, args);
}
}
11.BeanTest
package test;
import com.test.GetHashCodeClass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
/**
* BeanTest
*
* @since 2020/06/29 16:30
*/
@Component
public class BeanTest implements CommandLineRunner {
@Autowired
private GetHashCodeClass getHashCodeClass;
/**
* 实际的入口函数
*
* @param args
* @throws Exception
*/
public void run(String... args) throws Exception {
System.out.println(getHashCodeClass.getHashCode());
}
}
12.application.yml
target:
string:
target: yyy
13.运行
OK
参考https://blog.csdn.net/qq_35794278/article/details/88662827