1.创建一个父项目: demo
1.1 项目结构:
1.2 pom文件内容:
<?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.7.6</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<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>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.visy</groupId>
<artifactId>auto-config-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>
<modules>
<module>auto-config-starter</module>
<module>test-project</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.创建一个自定义的starter项目:auto-config-starter
2.1 pom文件内容:
<?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>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.visy</groupId>
<artifactId>auto-config-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.2 配置文件读取类:DemoProperties
package com.visy.autoconfigstarter.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @author visy.wang
* @description: 配置文件读取
* @date 2022/12/8 14:33
*/
@Data
@ConfigurationProperties(prefix = "demo")
public class DemoProperties {
private Boolean enable;
private String username;
private Integer password;
}
2.3 提供给第三方的服务类:DemoService
package com.visy.autoconfigstarter.service;
import lombok.AllArgsConstructor;
import lombok.Data;
/**
* @author visy.wang
* @description: 提供给第三方的服务
* @date 2022/12/8 14:42
*/
@Data
@AllArgsConstructor
public class DemoService {
private String name;
private Integer pass;
public void say(){
System.out.println(name + " says : my password is " + pass);
}
}
2.4 自动装配配置类:MyAutoConfiguration
package com.visy.autoconfigstarter.config;
import com.visy.autoconfigstarter.properties.DemoProperties;
import com.visy.autoconfigstarter.service.DemoService;
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;
/**
* @author visy.wang
* @description: 自动装配配置类
* @date 2022/12/8 14:39
*/
@Configuration
//负责将指定的Bean注册到Spring容器中
@EnableConfigurationProperties(value = DemoProperties.class)
//当给定的类名在类路径上存在时,则实例化当前Bean
@ConditionalOnClass(value = DemoService.class)
//当配置文件中 demo.enable = true 配置时进行自动配置,如果没有设置此值就默认使用 matchIfMissing 指定的值
@ConditionalOnProperty(prefix = "demo", name = "enable")
//等价于EnableConfigurationProperties,可引入多个,有三种引入方式,一般用于引入内部使用的依赖
//@Import({DemoProperties.class})
public class MyAutoConfiguration {
/**
* 使用配置文件
*/
@Autowired
private DemoProperties properties;
/**
* 注入demoService,供第三方使用
*/
@Bean
@ConditionalOnMissingBean(value = DemoService.class) //当给定的Bean不存在时,则实例化当前Bean
public DemoService demoService(){
return new DemoService(properties.getUsername(), properties.getPassword());
}
}
2.5 配置:spring.factories (在resources下建立META-INF目录,然后创建spring.factories文件)
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.visy.autoconfigstarter.config.MyAutoConfiguration
2.6 项目执行: maven 的install命令安装
3.测试项目:test-project
3.1 pom文件:
<?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>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.visy</groupId>
<artifactId>test-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test-project</name>
<description>test-project</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.visy</groupId>
<artifactId>auto-config-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
3.2 配置文件:application.yml配置
demo:
enable: true
username: zhangSan
password: 123456
3.3 测试接口:TestController
package com.visy.testproject.controller;
import com.visy.autoconfigstarter.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author visy.wang
* @description:
* @date 2022/12/8 14:54
*/
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
private DemoService demoService;
@RequestMapping("/t")
public String test(){
demoService.say();
return demoService.toString();
}
}
3.4启动项目test-project
3.5 测试:
浏览器访问:http://localhost:8080/test/t
- 页面显示内容:
DemoService(name=zhangSan, pass=123456)
- 后台控制台打印:
zhangSan says : my password is 123456
至此,我们实现了在test-project中直接使用auto-config-starter提供的demoService对象