spring boot 使用 starter 解决了很多配置问题, 但是, 他是怎么来解决这些问题的呢?
这里通过一个简单的例子, 来看一下, starter是怎么来设置默认配置的.
一. 建 starter 项目
自定义的starter, 项目命名规范是: 自定义名-spring-boot-starter
先来看一下, 我最后的目录结构
1. 修改pom.xml文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
<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>org.elvin</groupid> <artifactid>my-spring-boot-starter</artifactid> <version> 1.0 -snapshot</version> <packaging>jar</packaging> <name>my-spring-boot-starter</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> <build> <plugins> <plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-compiler-plugin</artifactid> <version> 2.3 . 2 </version> <configuration> <source> 1.8 </source> <target> 1.8 </target> </configuration> </plugin> </plugins> </build> </project> |
其实只是加入了 spring-boot-autoconfigure
app文件中的main方法, 我注释掉了, 这个在这里没有用到
2. 配置属性对应的接收文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package org.elvin; import org.springframework.boot.context.properties.configurationproperties; /** * author: elvin * date: 2017/12/12 14:51 * description: */ @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; } } |
3. 对外service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package org.elvin; /** * author: elvin * date: 2017/12/12 14:55 * description: */ 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; } } |
4. 对外service与配置对应文件关联
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
package org.elvin; 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; /** * author: elvin * date: 2017/12/12 14:59 * description: */ @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; } } |
5. starter配置 : spring.factories
1
|
org.springframework.boot.autoconfigure.enableautoconfiguration=org.elvin.helloserviceautoconfiguration |
做完这些之后, 通过 mvn clean install , 打包到maven库里面
二. spring boot 项目使用
新建一个spring boot 项目, 选择web即可.
目录结构:
先看一下引用pom.xml
1
2
3
4
5
|
<dependency> <groupid>org.elvin</groupid> <artifactid>my-spring-boot-starter</artifactid> <version> 1.0 -snapshot</version> </dependency> |
再看一下hellocontroller
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package org.elvin.learn.springboot.controller; import org.springframework.beans.factory.annotation.autowired; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller; import org.elvin.*; /** * author: elvin * date: 2017/12/12 15:34 * description: */ @restcontroller @requestmapping ( "hello" ) public class hellocontroller { @autowired private helloservice helloservice; @requestmapping ( "index" ) public string index(){ return helloservice.sayhello(); } } |
这里的 helloservice 就是 前面自定义 starter 里面的.
1. 结果: 未配置情况下, 应该是输出 hello world
2. 在配置文件中, 加入 hello.msg=hahahahahah
这个例子很简单, 只是显示一下主要的过程, 别的都是各插件自己的逻辑判断了.
参考资料:
javaee开发的颠覆者 spring boot实战
以上这篇spring boot 自定义starter的实现教程就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/elvinle/archive/2017/12/12/8028686.html