SpringBoot --- 自定义 Starter

创建

1、需要创建一个新的空工程

2、新的工程需要引入两个模块

一个Maven 模块 作为启动器

一个SpringBoot 模块 作为自动配置模块

3、在Starter 模块(即启动器模块)的 pom.xml 引入 自动配置模块

    <!--启动器-->
<groupId>com.ling.starter</groupId>
<artifactId>ling-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies>
<!-- 引入自动配置模块-->
<dependency>
<groupId>com.ling.starter</groupId>
<artifactId>ling-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>

4、接下来主要的工作是编写自动配置包 ling-spring-boot-starter-autoconfigurer

首先,我们要明确,需要使用者配置的属性有哪些,需要编写一个类并用 @ConfigurationProperties 标注,用 prefix 明确配置的字首部分,约定后,使用者配置。

package com.ling.starter;

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

@ConfigurationProperties(prefix = "ling.hello")
public class HelloProperties {
private String prefix;
private String suffix; public String getPrefix() {
return prefix;
} public void setPrefix(String prefix) {
this.prefix = prefix;
} public String getSuffix() {
return suffix;
} public void setSuffix(String suffix) {
this.suffix = suffix;
}
}

5、接下来是编写Service 类,明确的是,以上的配置,用于哪些处理。

package com.ling.starter;

public class HelloService {

    HelloProperties helloProperties;

    public HelloProperties getHelloProperties() {
return helloProperties;
} public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
} public String syaHello(String name){ // 做业务处理
return helloProperties.getPrefix()+ "---" + name + "---" + helloProperties.getSuffix();
}
}

编写配置类(类似 xml ),注入Bean。

@Configuration     //表明这是一个配置类
@ConditionalOnWebApplication //判断是否是web 工程,是则配置
@EnableConfigurationProperties(HelloProperties.class) //需要注入哪些类到容器中
public class HelloAutoconfiguration { @Autowired
HelloProperties helloProperties; @Bean
public HelloService helloService(){
HelloService helloService = new HelloService();
helloService.setHelloProperties(helloProperties);
return helloService;
}
}

到这里自定义 starter 已经完成。

测试

1、创建一个web 工程,测试

2、导入自定义的 starter 依赖

<!--        测试 自定义 starter-->
<dependency>
<groupId>com.ling.starter</groupId>
<artifactId>ling-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

3、导入成功后,可以在依赖库中找到自定义 starter 的包,因为 ling-spring-boot-starter 的 pom 文件已经导入了 ling-spring-boot-starter-autoconfigurer 的依赖。所以会自动导入ling-spring-boot-starter 所依赖的包。

4、创建并配置 properties.yml 文件(配置是没有相关提示属于正常,按照约定好的字首和属性名配置即可)

ling:
hello:
prefix: 你好呀
suffix: 欢迎你。。。

5、编写Contriller 类

@RestController  //非JSON 数据,可用 @RestController
/* @RestController== @ResponseBody +@Controller*/
public class HWcontroller { @Autowired
HelloService helloService; @RequestMapping("/hello")
public String hello(){
return helloService.syaHello("son");
}
}

6、启动工程,访问

http://localhost:8080/hello

最新文章

  1. &quot;$cond&quot;
  2. UE4 VR 模式画面扭曲 解决方法
  3. Objective-C 编码建议
  4. 夺命雷公狗-----React---13--事件监听
  5. [转]Ionic最佳实践-使用模态窗口modal
  6. WCF - WAS Hosting
  7. C语言-03流程控制
  8. Sql操作表字段
  9. HDOJ(HDU) 2093 考试排名(Arrays.sort排序、类的应用)
  10. c# 与 c++ 编译
  11. Qt String 与char* char int之间的转换
  12. POJ 1915-Knight Moves (单向BFS &amp;amp;&amp;amp; 双向BFS 比)
  13. pycharm5工具免费分享及安装教程
  14. SQL优化指南
  15. PYTHON-模块 re subprocess
  16. 12集合(2)-----Set
  17. [R] 繪圖 Par 函数
  18. eclipse如何使用log4j详解,你get了吗???
  19. tensorflow学习笔记————分类MNIST数据集
  20. [20170728]oracle保留字.txt

热门文章

  1. 性能测试 -- docker安装influxdb
  2. PHP设计模式之----观察者模式
  3. MYSQL_详细基本命令
  4. SpringBoot之整合Quartz调度框架-基于Spring Boot2.0.2版本
  5. Python Tuple(元组) cmp()方法
  6. PHP zip_open() 函数
  7. layui实现图片上传
  8. 7.9 NOI模拟赛 数列 交互 高精 字符串
  9. JS 常用方法汇总(不定期更新)
  10. SpringCloud服务注册中心