自定义starter

starter:
1. 这个场景使用的依赖是什么(jar包)
2. 如何编写自动配置 参考WebMvcAutoConfiguration.java
SpringBoot自学好几天 中途开始写笔记 自定义starter 20190224

@Configuration  //指定这个类是配置类
@ConditionalOnXXX //在条件成立的情况下 生效
@AutoConfigureOrder //指定自动配置类的顺序 
@AutoConfigureAfter //指定自动配置类在哪个类加载之后
@Bean //给容器中添加组件 
@ConfigurationProperties结合相关的xxxProperties类 来绑定相关的配置
@EnableConfigurationProperties //让xxxProperties 类生效 并加入到容器中

自动配置类要能加载 必须把自动配置类指定在类路径下/META-INF/spring.factories
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\
org.springframework.boot.autoconfigure.cloud.CloudServiceConnectorsAutoConfiguration,\
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration,\
org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration,\

  1. 模式
    SpringBoot自学好几天 中途开始写笔记 自定义starter 20190224
    启动器只用来做依赖导入,
    专门写一个自动配置模块
    启动器依赖自动配置模块
    别人只需要引入启动器 (starter)
    当然:也可以都写到启动器里边
    约定名称:
  • mybatis-spring-boot-starter : 自动以启动器名+spring-boot-starter
  1. 创建工程
    SpringBoot自学好几天 中途开始写笔记 自定义starter 20190224
    SpringBoot自学好几天 中途开始写笔记 自定义starter 20190224
  2. 自动配置Modules
    SpringBoot自学好几天 中途开始写笔记 自定义starter 20190224
    SpringBoot自学好几天 中途开始写笔记 自定义starter 20190224
    SpringBoot自学好几天 中途开始写笔记 自定义starter 20190224
  3. 再创建一个模块 自动配置相关的东西
    SpringBoot自学好几天 中途开始写笔记 自定义starter 20190224
    SpringBoot自学好几天 中途开始写笔记 自定义starter 20190224
    SpringBoot自学好几天 中途开始写笔记 自定义starter 20190224
    SpringBoot自学好几天 中途开始写笔记 自定义starter 20190224
  4. example-springboot-starter pom.xml 引入我们的example-springboot-starter-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>

    <groupId>com.example.starter</groupId>
    <artifactId>example-springboot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--启动器-->
    <dependencies>
        <!--引入自动配置-->
        <dependency>
            <groupId>com.example.starter</groupId>
            <artifactId>example-springboot-starter-autoconfigure</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
    
</project>
  1. 自动配置 没用的模块删掉
    SpringBoot自学好几天 中途开始写笔记 自定义starter 20190224
    pom.xml 中的test也不要了 插件也不要了
<?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.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example.starter</groupId>
    <artifactId>example-springboot-starter-autoconfigure</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>example-springboot-starter-autoconfigure</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
       <!--引入spring-boot-starter;所有starter的基本配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

       
    </dependencies>

 

</project>

  1. 编写类
    SpringBoot自学好几天 中途开始写笔记 自定义starter 20190224
    HelloProperties 属性映射
package com.example.starter;

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

/**
 * @author LM
 * @create 2019-02-24 10:17
 */
//绑定配置文件中以example.hello 开头的所有属性
@ConfigurationProperties(prefix = "example.hello")
public class HelloProperties {
    private String  prefix;
    private  String suffix;

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }
}

HelloService类方法 使用到属性文件的属性

package com.example.starter;

/**
 * @author LM
 * @create 2019-02-24 10:16
 */
public class HelloService {

    HelloProperties helloProperties;


    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

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

    public String  sayHello(String name){
        //从配置中拿属性
        return helloProperties.getPrefix()+ name +helloProperties.getSuffix();
    }
}

HelloServiceAutoConfiguration 自动配置类

package com.example.starter;

import 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;

/**
 * @author LM
 * @create 2019-02-24 10:22
 */
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {

    @Autowired
    HelloProperties helloProperties;

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

spring.factories 配置好这个 才能!!生效

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.starter.HelloServiceAutoConfiguration

有报错 把test文件夹也删了

  1. example-springboot-starter-autoconfigure 安装到仓库
    SpringBoot自学好几天 中途开始写笔记 自定义starter 20190224
    可能会报错:Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.5.6.RELEASE:repackage failed: Unable to find main class
    修改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>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example.starter</groupId>
    <artifactId>example-springboot-starter-autoconfigure</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>example-springboot-starter-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>


    </dependencies>



</project>

SpringBoot自学好几天 中途开始写笔记 自定义starter 20190224
11. example-springboot-starter安装到仓库
SpringBoot自学好几天 中途开始写笔记 自定义starter 20190224
SpringBoot自学好几天 中途开始写笔记 自定义starter 20190224

  1. 测试 创建项目
    SpringBoot自学好几天 中途开始写笔记 自定义starter 20190224
    SpringBoot自学好几天 中途开始写笔记 自定义starter 20190224
    SpringBoot自学好几天 中途开始写笔记 自定义starter 20190224
    SpringBoot自学好几天 中途开始写笔记 自定义starter 20190224
  2. 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 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.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example.starter.test</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-starter-test</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>

        <!--引入自定义starter-->
        <dependency>
            <groupId>com.example.starter</groupId>
            <artifactId>example-springboot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </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>
  1. 编写测试类
package com.example.starter.test.springboot.controller;

import com.example.starter.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author LM
 * @create 2019-02-24 21:48
 */
@RestController
public class HelloController  {
    @Autowired
    HelloService helloService;
    @GetMapping("/hello")
    public String hello() {
        return helloService.sayHello("测试");
    }
}
  1. 配置文件
example.hello.prefix=EXAMPLE
example.hello.suffix=HELLO
  1. 测试
    SpringBoot自学好几天 中途开始写笔记 自定义starter 20190224

相关文章:

  • 2022-01-06
  • 2021-07-08
  • 2021-09-07
  • 2022-01-18
  • 2021-04-17
  • 2021-09-10
猜你喜欢
  • 2021-12-30
  • 2021-07-31
  • 2021-05-22
  • 2021-08-26
  • 2021-09-19
  • 2021-07-10
  • 2021-08-21
相关资源
相似解决方案