1.创建空项目 hellospringbootstarter
2.创建 2个module
------》1.创建maven项目 hello-spring-boot-starter 作为启动starter
------》2.创建springboot项目 hello-spring-boot-stater-autoconfigurtion,自动配置项目
在hello-spring-boot-starter项目中,删除多余的东西,只需要在pom.xml中,引入自动配置项目hello-spring-boot-starter-autoconfigurtion即可;
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>
<groupId>fun.huanghai.boot</groupId>
<artifactId>hello-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>fun.huanghai.boot</groupId>
<artifactId>hello-spring-boot-starter-autoconfigurtion</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
在自动配置项目中hello-spring-boot-starter-autoconfigurtion操作:
删除多余的文件或文件夹中(不需要用到),启动类等
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 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.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>fun.huanghai.boot</groupId>
<artifactId>hello-spring-boot-starter-autoconfigurtion</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hello-spring-boot-starter-autoconfigurtion</name>
<description>hello-spring-boot-starter-autoconfigurtion</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
1.创建 HelloProperties 作为属性配置
在类上添加注解
@ConfigurationProperties("fun.hello") 表明这是属性配置
package fun.huanghai.boot.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties("fun.hello")
public class HelloProperties {
private String prefix;
private String suffix;
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
2.创建 HelloService
需要使用到HelloProperties的属性
package fun.huanghai.boot.service;
import fun.huanghai.boot.bean.HelloProperties;
import org.springframework.beans.factory.annotation.Autowired;
public class HelloService {
@Autowired
private HelloProperties helloProperties;
public String sayHello(String name){
return helloProperties.getPrefix() + "》》" + name + "===" + helloProperties.getSuffix();
}
}
3.创建 HelloAutoConfigurtion自动配置
在类上标注:
@Configuration 表明这是一个配置类
@EnableConfigurationProperties(HelloProperties.class) 表明需要的属性从HelloProperties中获取
在方法上标注:
@ConditionalOnMissingBean(HelloService.class)
表明ioc 器中不存在HelloService bean的情况下,才创建
package fun.huanghai.boot.auto;
import fun.huanghai.boot.bean.HelloProperties;
import fun.huanghai.boot.service.HelloService;
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;
@Configuration
@EnableConfigurationProperties(HelloProperties.class)
public class HelloAutoConfigurtaion {
@ConditionalOnMissingBean(HelloService.class)
@Bean
public HelloService helloService(){
return new HelloService();
}
}
在类路径下创建META-INF文件夹,在创建文件spring.factories 文件内容为:
## Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ fun.huanghai.boot.auto.xxxAutoConfigurtaion #这个为自动配置类全路径
表明,springboot启动时候,要将我这个自动配置类加入到容器中
## Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
fun.huanghai.boot.auto.HelloAutoConfigurtaion
创建完成后:
将两个项目安装到maven创库中
到此,自定义starter完成,接下来在应用
在另外一个springboot项目中引入
pom.xml
<!-- 引入自定义hello-starter 20220417 -->
<dependency>
<groupId>fun.huanghai.boot</groupId>
<artifactId>hello-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
创建一个HelloController,注入HelloService
package fun.huanghai.guifan.controller;
import fun.huanghai.boot.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping("/hello")
public String hello(String name){
return helloService.sayHello(name);
}
}
在application.properties或者application.yml中,配置
fun.hello.prefix=fun
fun.hello.suffix=end
启动,访问,查看运行结果
访问结果,生效,完成;