springboot 新增并使用自定义starter

1、新增maven 项目,在项目的pom 文件中定义需要的依赖

<?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.2.4.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.fd</groupId>
   <artifactId>demo-starter</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>demo-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-actuator-autoconfigure</artifactId>
      </dependency>
 
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
         <exclusions>
            <exclusion>
               <groupId>org.junit.vintage</groupId>
               <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
         </exclusions>
      </dependency>
   </dependencies>
</project>

2、新增配置类,写好的配置项和默认的配置值,指明配置项前缀

package com.fd.demostarter.properties;
 
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
 
@Component
@ConfigurationProperties(prefix = "demo")
public class DemoProperties {
    private String name;
    private String msg;
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getMsg() {
        return msg;
    }
 
    public void setMsg(String msg) {
        this.msg = msg;
    }
}

3、新增自动装配类,使用@Configuration和@ConditionalOnMissingBean 来进行自动装配的实现

package com.fd.demostarter.config;
 
import com.fd.demostarter.properties.DemoProperties;
import com.fd.demostarter.service.DemoService;
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;
 
@ConditionalOnClass(DemoService.class) //如果存在DemoService类才加载此配置文件
@EnableConfigurationProperties(DemoProperties.class)
@Configuration
public class DemoAutoConfiguration {
    @Autowired
    private DemoProperties demoProperties;
 
    @Bean
    @ConditionalOnMissingBean(DemoService.class) //判断DemoService 类是否已经存在实现类,如果存在则忽略此bean 的注入
    public DemoService getDemoService() {
        return new DemoService(demoProperties);
    }
 
}
 
package com.fd.demostarter.service;
 
import com.fd.demostarter.properties.DemoProperties;
 
public class DemoService {
    private DemoProperties demoProperties;
 
    public DemoService(DemoProperties demoProperties) {
        this.demoProperties = demoProperties;
    }
 
    public String hello() {
        return "greet by" + demoProperties.getName() + " msg " + demoProperties.getMsg();
    }
}

4、声明Starter,两种方式

  • a、自动装配:在resources/META-INF/新建spring.factories文件,指定Starter的自动装配类;
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.fd.demostarter.config.DemoAutoConfiguration
  • b、手动装配:通过配置注解启动,如同官方的@EnableXXX
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({DemoAutoConfiguration.class})
public @interface EnableDemo {
}

5、如何使用demo-starter

只需要在项目中的pom.xml 添加对应的依赖就好,如果是手动装配的则需要在启动类中加入@EnableDemo,然后配置对应的需要配置的信息
application.properties 配置starter需要配置的属性
demo.msg=msssss
demo.name=xxxx

<dependency>
    <groupId>com.fd</groupId>
    <artifactId>demo-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值