SpringBoot實戰筆記:15_SpringBoot 自定義starter pom
阿新 • • 發佈:2019-01-06
15_SpringBoot 自定義starter pom
我們在 pom.xml
中的那些springboot依賴,就是starter
目的:當某個類存在的時候,自動配置這個類的Bean,並可將Bean的書寫在 application.properties
中配置
- 其實就相當於在學習java的時候,將一個專案打成jar包,然後在另一個專案中使用它。
1,建立一個Maven專案
2,修改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.zyf</groupId>
<artifactId>spring-boot-starter-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-starter-demo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--新增自動配置的依賴-->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-autoconfigure -->
<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>
3,新增屬性配置(型別安全的屬性配置實際上就是一個類)
package com.zyf.spring_boot_stater_demo;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Created by zyf on 2018/3/7.
*/
//在配置檔案中通過:demo.msg=xxx來配置msg
//若不設定,則預設為:我是DEMO
@ConfigurationProperties(prefix = "demo")
public class DemoProperties {
public static final String MSG = "我是DEMO";
private String msg = MSG;
public static String getMSG() {
return MSG;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
4,建立一個類DemoService,作為判斷依據類
根據這個類是否存在,來決定是否建立這個類的Bean
package com.zyf.spring_boot_stater_demo;
import org.springframework.stereotype.Service;
/**
* Created by zyf on 2018/3/7.
*/
//當進行自動配置時,判斷DemoService是否存在,若存在,則建立這個類的Bean
public class DemoService {
private String msg;
public String say(){
return "hello:" + msg;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
5,若想我們自己定義的自動配置可以生效,需要將該類註冊到 spring.factories
檔案中
註冊檔案中的內容:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.zyf.DemoServiceAutoConfiguration
6,將該starter,安裝到本地maven庫
7,安裝成功
8,建立一個SpringBoot專案,將自定義的starter匯入pom
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--自定義的maven依賴-->
<dependency>
<groupId>com.zyf</groupId>
<artifactId>spring-boot-starter-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
9,在右側Maven Projects檢視
10,一個非常簡單的執行類
package com.zyf;
import com.zyf.spring_boot_stater_demo.DemoService;
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;
@SpringBootApplication
@RestController
public class Springboot05Application {
@Autowired
DemoService demoService;
@RequestMapping("/")
public String index(){
return demoService.say();
}
public static void main(String[] args) {
SpringApplication.run(Springboot05Application.class, args);
}
}
11,演示
可以看到是預設的
12,在配置檔案中設定debug=true可以檢視到自動配置相關資訊
可以看到自定義的 DemoServiceAutoConfiguration
已經自動配置了
13,在配置檔案中新增demo.msg配置
debug=true
demo.msg="everybody is good , that's real good!"