自定义一个springboot starter

新建一个maven项目

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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.20.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>simple-spring-boot-starter</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>simple-spring-boot-starter</name>
	<description>Demo project for Spring Boot</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-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-autoconfigure</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<!-- 注意:下面这个插件不能有,否则打的包没法使用 -->
	<build>
		<!--<plugins>-->
			<!--<plugin>-->
				<!--<groupId>org.springframework.boot</groupId>-->
				<!--<artifactId>spring-boot-maven-plugin</artifactId>-->
			<!--</plugin>-->
		<!--</plugins>-->
	</build>

</project>

配置信息:

@ConfigurationProperties(prefix = "simple")
public class SimpleProperties {

    private String name = "XXX";

    public String getName() {
        return name;
    }

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

}

功能实现:

public class SimpleService {
    private String name;

    public String printName(){
        return "The name is : " + name;
    }

    public String getName() {
        return name;
    }

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

 

导入配置:

@Configuration
@EnableConfigurationProperties(SimpleProperties.class)
@ConditionalOnClass(SimpleService.class)
public class SimpleAutoConfigure {

    @Autowired
    private SimpleProperties simpleProperties;

    @Bean
    @ConditionalOnMissingBean(SimpleService.class)
    public SimpleService simpleService() {
        // @ConditionalOnMissingBean:当spring容器中没有这个Bean的时候才进行下一步
        SimpleService simpleService = new SimpleService();
        simpleService.setName(simpleProperties.getName());
        return simpleService;
    }
}

src/resources/META-INF/ 下新建文件 spring.factories 写入以下内容:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.SimpleAutoConfigure

mvn:install 打包到本地仓库中

 

测试一下

再新建一个maven项目,pom.xml中引入上面的jar包

        <dependency>
            <groupId>com.example</groupId>
            <artifactId>simple-spring-boot-starter</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

application.properties 配置文件中配置:simple.name=wanggq

编写测试代码:

@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class SimpleStarterTest {

    @Autowired
    private SimpleService simpleService;

    @Test
    public void test(){
        System.out.println(simpleService.printName());
    }
}

输出如下:

The name is : wanggq

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值