美文网首页
添加自己的starter

添加自己的starter

作者: 潦倒神仙 | 来源:发表于2020-08-11 22:02 被阅读0次
  1. 需要编写一个starter,
<?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>cn.hl</groupId>
    <artifactId>my-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>cn.hl</groupId>
            <artifactId>my-spring-boot-autoconfigure</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

2.创建autoconfigure项目


<?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>2.2.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.hl</groupId>
    <artifactId>my-spring-boot-autoconfigure</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>my-spring-boot-autoconfigure</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.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>
</project>

3.在项目添加MyProperties,用于接收配置文件中的属性

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "my")
@Data
public class MyProerties {

    private String first;

    private String second;

}

4.添加MyService,用于实现该项目功能

import lombok.Data;

@Data
public class MyService {
    MyProerties myProerties;
    public String testMethod(){

        return "This is my first method: " + myProerties.getFirst();
    }
}

  1. 添加MyServiceAutoConfigure,用于引入配置文件类,和将功能类注入容器
 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@ConditionalOnWebApplication
@EnableConfigurationProperties(MyProerties.class)
@Configuration
public class MyServiceAutoConfigure {

    @Autowired
    private MyProerties myProerties;

    @Bean
    public MyService myService() {
        MyService myService = new MyService();
        myService.setMyProerties(myProerties);
        return myService;
    }

}
  1. 在resourcr目录下创建META-INF目录,在该目录下新建spring.factories
  2. 在spring.factories中添加
org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.hl.starter.MyServiceAutoConfigure

8.打包autoconfigure项目,再打包starter项目。

相关文章

网友评论

      本文标题:添加自己的starter

      本文链接:https://www.haomeiwen.com/subject/njludktx.html