自定义starter自动导入配置

一、先准备好项目

  • wx-spring-boot-starter-config: 处理自动配置的项目
  • wx-spring-boot-starter:空项目,引入wx-spring-boot-starter-config
  • test-my-starter:测试starter的项目,是一个web项目,引入wx-spring-boot-starter

二、准备 wx-spring-boot-starter-config项目

1、准备pom文件,引入spring boot ,lombok是为了减少gettersetter代码

<?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>
    <groupId>com.wx.starter</groupId>
    <artifactId>wx-spring-boot-starter-config</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>wx-spring-boot-starter-config</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
        </dependency>
    </dependencies>
</project>

2、准备配置属性类,绑定前缀是my.config的配置

@Data
@ConfigurationProperties(prefix = "my.config")
public class MyProperties {
    private String name;
    private Integer age;
}

3、准备测试类,提供一个方法,供其它项目测试获取配置

@Setter
public class MyService {
    private MyProperties myProperties;
    public String findMyProperties() {
        return myProperties.getName() + "_" + myProperties.getAge();
    }
}

4、准备自动配置类,并将MyService注入容器,将MyProperties置入MyService

@Configuration
@ConditionalOnWebApplication //只有web项目才会生效
@EnableConfigurationProperties(MyProperties.class)
public class MyConfig {
    @Autowired
    private MyProperties myProperties;
    @Bean
    public MyService myService() {
        MyService myService = new MyService();
        myService.setMyProperties(myProperties);
        return myService;
    }
}

5、准备spring.factories文件,配置EnableAutoConfiguration使spring boot可以识别到自动配置类
resources下面建一个META-INF目录,在下面建一个spring.factories文件,配置如下代码:指向自己的自动配置类

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.wx.starter.config.MyConfig

三、准备 wx-spring-boot-starter项目

1、pom文件引入 wx-spring-boot-starter-config项目

<dependencies>
    <dependency>
        <groupId>com.wx.starter</groupId>
        <artifactId>wx-spring-boot-starter-config</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>

2、使用maven的installwx-spring-boot-starter-config安装到maven仓库中
3、使用maven的installwx-spring-boot-starter安装到maven仓库中,顺序不能反

四、准备test-my-starter项目

1、准备pom文件,需要准备一个web项目,因为上面自动配置类起作用的条件配置了@ConditionalOnWebApplication,并引入自定义的wx-spring-boot-starter

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.wx.starter</groupId>
        <artifactId>wx-spring-boot-starter-config</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>

3、在application.properties文件中配置属性

my.config.name=aa
my.config.age=10

4、使用JUnit测试获取配置属性代码

@RunWith(SpringRunner.class)
@SpringBootTest
public class MyServiceTest {
    @Autowired
    private MyService myService;
    @Test
    public void testHello() {
        System.out.println(myService.findMyProperties());
    }
}

控制台输出:


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。