springboot 的start

建一个父工程

不需要其他目录结构,需要注意的是把type的类型改为POM 这样就没有工程的目录结构 因为父工程不需要

给父工程的pom添加依赖

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

这样父工程就完成好了

我们仿造mybatis的起步来写

现在需要对外暴露一个工程依赖 myspringboot-start 以刚创建的工程为父工程
子工程为对外暴露的工程 依赖我们的配置类
<dependencies>
<dependency>
<groupId>com.parentdemo</groupId>
<artifactId>mystart-springboot-configruation</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
注意:这个启动工程 如果需要其他的依赖包 统一的放到这个工程中进行管理
这个依赖的包就是我们接下来需要自定义的配置依赖工程 一个普通的springboot工程

工程如图类似就可以了 不需要application.yml文件

定义一个类接收外部的参数:

package com.nicong.demo;

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

@ConfigurationProperties(prefix = "spring.demo")
public class User {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
@ConfigurationProperties(prefix = "spring.demo") 这个注解简单地说一下
就是从上下文中自动的获取到对应字段的属性映射到当前类中去
并且以属性prefix = "spring.demo" 为开头的属性
接下来 创建一个配置类:
package com.nicong.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnProperty(name = "spring.demo.name")
@EnableConfigurationProperties(User.class)
public class HelloConfg {
@Autowired
private User user;
@Bean
public helloController getUser(){

return new helloController(user);
}
}
@Configuration标注为一个配置类会被扫描加载就不多说了
@ConditionalOnProperty(name = "spring.demo.name") 这个就相当于 一个条件判断 容器中存在这样的key就加载 否则就不加载这个配置类,
以@Conditionalxxx的条件判断还有很多这里就不多说了
@EnableConfigurationProperties(User.class)这个可能大家比较陌生的 他的作用说的简单一点就是把带有@ConfigurationProperties注解的类放到springboot容器中
User.class 这个是需要放入的类型 可以写多个 

在来个对外访问的类
package com.nicong.demo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class helloController {

private User user;

public helloController(User user) {
this.user = user;
}

@GetMapping("/")
public String getHello(){

return user.getName()+"欢迎你!";
}
}
User参数会自动的通过有参数的构造方法注入 因为User已经被放入到容器中了 这里就不多说了
这个类就是一个简单的controller类

接下来最重要的一步

在resources下 建包 META-INF 文件 spring.factories

说白了就是springboot启动的时候需要自动加载的类

org.springframework.boot.autoconfigure.EnableAutoConfiguration 为KEY
value就是
com.nicong.demo.HelloConfg 访问的相对路劲的类

接下来就是把工程打包放入到maven中 就不多说了
再然后在其他工程中引用start工程的坐标就可以用了

希望对你们有帮助 有问题可以交流 qq359073594



超简洁,玩转springboot 之springboot自定义start工程的更多相关文章

  1. springboot 2.0 自定义redis自动装配

    首先创建maven项目 pom.xml: <?xml version="1.0" encoding="UTF-8"?> <project xm ...

  2. SpringBoot系列之自定义starter实践教程

    SpringBoot系列之自定义starter实践教程 Springboot是有提供了很多starter的,starter翻译过来可以理解为场景启动器,所谓场景启动器配置了自动配置等等对应业务模块的一 ...

  3. 【SpringBoot】SpringBoot 国际化(七)

    本周介绍SpringBoot项目的国际化是如何处理的,阅读本章前请阅读[SpringBoot]SpringBoot与Thymeleaf模版(六)的相关内容 国际化原理 1.在Spring中有国际化Lo ...

  4. 【SpringBoot】SpringBoot与SpringMVC自动配置(五)

    本文介绍SpringBoot对Spring MVC自动配置,SpringBoot自动配置原理可以参考:[SpringBoot]SpringBoot配置与单元测试(二) 首先新建一个SpringBoot ...

  5. SpringBoot(四) -- SpringBoot与Web开发

