Spring Boot 自定义starter的示例代码

时间:2022-09-03 18:18:12

springboot 个人感觉特点:

1)众多库的集合(各种starter),方便快速构建应用系统。

2)自动配置spring(通过autoconfiguration机制),简化配置,也方便扩展新的starter。

3)内嵌web容器,无需war部署。

创建一个用maven构建的springboot项目

Spring Boot 自定义starter的示例代码

pom文件配置如下:

?
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?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.xjw.springboot</groupid>
  <artifactid>hellostarter</artifactid>
  <version>0.0.1-snapshot</version>
  <packaging>jar</packaging>
 
  <name>hello-spring-boot-starter</name>
  <description>测试自定义starter</description>
 
  <parent>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-parent</artifactid>
    <version>1.5.2.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>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter</artifactid>
    </dependency>
 
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-test</artifactid>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-configuration-processor</artifactid>
      <optional>true</optional>
    </dependency>
  </dependencies>
 
  <build>
    <plugins>
      <plugin>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-maven-plugin</artifactid>
      </plugin>
    </plugins>
  </build>
 
 
</project>

定义一个pojo用来接收properties中配置的信息

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.xjw;                                                    
                                                             
import org.springframework.boot.context.properties.configurationproperties;                       
                                                             
@configurationproperties(prefix = "hello")                                       
public class helloserviceproperteis {                                          
                                                             
  private string msg;                                                 
                                                             
  public string getmsg() {                                              
    return msg;                                                   
  }                                                          
                                                             
  public void setmsg(string msg) {                                          
    this.msg = msg;                                                 
  }                                                          
                                                             
}

@configurationproperties:用来标识这个pojo是一个用来接收指定前缀的资源配置值

prefix:表示在配置文件中配置项前缀[/code]

编写一个service用来对外提供服务

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.xjw;
 
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;
  }
 
}

配置一个pojo用来读取上面配置的helloserviceproperteis

?
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
package com.xjw;                                                             
                                                                      
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(value = helloserviceproperteis.class)                                   
@conditionalonclass(helloservice.class)                                                  
@conditionalonproperty(prefix = "hello", value = "enable", matchifmissing = true)                             
public class helloautoconfiguration {                                                   
                                                                      
  @autowired                                                              
  private helloserviceproperteis helloserviceproperteis;                                        
                                                                      
  @bean                                                                 
  @conditionalonmissingbean(helloservice.class)                                             
  public helloservice helloservice() {                                                 
    helloservice helloservice = new helloservice();                                          
    helloservice.setmsg(helloserviceproperteis.getmsg());                                       
    return helloservice;                                                       
  }                                                                   
}

@configuration:标识此类为一个spring配置类

@enableconfigurationproperties(value = helloserviceproperteis.class):启动配置文件,value用来指定我们要启用的配置类,可以有多个,多个时我们可以这么写value={xxproperties1.class,xxproperteis2.class....}

@conditionalonclass(helloservice.class):表示当classpath下存在helloservice.class文件时改配置文件类才有效

@conditionalonproperty(prefix = "hello", value = "enable", matchifmissing = true):表示只有我们的配置文件是否配置了以hello为前缀的资源项值,并且在该资源项值为enable,如果没有配置我们默认设置为enable[/code]

最后在src/main/resources 文件夹下新建文件夹 meta-inf,在新建的meta-inf文件夹下新建spring.factories

Spring Boot 自定义starter的示例代码

在新建的spring.factories文件中配置自动启动类为我们之前编写的helloautoconfiguration 类

org.springframework.boot.autoconfigure.enableautoconfiguration=com.xjw.helloautoconfiguration

Spring Boot 自定义starter的示例代码

然后就可以在其他的spring-boot项目中使用我们刚刚新建的starter了,我们来测试一下

在新建一个spring-boot项目,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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?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.xjw.springboot</groupid>
  <artifactid>hellostarter.test</artifactid>
  <version>0.0.1-snapshot</version>
  <packaging>jar</packaging>
 
  <name>hello-spring-boot-starter-test</name>
  <description>测试自定义starter</description>
 
  <parent>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-parent</artifactid>
    <version>1.5.2.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>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-web</artifactid>
    </dependency>
 
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-test</artifactid>
      <scope>test</scope>
    </dependency>
 
    <dependency>
      <groupid>com.xjw.springboot</groupid>
      <artifactid>hellostarter</artifactid>
      <version>0.0.1-snapshot</version>
    </dependency>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-devtools</artifactid>
    </dependency>
  </dependencies>
 
  <build>
    <plugins>
      <plugin>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-maven-plugin</artifactid>
      </plugin>
    </plugins>
  </build>
 
 
</project>

然后我们直接在咋们的启动类中中尝试使用以下我们上面定义的starter提供的helloservice:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.xjw;
 
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;
 
@restcontroller
@springbootapplication
public class hellospringbootstartertestapplication {
 
  @autowired
  private helloservice helloservice;
 
  @requestmapping("/")
  public string index() {
    return helloservice.sayhello();
  }
 
  public static void main(string[] args) {
    springapplication.run(hellospringbootstartertestapplication.class, args);
  }
}

接着我们修改测试项目中的application.properteis,加入如下配置:

?
1
2
3
4
5
debug=true
server.port=8888
 
#hello=enable
hello.msg=测试starter

最后启动项目,观察控制台输出的内容中依赖的starter,从positive matches下我们可以看到有这么一句:

helloautoconfiguration matched:
- @conditionalonclass found required class 'com.xjw.helloservice'; @conditionalonmissingclass did not find unwanted class (onclasscondition)
- @conditionalonproperty (hello.enable) matched (onpropertycondition)

或者我们打开项目依赖树也能找到我们的starter ,这说明spring已经自动的启动了我们的starter了,打开浏览器输入地址:http://localhost:8888/将会看到如下结果

Spring Boot 自定义starter的示例代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.cnblogs.com/cz-xjw/p/6632402.html