starter:

一、这个场景需要使用到的依赖是什么?

二、如何编写自动配置

启动器只用来做依赖导入;(启动器模块是一个空 JAR 文件,仅提供辅助性依赖管理,这些依赖可能用于自动装配或者其他类库)

专门来写一个自动配置模块;

启动器依赖自动配置;别人只需要引入启动器(starter)

我们一般的的经验

@Configuration    //指定这个类是一个配置类 @ConditionalOnXXX    //在指定条件成立的情况下自动配置类生效 @AutoConfigureAfter    //指定自动配置类的顺序

@Bean //给容器中添加组件

@ConfigurationPropertie(prefix=””)结合相关xxxProperties类来绑 定相关的配置

@EnableConfigurationProperties  //让xxxProperties生效加入到容器中

自动配置类要能加载

将需要启动就加载的自动配置类,配置在META‐INF/spring.factories下(即要把绿色要配置在里面)

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\

org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\

三、模式

1.启动器只是用来做依赖导入;启动器模块是一个空 JAR 文件,仅提供辅助性依赖管理,这些依赖可能用于自动 装配或者其他类库;比如

2. 启动器依赖自动配置;别人只需要引入启动器(starter)

所以我们要 专门来写一个自动配置模块autocongigurer;

3.推荐命名规则

四、步骤:

1)、启动器模块

<?xml  version="1.0"  encoding="UTF‐8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema‐instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven‐4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.atguigu.starter</groupId>
<artifactId>atguigu‐spring‐boot‐starter</artifactId>
<version>1.0‐SNAPSHOT</version> <!‐‐启动器‐‐>
<dependencies> <!‐‐引入自动配置模块‐‐>
<dependency>
<groupId>com.atguigu.starter</groupId>
<artifactId>atguigu‐spring‐boot‐starter‐autoconfigurer</artifactId>
<version>0.0.1‐SNAPSHOT</version>
</dependency>
</dependencies> </project>

2)、自动配置模块

 <?xml  version="1.0"  encoding="UTF‐8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema‐instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven‐ 4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> 5
<groupId>com.atguigu.starter</groupId>
<artifactId>atguigu‐spring‐boot‐starter‐autoconfigurer</artifactId>
<version>0.0.1‐SNAPSHOT</version>
<packaging>jar</packaging>
<name>atguigu‐spring‐boot‐starter‐autoconfigurer</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!‐‐ lookup parent from repository ‐‐>
</parent>
<properties>
<project.build.sourceEncoding>UTF‐8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF‐8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!‐‐引入spring‐boot‐starter;所有starter的基本配置‐‐>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter</artifactId>
</dependency>
</dependencies> </project>
package  com.atguigu.starter;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "atguigu.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;
}
}
package  com.atguigu.starter; public  class  HelloService  {
HelloProperties helloProperties; public HelloProperties getHelloProperties() { return helloProperties;
} public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
} public String sayHellAtguigu(String name){
return helloProperties.getPrefix()+"‐" +name + helloProperties.getSuffix();
} }
package  com.atguigu.starter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnWebApplication //web应用才生效
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration { @Autowired
HelloProperties helloProperties; @Bean
public HelloService helloService(){ HelloService service = new HelloService();
service.setHelloProperties(helloProperties); return service;
}
}

