命名规范
官方命名规范
- 模式:spring-boot-starter-模块名
- 举例:spring-boot-starter-web、spring-boot-starter-jdbc
自定义命名规范
- 模式:模块-spring-boot-starter
- 举例:mybatis-spring-boot-starter
实例
项目
父项目:hello-spring-boot-starter-parent
<?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>cn.hehe.springboot</groupId>
<artifactId>hello-spring-boot-starter-parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>hello-spring-boot-starter</module>
<module>hello-spring-boot-autoconfigurer</module>
</modules>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.4.5</version>
</dependency>
</dependencies>
</project>
子项目:hello-spring-boot-starter
<?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">
<parent>
<artifactId>hello-spring-boot-starter-parent</artifactId>
<groupId>cn.hehe.springboot</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>hello-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>cn.hehe.springboot</groupId>
<artifactId>hello-spring-boot-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!--如果当前starter 还需要其他的类库就在这里引用-->
</dependencies>
</project>
子项目:hello-spring-boot-autoconfigurer
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.hehe.springboot</groupId>
<artifactId>hello-spring-boot-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hello-spring-boot-autoconfigurer</name>
<description>Demo project for Spring Boot</description>
<properties>
<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-configuration-processor</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--安装到仓库后,引用时不出现 BOOT-INF文件夹(会导致找不到相关类)-->
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
属性类
package cn.hehe.springboot.hellospringbootautoconfigurer.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @program: hello-springbootstarterparent
* @description: 属性类
* @author: hehe
* @create: 2021-04-16 16:03
**/
@ConfigurationProperties("hello")
public class HelloProperties {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
控制类
package cn.hehe.springboot.hellospringbootautoconfigurer.controller;
import cn.hehe.springboot.hellospringbootautoconfigurer.config.HelloProperties;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
* @program: hello-springbootstarterparent
* @description: hello控制类
* @author: hehe
* @create: 2021-04-16 16:05
**/
@RestController
public class HelloController {
private HelloProperties helloProperties;
public HelloController(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
@GetMapping("hello")
public String hello(){
return helloProperties.getName()+"欢迎您!";
}
}
自动配置类
package cn.hehe.springboot.hellospringbootautoconfigurer.config;
import cn.hehe.springboot.hellospringbootautoconfigurer.controller.HelloController;
import org.springframework.beans.factory.annotation.Autowired;
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;
/**
* @program: hello-springbootstarterparent
* @description: Hello自动配置类
* @author: hehe
* @create: 2021-04-16 16:08
**/
@Configuration
@ConditionalOnProperty(value="hello.name")
@EnableConfigurationProperties({HelloProperties.class})
public class HelloAutoConfitguration {
@Autowired
HelloProperties helloProperties;
@Bean
public HelloController helloController(HelloProperties helloProperties){
return new HelloController(helloProperties);
}
}
spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.hehe.springboot.hellospringbootautoconfigurer.config.HelloAutoConfitguration
安装starter
引用测试
①引用项目
<dependency>
<groupId>cn.hehe.springboot</groupId>
<artifactId>hello-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
②配置属性
③访问地址:http://localhost:8080/hello