精通SpringBoot——第五篇:写一个spring-boot-starter包
为了能更好的理解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包
<dependency> <groupId>com.developlee</groupId> <artifactId>spring-boot-hello-starter</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
在项目启动类中直接做测试:
@SpringBootApplication @RestController public 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,以后要用直接引用就行了~ 这样写代码是不是会有不一样的体验呢?
最后,以上示例代码可在我的github.com中找到。
我的个人公众号:developlee的潇洒人生。
关注了也不一定更新,更新就不得了了。

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Java最全异常讲解
导引问题 实际工作中,遇到的情况不可能是非常完美的。比如:你写的某个模块,用户输入不一定符合你的要求、你的程序要打开某个文件,这个文件可能不存在或者文件格式不对,你要读取数据库的数据,数据可能是空的等。我们的程序再跑着,内存或硬盘可能满了 等等。 软件程序在运行过程中,非常可能遇到刚刚提到的这些异常问题,我们叫异常,英文是:Exception,意思是例外。这些,例外情况,或者叫异常,怎么让我们写的程序做出合理的处理。而不至于程序崩溃。 常见的错误:用户输入错误设备错误。硬件问题,比如打印机关掉、服务器问题物理限制。磁盘满了代码限制。数组下标越界等设计良好的程序应该在异常发生时提供处理这些错误的方法,使得程序不会因为异常的发生而终断或产生不可预见的结果。 如果没有异常处理机制,那么:两个坏处:1.逻辑代码和错误处理代码放一起!2.程序员本身需要考虑的例外情况较复杂,对程序员本身要求较高!异常机制就是当程序出现错误,程序如何安全退出的机制 异常(Exception)的概念 Java 如何处理异常? 第一个异常示例和解析: public static void main(String[] a...
- 下一篇
Scala的类层级讲解
Scala的类层级 Scala里,每个类都继承自通用的名为Any的超类。 因为所有的类都是Any的子类,所以定义在Any中的方法就是“共同的”方法:它们可以被任何对象调用。 Scala还在层级的底端定义了一些类,如Null和Nothing,扮演通用的子类。 即,Any是所有其他类的超类,Nothing是所有其他类的子类。 类层级图如下: Any类 层级的顶端是Any类,定义了下列方法: final def ==(that: Any): Boolean final def !=(that: Any): Boolean def equals(that: Any): Boolean def hashCode: Int def toString: String 解释:因为每个类都继承自Any,所以Scala程序里的每个对象都能用==、!=或equals比较,用hashCode做散列,以及用toString格式化。 AnyVal, Unit和AnyRef 根类Any有两个子类:AnyVal和AnyRef。 AnyVal AnyVal是Scala里每个内建++值类++的父类。有9个这样的值类:By...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS7设置SWAP分区,小内存服务器的救世主
- SpringBoot2全家桶,快速入门学习开发网站教程
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- Windows10,CentOS7,CentOS8安装Nodejs环境
- Hadoop3单机部署,实现最简伪集群
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- 设置Eclipse缩进为4个空格,增强代码规范
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池