crm开发定制【云原生】SpringCloud-Spring Boot Starter使用测试

目录


Spring Boot Starter是什么?

 Spring Boot Starter 是在 SpringBoot crm开发定制组件中被提出来的一种概念、crm开发定制简化了很多烦琐的配置、crm开发定制通过引入各种 Spring Boot Starter crm开发定制包可以快速搭建出一个crm开发定制项目的脚手架。

crm开发定制比如我们经常用的一些:

spring-boot-starter-web:

spring-boot-starter-data-redis:

spring-boot-starter-data-mongodb:

spring-boot-starter-data-jpa:

spring-boot-starter-activemq:

crm开发定制总体来说就是 starter crm开发定制是一种对依赖的合成。

crm开发定制以前传统的做法

在没有 starter 之前,传统的SSM项目、假如我想要在 Spring 中使用 jpa,可能需要做以下操作: 

首先在 Maven 中引入使用的数据库的依赖>>然后引入 jpa 的依赖>>在xml中配置一些属性信息>>调试调用直到可以正常运行

上面这样的操作会有一些问题、比如:

如果过程比较繁琐,这样一步步操作会增加出错的可能性

在配置的时候也会划掉大量的时间、对于新手和小白不太友好

使用 Spring Boot Starter 之后

 starter 的主要目的就是为了解决上面的这些问题

starter 的理念:

starter 会把所有用到的依赖都给包含进来,避免了开发者自己去引入依赖所带来的麻烦。需要注意的是不同的 starter 是为了解决不同的依赖,所以它们内部的实现可能会有很大的差异,例如 jpa 的 starter 和 Redis 的 starter 可能实现就不一样,这是因为 starter 的本质在于 synthesize,这是一层在逻辑层面的抽象,也许这种理念有点类似于 Docker,因为它们都是在做一个 “包装” 的操作,如果你知道 Docker 是为了解决什么问题的,也许你可以用 Docker 和 starter 做一个类比。

starter 的实现:

虽然不同的 starter 实现起来各有差异,但是他们基本上都会使用到两个相同的内容:ConfigurationProperties 和 AutoConfiguration。因为 Spring Boot 坚信 “约定大于配置” 这一理念,所以我们使用 ConfigurationProperties 来保存我们的配置,并且这些配置都可以有一个默认值,即在我们没有主动覆写原始配置的情况下,默认值就会生效,这在很多情况下是非常有用的。除此之外,starter 的 ConfigurationProperties 还使得所有的配置属性被聚集到一个文件中(一般在 resources 目录下的 application.properties),这样我们就告别了 Spring 项目中 XML 地狱。

上面的 starter 依赖的 jar 和我们自己手动配置的时候依赖的 jar 并没有什么不同,所以我们可以认为 starter 其实是把这一些繁琐的配置操作交给了自己,而把简单交给了用户。除了帮助用户去除了繁琐的构建操作,在 “约定大于配置” 的理念下,ConfigurationProperties 还帮助用户减少了无谓的配置操作。并且因为 application.properties 文件的存在,即使需要自定义配置,所有的配置也只需要在一个文件中进行,使用起来非常方便。采用的starter都上面都给大家列出来了。

 创建Spring Boot Starter步骤

创建 starter 项目

创建ConfigurationProperties 用来保存配置信息

创建 AutoConfiguration,引用定义好的配置信息

在 AutoConfiguration 实现所有 starter 应该完成的操作,并且把这个类加入 spring.factories 配置文件中进行声明 

打包项在 SpringBoot 项目中引入该项目依赖,然后就可以使用该 starter 了

具体操作步骤:

在idea新建一个starter项目、直接执行下一步即可生成项目。

 在xml中加入如下配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <artifactId>http-starter</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <!-- 自定义starter都应该继承自该依赖 -->
  9. <!-- 如果自定义starter本身需要继承其它的依赖,可以参考 https://stackoverflow.com/a/21318359 解决 -->
  10. <parent>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starters</artifactId>
  13. <version>1.5.2.RELEASE</version>
  14. </parent>
  15. <dependencies>
  16. <!-- 自定义starter依赖此jar包 -->
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter</artifactId>
  20. </dependency>
  21. <!-- lombok用于自动生成get、set方法 -->
  22. <dependency>
  23. <groupId>org.projectlombok</groupId>
  24. <artifactId>lombok</artifactId>
  25. <version>1.16.10</version>
  26. </dependency>
  27. </dependencies>
  28. </project>

创建 proterties 类来保存配置信息

  1. @ConfigurationProperties(prefix = "http")
  2. @Getter
  3. public class HttpProperties {
  4. // 如果配置文件中配置了http.url属性,则该默认属性会被覆盖
  5. private String url = "https://blog.csdn.net/weixin_39709134?type=blog";
  6. }

创建业务类:

  1. @Setter
  2. @Getter
  3. public class HttpClient {
  4. private String url;
  5. // 根据url获取网页数据
  6. public String getHtml() {
  7. try {
  8. URL url = new URL(this.url);
  9. URLConnection urlConnection = url.openConnection();
  10. BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "utf-8"));
  11. String line = null;
  12. StringBuilder sb = new StringBuilder();
  13. while ((line = br.readLine()) != null) {
  14. sb.append(line).append("\");
  15. }
  16. return sb.toString();
  17. } catch (Exception e) {
  18. e.printStackTrace();
  19. }
  20. return "error";
  21. }
  22. }

这个业务类包含了url 属性和一个 getHtml 方法,用于获取一个网页 HTML 数据

创建 AutoConfiguration

  1. @Configuration
  2. @EnableConfigurationProperties(HttpProperties.class)
  3. public class HttpAutoConfiguration {
  4. @Resource
  5. private HttpProperties properties; // 使用配置
  6. // 在Spring上下文中创建一个对象
  7. @Bean
  8. @ConditionalOnMissingBean
  9. public HttpClient init() {
  10. HttpClient client = new HttpClient();
  11. String url = properties.getUrl();
  12. client.setUrl(url);
  13. return client;
  14. }
  15. }

在上面的 AutoConfiguration 中我们实现了自己要求:在 Spring 的上下文中创建了一个 HttpClient 类的 bean,并且我们把 properties 中的一个参数赋给了该 bean。

最后,我们在 resources 文件夹下新建目录 META-INF,在目录中新建 spring.factories 文件,并且在 spring.factories 中配置 AutoConfiguration:

  1. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  2. com.nosuchfield.httpstarter.HttpAutoConfiguration

最后使用 Maven 打包该项目。之后创建一个 SpringBoot 项目,在项目中添加我们之前打包的 starter 作为依赖,然后使用 SringBoot 来运行我们的 starter

测试如下:

  1. @Component
  2. public class RunIt {
  3. @Resource
  4. private HttpClient httpClient;
  5. public void hello() {
  6. System.out.println(httpClient.getHtml());
  7. }
  8. }

 

 之后可以在 application.properties中修改配置来进行测试证明 properties 中的数据确实被覆盖

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发