starter介绍
spring boot 在配置上相比spring要简单许多, 其核心在于spring-boot-starter, 在使用spring boot来搭建一个项目时, 只需要引入官方提供的starter, 就可以直接使用, 免去了各种配置。starter简单来讲就是引入了一些相关依赖和一些初始化的配置。
Spring官方提供了很多starter,第三方也可以定义starter。为了加以区分,starter从名称上进行了如下规范:
- Spring官方提供的starter名称为:spring-boot-starter-xxx
例如Spring官方提供的spring-boot-starter-web - 第三方提供的starter名称为:xxx-spring-boot-starter
例如由mybatis提供的mybatis-spring-boot-starter
自定义starter
- 第一步:创建starter工程case-spring-boot-starter并配置pom.xml文件
<?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>
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.2.1.RELEASE</version>
</parent>
<groupId>org.example</groupId>
<artifactId>hello-world-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
</project>
- 第二步:创建配置属性类HelloWorldProperties
package config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Data
/**
* 读取配置文件属性赋值给类
*/
@ConfigurationProperties(prefix = "hello-world")
public class HelloWorldProperties {
private String name;
private int age;
private char sex;
}
- 第三步:创建服务类HelloWorldService
package service;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class HelloWorldService {
private String name;
private int age;
private char sex;
public String sayHello() {
return "你好!我的名字叫 " + name + ", 已经" + age + "岁了,我是一个" + sex + "孩子";
}
}
- 第四步:创建自动配置类HelloWorldServiceAutoConfiguration
package config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import service.HelloWorldService;
@Configuration
@EnableConfigurationProperties(HelloWorldProperties.class)
public class HelloWorldServiceAutoConfiguration {
private HelloWorldProperties helloWorldProperties;
public HelloWorldServiceAutoConfiguration(HelloWorldProperties helloWorldProperties) {
this.helloWorldProperties = helloWorldProperties;
}
@Bean
@ConditionalOnMissingBean
public HelloWorldService helloWorldService() {
return new HelloWorldService(helloWorldProperties.getName(), helloWorldProperties.getAge(), helloWorldProperties.getSex());
}
}
- 第五步:在resources目录下创建META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
config.HelloWorldServiceAutoConfiguration
- 第六步:将starter安装到本地maven仓库供其他应用来使用。
使用使用starter
- 第一步:创建maven工程hello-test并配置pom.xml文件
<?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>
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.2.1.RELEASE</version>
</parent>
<groupId>org.example</groupId>
<artifactId>hello-test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--自定义的starter-->
<dependency>
<groupId>org.example</groupId>
<artifactId>hello-world-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
- 第二步:创建application.yml文件
server:
port: 8080
hello-world:
name: 小明
sex: 男
age: 16
- 第三步:创建HelloController
package application.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import service.HelloWorldService;
@RestController
public class HelloController {
@Autowired
private HelloWorldService helloWorldService;
@GetMapping("/hello")
public String sayHello() {
return helloWorldService.sayHello();
}
}
- 第四步:创建启动类HelloApplication
package application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class, args);
}
}
- 执行启动类main方法,访问地址http://localhost:8080/hello
一定要注意启动类的位置放在最外侧,即包含所有子包