自己写spring boot starter

学习了:《spring boot实战》汪云飞著 6.5.4节

pom.xml

<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.stono</groupId>
<artifactId>sboot161</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging> <name>sboot161</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>1.5.9.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

HelloService.java

package com.stono;

public class HelloService {
private String msg; public String sayHello(){
return "Hello"+msg;
} public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
}
}
HelloServiceProperties.java
package com.stono;

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

@ConfigurationProperties(prefix = "hello")
public class HelloServiceProperties {
private static final String MSG = "world";
private String msg = MSG; public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
}
}
HelloServiceAutoConfiguration.java
package com.stono;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
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
@EnableConfigurationProperties(HelloServiceProperties.class)
@ConditionalOnClass(HelloService.class)
@ConditionalOnProperty(prefix = "hello", value = "enabled", matchIfMissing = true)
public class HelloServiceAutoConfiguration {
@Autowired
private HelloServiceProperties helloServiceProperties; @Bean
@ConditionalOnMissingBean(HelloService.class)
public HelloService helloService(){
HelloService helloService = new HelloService();
helloService.setMsg(helloServiceProperties.getMsg());
return helloService;
}
}

编译完毕之后,使用IntelliJ 自带的Edit Configurations... 进行maven 编译设置,参数为 clean package

运行配置好的maven命令即可编译jar包;

如果在terminal中直接运行mvn clean package

会出现错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project sboot161:
Compilation failure: Compilation failure:
[ERROR] 不再支持源选项 1.5。请使用 1.6 或更高版本。
[ERROR] 不再支持目标选项 1.5。请使用 1.6 或更高版本。
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

学习了:http://blog.csdn.net/sosous/article/details/78312867

进行pom.xml文件修改:

  <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

就可以正常运行 mvn clean package了;

运行之后,需要使用mvn install命令将jar包安装本地库:

mvn install:install-file -Dfile=D:\Java\IdeaProjects\sboot161\target\sboot161-1.0-SNAPSHOT.jar -DgroupId=com.stono -DartifactId=sboot161 -Dversion=1.0-SNAPSHOT -Dpackaging=jar

然后在其他项目中就可以添加该pom依赖,在程序中自动引入该HelloService了;

自己写spring boot starter的更多相关文章

  1. 手把手教你手写一个最简单的 Spring Boot Starter

    欢迎关注微信公众号:「Java之言」技术文章持续更新,请持续关注...... 第一时间学习最新技术文章 领取最新技术学习资料视频 最新互联网资讯和面试经验 何为 Starter ? 想必大家都使用过 ...

  2. 手把手教你定制标准Spring Boot starter,真的很清晰

    写在前面 我们每次构建一个 Spring 应用程序时,我们都不希望从头开始实现具有「横切关注点」的内容:相反,我们希望一次性实现这些功能,并根据需要将它们包含到任何我们要构建的应用程序中 横切关注点 ...

  3. 年轻人的第一个自定义 Spring Boot Starter!

    陆陆续续,零零散散,栈长已经写了几十篇 Spring Boot 系列文章了,其中有介绍到 Spring Boot Starters 启动器,使用的.介绍的都是第三方的 Starters ,那如何开发一 ...

  4. 从零开始开发一个Spring Boot Starter

    一.Spring Boot Starter简介 Starter是Spring Boot中的一个非常重要的概念,Starter相当于模块,它能将模块所需的依赖整合起来并对模块内的Bean根据环境( 条件 ...

  5. 最详细的自定义Spring Boot Starter开发教程

    1. 前言 随着Spring的日渐臃肿,为了简化配置.开箱即用.快速集成,Spring Boot 横空出世. 目前已经成为 Java 目前最火热的框架了.平常我们用Spring Boot开发web应用 ...

  6. 自定义spring boot starter 初尝试

    自定义简单spring boot starter 步骤 从几篇博客中了解了如何自定义starter,大概分为以下几个步骤: 1 引入相关依赖: 2 生成属性配置类: 3 生成核心服务类: 4 生成自动 ...

  7. Spring Boot Starter 介绍

    http://www.baeldung.com/spring-boot-starters 作者:baeldung 译者:http://oopsguy.com 1.概述 依赖管理是任何复杂项目的关键部分 ...

  8. spring -boot s-tarter 详解

    Starter POMs是可以包含到应用中的一个方便的依赖关系描述符集合.你可以获取所有Spring及相关技术的一站式服务,而不需要翻阅示例代码,拷贝粘贴大量的依赖描述符.例如,如果你想使用Sprin ...

  9. Spring Boot (一): Spring Boot starter自定义

    前些日子在公司接触了spring boot和spring cloud,有感于其大大简化了spring的配置过程,十分方便使用者快速构建项目,而且拥有丰富的starter供开发者使用.但是由于其自动化配 ...

随机推荐

  1. find_in_set的用法(某个字段包含某个字符)

    有个文章表里面有个type字段,他存储的是文章类型,有 1头条,2推荐,3热点,4图文 .....11,12,13等等 现在有篇文章他既是 头条,又是热点,还是图文, type中以 1,3,4的格式存 ...

  2. 【知识总结】多项式全家桶(二)(ln和exp)

    上一篇:[知识总结]多项式全家桶(一)(NTT.加减乘除和求逆) 一.对数函数\(\ln(A)\) 求一个多项式\(B(x)\),满足\(B(x)=\ln(A(x))\). 这里需要一些最基本的微积分 ...

  3. MVC系列学习(十七)-过滤器

    本次学习的文件结构如下 1.过滤器的几种表示方式 1.1将过滤器 加到方法上,作用范围为该方法 1.2将过滤器加到当前类上,作用范围为该类的所有方法 1.3添加全局过滤器,作用范围为所有方法 2.Ac ...

  4. Unity学习-工具准备(一)

    工具介绍 Unity 4.5.4 VS2013 Visual Studio 2013 Tools for Unity unity3d圣典 五大面板 Hierarchy:场景资源面板 [管理 当前场景 ...

  5. 给定的逗号分隔的数字字符串转换为Table

    --将给定的逗号分隔的数字字符串转换为Table CREATE FUNCTION [dbo].[fu_Split](@strString nvarchar(4000)) RETURNS @Result ...

  6. echarts交叉关系图一

    想要做一个公司-人员关系图,官网echarts图graph webkit dep 稍微改了一下, 也是有点恶心自己,调了一个数据最多的去改,如果正好有人需要就不用去改了 说明:此图没有坐标,可以设置图 ...

  7. JS——dom

    节点的获取 <script> var div = document.getElementById("box");//返回指定标签 var div = document. ...

  8. Codeforces_732D_(二分贪心)

    D. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  9. 用svg做流程管理

    说起流程管理这个功能,如果没有个动态图配合显示,简直就是太没有客户体验感了.就比如说请假流程吧,流程走到哪一步了,流程走向过程中都那些人审批的,审批的结果等等,如果就来个列表,也不是说不行,就是觉得太 ...

  10. zepto处理touch事件

    处理Touch事件能让你了解到用户的每一根手指的位置,在touch事件触发的时候产生,可以通过touch event handler的event对象取到,如果基于zepto.js开发,一般是通过eve ...