【串线篇】spring boot自定义starter的更多相关文章

  1. Spring Boot自定义starter必知必会条件

    前言 在目前的Spring Boot框架中,不管是Spring Boot官方还是非官方,都提供了非常多的starter系列组件,助力开发者在企业应用中的开发,提升研发人员的工作效率,Spring Bo ...

  2. spring boot自定义starter

    1.spring boot 项目中自定义jar包 2.项目目录 3.src/main/java 下面写自己的方法,重点是 resources 下面的文件,在resources下面新建文件夹名字为 ME ...

  3. Spring Boot 自定义 starter

    一.简介 SpringBoot 最强大的功能就是把我们常用的场景抽取成了一个个starter(场景启动器),我们通过引入springboot 为我提供的这些场景启动器,我们再进行少量的配置就能使用相应 ...

  4. Spring boot 自定义starter

    以下配置来自尚硅谷.. 常用如何配置 @Configuration //指定这个类是一个配置类 @ConditionalOnXXX //在指定条件成立的情况下自动配置类生效 @AutoConfigur ...

  5. Spring Boot 自定义Starter 可能引发的问题(Error)

    如果你的项目出现: Consider defining a bean of type 'com.wy.helloworld_spring_boot_starter.PersonService' in ...

  6. Spring Boot 自定义kafka 消费者配置 ContainerFactory最佳实践

    Spring Boot 自定义kafka 消费者配置 ContainerFactory最佳实践 本篇博文主要提供一个在 SpringBoot 中自定义 kafka配置的实践,想象这样一个场景:你的系统 ...

  7. spring boot自定义线程池以及异步处理

    spring boot自定义线程池以及异步处理@Async:什么是线程池?线程池是一种多线程处理形式,处理过程中将任务添加到队列,然后在创建线程后自动启动这些任务.线程池线程都是后台线程.每个线程都使 ...

  8. Spring Boot自定义配置与加载

    Spring Boot自定义配置与加载 application.properties主要用来配置数据库连接.日志相关配置等.除了这些配置内容之外,还可以自定义一些配置项,如: my.config.ms ...

  9. Spring Boot 2.X(四):Spring Boot 自定义 Web MVC 配置

    0.准备 Spring Boot 不仅提供了相当简单使用的自动配置功能,而且开放了非常自由灵活的配置类.Spring MVC 为我们提供了 WebMvcConfigurationSupport 类和一 ...

随机推荐

  1. C语言指令数组名和数组名取地址

    以下C语言指令:int a[5] = {1, 3, 5, 7, 9}; int *p = (int *)(&a + 1); printf("%d, %d", *(a + 1 ...

  2. [VBA]删除多余工作表

    sub 删除多余工作表() Dim i As Integer Application.DisplayAlerts = False For i = Worksheets.Count To 1 step ...

  3. 测开之路一百二十八:flask之重定向和404

    a.b两个视图,分别返回a的页面和b的页面 重定向:redirect 重定向到路由:请求/a/时,重定向到/b/ 重定向到视图函数:url_for(“函数名“),访问/a/时,重定向到函数b() 主动 ...

  4. Java使用JDBC连接Hive

    最近一段时间,处理过一个问题,那就是hive jdbc的连接问题,其实也不是大问题,就是url写的不对,导致无法连接.问题在于HiveServer2增加了别的安全验证,导致正常的情况下,传递的参数无法 ...

  5. 【ABAP系列】SAP ABAP MIR7预制凭证BAPI

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP MIR7预制凭 ...

  6. python+selenium切换窗口(获取句柄信息)

    current_window_handle:获得当前窗口句柄: window_handles:返回所有窗口的句柄到当前会话: switch_to.window(suoyou[0]) ========= ...

  7. ssh远程登录过程中卡住

    1.首先排查网络连通性,查看网络是否通畅,远程端口是否开放 2.查看服务器复制,cpu,内存负载是否过大 3.检查ssh配置,查看以下配置是否这样配置 UseDNS no GSSAPIAuthenti ...

  8. SpringMVC访问映射的jsp文件时,报404错误

    配置文件中需要配置映射自然不必多说 <bean class="org.springframework.web.servlet.view.InternalResourceViewReso ...

  9. [DS+Algo] 005 三种简单排序及其代码实现

    目录 1. 冒泡排序 BubbleSort 1.1 算法描述 1.2 性能分析 1.3 Python 代码实现 2. 选择排序 SelectionSort 2.1 算法描述 2.2 选择排序的主要优点 ...

  10. [转帖]Twitter 宣布抛弃 Mesos,全面转向 Kubernetes

    Twitter 宣布抛弃 Mesos,全面转向 Kubernetes http://www.itpub.net/2019/05/06/1788/ 事实标准了.   作者 | 阿里云智能高级技术专家 张 ...