    一.发开前准备 1.创建一个SpringBoot应用,引入我们需要的模块 2.SpringBoot已经默认将这些场景配置好了,只需要在配置文件中指定少量配置,就能运行起来 3.编写业务代码 二.静态资 ...

  6. 【SpringBoot】SpringBoot 入门示例

    参考资料: http://www.tuicool.com/articles/mqeee2A http://www.cnblogs.com/suncj/p/4065589.html http://spr ...

  7. SpringBoot简单打包部署(附工程)

    前言 本文主要介绍SpringBoot的一些打包事项和项目部署以及在其中遇到一些问题的解决方案. SpringBoot打包 在SpringBoot打包这块,我们就用之前的一个web项目来进行打包. 首 ...

  8. springBoot系列-->springBoot注解大全

    一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration ...

  9. [SpringBoot] - 了解什么是SpringBoot,使用SpringBoot的配置文件

    首先明白Spring是什么,Spring是Java开发的一个框架,为了方便简化Java开发. 什么是注解(注解式开发)? Spring的常用注解有哪些? 假如用SpringBoot构建一个网站程序,应 ...

  10. Springboot】Springboot整合邮件服务(HTML/附件/模板-QQ、网易)

    介绍 邮件服务是常用的服务之一,作用很多,对外可以给用户发送活动.营销广告等:对内可以发送系统监控报告与告警. 本文将介绍Springboot如何整合邮件服务,并给出不同邮件服务商的整合配置. 如图所 ...

随机推荐

  1. 对于Oracle、mysql和sql server中的部分不同理解

    1.在mysql中事务默认是自动提交的,只有设置autocommit为0的时候,才用自己commit:(提到commit不要忘了rollback哦,回滚)2.但是在oracle中必须自己commit: ...

  2. sparksql解析流程

    1.sparkSql处理核心:Catalyst工作流程(本质:把sql.dataframe相结合,以树tree的形式来存储.优化) 2.catalyst工作流程 1)Parser(解析器):SqlPa ...

  3. Qt 自定义事件

    Qt 自定义事件很简单,同其它类库的使用很相似,都是要继承一个类进行扩展.在 Qt 中,你需要继承的类是 QEvent. 继承QEvent类,你需要提供一个QEvent::Type类型的参数,作为自定 ...

  4. Git入门配置

    1.账户注册: 无论是GitHub还是码云(下称Gitee),要使用他们,我们都需要先注册账户,已有账户的可以跳过此步骤. Gitee GitHub 2.创建仓库: a.创建远程仓库 登入Gitee后 ...

  5. python 文件批量改名重命名 rename

    path = '/Volumes/Seagate/dev/imgs/' os.chdir(path) print('cwd: ', os.getcwd()) for f in os.listdir(' ...

  6. 20210716 noip17

    考场 终于有一场在晚上考了 T1 随便画了画就发现要求每个点的后继个数,想起来有 dfs 和 toposort 两种方法,感觉很稳 T2 裸的网络流有 70pts?!真香 一看 T3 就想起了 Mst ...

  7. NRF52832空中升级DFU

    Secure DFU环境搭建 升级原理,加密原理在此不做描述,详情参考http://www.cnblogs.com/iini/p/9314246.html 1.工具一览 gcc-arm-none-ea ...

  8. SQL Server数据表设计编辑后无法保存处理办法

    关于使用 SQL Server 企业管理器,表[设计]界面,修改数据表字段或类型无法保存的问题处理过程: 使用SQL Server数据库的你是否遇到过每次数据库编辑工具内点击设计修改表字段或类型要保存 ...

  9. Robot Framework(8)- Collections 测试库常用的关键字列表

    如果你还想从头学起Robot Framework,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1770899.html 前言 所有关键字 ...

  10. 性能测试必备命令(3)- lscpu

    性能测试必备的 Linux 命令系列,可以看下面链接的文章哦 https://www.cnblogs.com/poloyy/category/1819490.html 介绍 显示有关CPU架构的信息 ...