spring boot2自定义starter

1.新建两个项目 starter 和starter-configure (非spring -boot 官方的用xxx-spring-boot-starter)

在这里插入图片描述

2.auto-configure pom.xml
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>com.kelly</groupId>
    <artifactId>kelly-spring-boot-starter-autoconfigure</artifactId>
    <version>1.0-SNAPSHOT</version>
    <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>
    </dependencies>
3. starter pom.xml
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.5.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.kelly</groupId>
	<artifactId>kelly-spring-boot-starter</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>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-web</artifactId>
		</dependency>
		<dependency>
			<groupId>com.kelly</groupId>
			<artifactId>kelly-spring-boot-starter-autoconfigure</artifactId>
			<version>1.0-SNAPSHOT</version>
		</dependency>
	</dependencies>
4. starter 实现
1)配置文件
package com.kelly.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "hello")
public class HelloProperties {
    private String welcomeTip;
    private String openTip;

    public String getWelcomeTip() {
        return welcomeTip;
    }

    public void setWelcomeTip(String welcomeTip) {
        this.welcomeTip = welcomeTip;
    }

    public String getOpenTip() {
        return openTip;
    }

    public void setOpenTip(String openTip) {
        this.openTip = openTip;
    }
}
2)服务
package com.kelly.service;

import com.kelly.config.HelloProperties;

public class HelloService {

    private HelloProperties helloProperties ;

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

    public void setHelloProperties(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }

    public String hello(String name ){
        return helloProperties.getOpenTip()+" "+name +" "+ helloProperties.getWelcomeTip();
    }
}


3)autoconfigure配置
package com.kelly;

import com.kelly.config.HelloProperties;
import com.kelly.service.HelloService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@ConditionalOnWebApplication
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceConfiguration {
    @Resource
    private HelloProperties helloProperties ;

    @Bean
    public HelloService helloService (){
        HelloService helloService = new HelloService();
        helloService.setHelloProperties(helloProperties);
        return helloService;
    }
}

5. install autoconfigure , install starter
6. 使用示例
1) 添加依赖
<dependency>
			<groupId>com.kelly</groupId>
			<artifactId>kelly-spring-boot-starter-autoconfigure</artifactId>
			<version>1.0-SNAPSHOT</version>
</dependency>
2)添加配置
hello.open-tip=Hello 
hello.welcome-tip=Welcome to ShenZhen
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值