设为首页 加入收藏

TOP

SpringBoot系列三:SpringBoot自定义Starter(二)
2018-11-21 22:08:37 】 浏览:587
Tags:SpringBoot 系列 定义 Starter
pendencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>

5) 在1.2节创建的 helloworld-spring-boot-starter 的 pom 下模块引入本节创建的 autoconfigure 模块:

<dependency>
    <groupId>com.seagetech.spring.boot</groupId>
    <artifactId>helloworld-spring-boot-starter-autoconfigure</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

1.4 实现HelloWorld自动配置

1) 在 helloworld 包下新建 HelloWorldProperties 类,作为配置类,目的是后面使用这个自动配置,直接可以在 application.properties 文件中配置:

package com.seagetech.spring.boot.helloworld;

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

/**
 * HelloWorld属性配置类
 * @auther: wangzb
 * @date: 2018/11/13 22:21
 */
@ConfigurationProperties(prefix = "hello")
public class HelloWorldProperties {
    /**
     * 打招呼的内容,默认为“World!”
     */
    private String msg = "World!";

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

2) 新建 HelloWorldService 接口,并实现:

接口:

/**
 * @auther: wangzb
 * @date: 2018/11/13 22:28
 */
public interface HelloWorldService {
    /**
     * 打招呼方法
     * @param name 人名,向谁打招呼使用
     * @return
     */
    String sayHello(String name);
}

实现类:

/**
 * @auther: wangzb
 * @date: 2018/11/13 22:33
 */
@Component
public class HelloWorldServiceImpl implements HelloWorldService{

    @Autowired
    private HelloWorldProperties helloWorldProperties;

    /**
     * 打招呼方法
     *
     * @param name 人名,向谁打招呼使用
     * @return
     */
    @Override
    public String sayHello(String name) {
        return "Hello " + name + " " + helloWorldProperties.getMsg();
    }
}

3) 新建 HelloWorldAutoConfiguration 配置类

/**
 * @auther: wangzb
 * @date: 2018/11/13 22:37
 */
//定义为配置类
@Configuration
//在web条件下成立
@ConditionalOnWebApplication
//启用HelloWorldProperties配置功能,并加入到IOC容器中
@EnableConfigurationProperties({HelloWorldProperties.class})
//导入HelloWorldService组件
@Import(HelloWorldServiceImpl.class)
public class HelloWorldAutoConfiguration {
}

1.5 创建 spring.factories 文件

在 SpringBoot自动配置原理 中,我们提到 @EnableAutoConfiguration 的关键功能是使用 SpringFactoriesLoader.loadFactoryNames 方法来扫描具有 META-INF/spring.factories 文件的 jar 包,这样我们的自动配置类才能生效,所以我们在 autoconfigure 模块的 resources 下创建 META-INF/spring.factories 文件:

在 spring.factories 中写入:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.seagetech.spring.boot.helloworld.HelloWorldAutoConfiguration

2 helloworld 自动配置的使用

创建好 helloworld 自动配置项目后,接下来就是使用了,我们在 Idea 中使用 install 将1节中创建好的 starter/autoconfigure 模块打包到本地 Maven 创库,然后在 SpringBoot入门 章节创建的 sping-boot-demo 项目中使用,在其 pom 文件中引入:

<dependency>
    <groupId>com.seagetech.spring.boot</groupId>
    <artifactId>helloworld-spring-boot-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

在主配置类路径下创建 HelloWorldController :

package com.seagetech.springbootdemo;

import com.seagetech.spring.boot.helloworld.HelloWorldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframew
首页 上一页 1 2 3 4 下一页 尾页 2/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇让SpringBoot启动更快一点 下一篇SpringBoot系列二:SpringBoot自..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目