定义springboot starter的两种方式

springboot starter 命令规则

首先了解一下starter的命名规范
如果是springboot官方的starter,命令格式为spring-boot-starter-xxxxx
如果是我们开发starter,命令格式为xxxxx-spring-boot-starter

1.自动注入式,引入依赖后就可直接注入

第一步:创建pom项目,引入springboot依赖

<properties>
        <spring-boot.version>2.1.9.RELEASE</spring-boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <optional>true</optional>
        </dependency>

        <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-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

第二步:编写代码
创建一个类TestProperties.java,接受配置文件中的配置内容。在application.properties文件中以这种格式配置:spring.test.id=1;就能将配置的值注入到id字段中。

@ConfigurationProperties(prefix = TestProperties.PREFIX)
public class TestProperties {

	public static final String PREFIX = "spring.test";

	private String id;
	private String name;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "TestProperties [id=" + id + ", name=" + name + "]";
	}

}

创建类TestPrinter,模拟用于注入的执行类

public class TestPrinter {
	private TestProperties testProperties;
	public TestPrinter (TestProperties testProperties) {
		this.testProperties = testProperties;
	}
	
	public void print() {
		System.out.println("test TestPrinter:"+testProperties.toString());
	}

}

创建类TestConfiguration,用于springboot自动注入

@Configuration
@ConditionalOnClass(TestPrinter.class)
@EnableConfigurationProperties(TestProperties.class)
public class TestConfiguration {
	
	@Autowired
	private TestProperties testProperties;
	
	@Bean
	@ConditionalOnProperty(name = "test.enable", havingValue = "true", matchIfMissing = true)
	public TestPrinter testPrinter() {
		return new TestPrinter(testProperties);
	}

}

最后:springboot的自动注入规则是会加载jar包中resource目录下META-INF/spring.factories文件

在这里插入图片描述
文件内容如下(里面的类路径需要换成你自己的):

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
springboot.starter.test.TestConfiguration

测试:新建一个maven项目,引入starter依赖,注入TestPrinter,调用testPrinter.print()方法,验证是否成功。

2.开关式,引入依赖后,还需要开启(@EnableXXX)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章