博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
精通SpringBoot——第五篇:写一个spring-boot-starter包
阅读量:7107 次
发布时间:2019-06-28

本文共 2613 字,大约阅读时间需要 8 分钟。

为了能更好的理解Springboot的自动配置和工作原理,我们今天来手写一个spring-boot-hello-starter。这个过程很简单,代码不多。接下来我们看看怎么开始实践。

1. 新建maven工程。

这块就不演示了,如果不会可以自行百度...啦啦啦,因为太简单了啊

2.新建一个properties类

/** * @author Lee * @// TODO 2018/7/25-9:21 * @description */@ConfigurationProperties(prefix = "customer")public class CustomerProperties {    private static final String DEFAULT_NAME = "Lensen";    private String name = DEFAULT_NAME;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}

2.创建一个服务类CustomerService

/** * @author Lee * @// TODO 2018/7/25-10:30 * @description */public class CustomerService {    private String name;    public String findCustomer(){        return "The Customer is " + name;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}

3.AutoConfiguration类

/** * @author Lee * @// TODO 2018/7/25-10:32 * @description */@Configuration@EnableConfigurationProperties(CustomerProperties.class)@ConditionalOnClass(CustomerService.class)@ConditionalOnProperty(prefix = "customer", value = "enabled", matchIfMissing = true)public class CustomerAutoConfiguration {    @Autowired    private CustomerProperties customerProperties;    @Bean    @ConditionalOnMissingBean(CustomerService.class)    public CustomerService customerService() {        CustomerService customerService = new CustomerService();        customerService.setName(customerProperties.getName());        return customerService;    }}

4. spring.factories配置

在src/main/resources新建文件夹META-INF,然后新建一个spring.factories文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\  com.developlee.configurer.CustomerAutoConfiguration

pom文件修改artifactId为spring-boot-hello-starter 打包成jar. 然后用mvn install 安装到本地mvn仓库。

5. 测试spring-boot-hello-start

新建springboot工程,在pom.xml文件引入安装的jar包

com.developlee
spring-boot-hello-starter
1.0-SNAPSHOT

在项目启动类中直接做测试:

@SpringBootApplication@RestControllerpublic class TestStarterApplication {    @Autowired    CustomerService customerService;    @GetMapping("/")    public String index(){        return customerService.getName();    }    public static void main(String[] args) {        SpringApplication.run(TestStarterApplication.class, args);    }}

application.properties文件配置下我们的customer.name

customer.name = BigBBrother

接下来启动项目,请求地址localhost:8080,即可看到页面上显示BigBBrother

到这我们已经完成了一个spring-boot-starter jar包的开发,有很多功能我们可以自己封装成一个jar,以后要用直接引用就行了~ 这样写代码是不是会有不一样的体验呢?

最后,以上示例代码可在我的中找到。

我的个人公众号:developlee的潇洒人生。
关注了也不一定更新,更新就不得了了。
qrcode_for_gh_2bd3f44efa21_258

转载地址:http://rgvhl.baihongyu.com/

你可能感兴趣的文章
安装并配置基于虚拟用户的vsftpd
查看>>
Activity间用Intent和Bundle传递参数
查看>>
【C1】scala入门
查看>>
http请求状态码和请求信息的含义
查看>>
关于假设检验
查看>>
MapReduce原理(分布式计算模型)----------总结
查看>>
Linux学习笔记(九)--命令学习(文件与目录查看)
查看>>
2013最新Ghost Windows 7硬盘安装法详细(图文)教程
查看>>
centos6.5 安装mysql5.6多实例(多配置文件)
查看>>
Redis配置文件主要功能说明
查看>>
为什么要"去IOE"
查看>>
ubuntu 12.04安装mongodb+eclipse erlang plugin+erlang runtime
查看>>
arm-linux-gcc4.4.3编译s3c2410平台linux内核
查看>>
gitlab服务器
查看>>
我的友情链接
查看>>
Case_Compressed Mode_Background
查看>>
python 利用pexpect进行多机远程命令执行
查看>>
Python学习系列 (第一章):Python 的简介
查看>>
【转载】addShutdownHook的用处
查看>>
CSS3学习3----举例
查看>>