SpringBoot从入门到精通—自定义starter

Spring Boot中提供了各种starter,starter可以理解为一个可拔插式的插件,当我们要使用的时候只用导入需要的starter即可。例如:你想使用jdbc插件,那么可以使用spring-boot-starter-jdbc;如果想使用mongodb,可以使用spring-boot-starter-data-mongodb。但是当我们需要的场景没有的时候我们可以来定制starter。

首先在IDEA中创建一个maven工程,在其中创建两个Model,一个是hello-spring-boot-starter,另一个是hello-spring-boot-starter-configurer,目结构如下:

要特别注意artifactId的命名规则: Spring官方starter通常命名为spring-boot-starter-{name},如 spring-boot-starter-web Spring官方建议非官方Starter命名应遵循{name}-spring-boot-starter的格式, 如mybatis-spring-boot-starter。

一般我们不会直接在starter中写配置,starter一般只一个空项目,然后主要的配置写在这个starter对应的autoconfigurer中,让starer依赖configurer就可以了。

1、在starter中引入对应的configurer依赖

starter一般是一个空模块,真正的实现放在configurer中,让starter依赖configurer,以后需要使用这个模块的时候只用引入starter就可以引入它所依赖的configurer。

    <dependencies>
        <!--在自定义的start中引入对应的配置类-->
        <dependency>
            <groupId>com.xust.iot</groupId>
            <artifactId>hello-spring-boot-starter-configurer</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

2、在hello-spring-boot-starter-configurer模块中写我们需要的配置

2.1 XxxPrperoties
package com.xust.iot.starter;

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

/**
 * 用户的配置信息类
 */
@ConfigurationProperties(prefix = "com.xust.user")
public class UserProperties {

    private String username;   //用户名
    private Integer age;       //用户年龄
    private String gender="M";   //用户性别

   //getter、setter。。。

2.2 核心业务类
package com.xust.iot.starter;

/**
 * 核心服务类
 */
public class UserService {

    private UserProperties userProperties;

    public UserService(){

    }

    public UserService(UserProperties userProperties) {
        this.userProperties = userProperties;
    }

    public String sayHello(){
        return "大家好,我叫"+userProperties.getUsername()+",今年"+userProperties.getAge()+"岁,"+"性别:"+userProperties.getGender();
    }
}
2.3 自动配置类XxxAutoConfigurer
package com.xust.iot.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;

@Configuration    //说明这是配置
@ConditionalOnWebApplication   //说明在Web环境下该类起作用
@EnableConfigurationProperties(UserProperties.class)   //自动配置UserProperties中的属性
public class UserServiceAutoConfigurer {

    @Autowired
    private UserProperties userProperties;

    @Bean
    public UserService userService() {
        UserService userService = new UserService(userProperties);
        return userService;
    }
}
2.4 spring.factories

然后需要在hello-spring-boot-starter-configurer的src/main/resources文件夹下新建META-INF文件夹然后新建spring.factories文件,配置这个类让他可以自动启动。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.xust.iot.starter.UserServiceAutoConfigurer
2.5 打包mvn clean install

使用Maven命令mvn clean install或直接使用IDEA提供的Maven插件执行install命令把我们的这个starter安装到本地Maven仓库。注意:如果提示没有找到pom文件的错误,那就使用命令行找到对应的项目执行Maven命令

测试一下 之后我们再新建一个普通的Spring Boot项目,引入我们自定义的starter

        <!--在别的项目中引入我们自己的starter,只需要引入starter就可以了-->
        <dependency>
            <groupId>com.xust.iot</groupId>
            <artifactId>hello-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

引入后的效果:

写一个controller来测试一下我们的starter是否有效

@Controller
public class UserController {

     @Autowired
     UserService userService;

     @ResponseBody
     @RequestMapping(value = "/user")
     public String hello(){
         return userService.sayHello();
     }
}

可以在主配置文件中配置UserProperties中的属性(不配置将会使用默认值)

com.xust.user.username=李四
com.xust.user.age=21
com.xust.user.gender=M

启动运行看看效果:

留言区

还能输入500个字符