步骤:
1.新建一个starter项目,该项目是一个空的项目,只需要在pom.xml中 引入后面的 mail-spring-boot-autoconfigure(需要mail-spring-boot-autoconfigure项目建好并且打包到maven仓库中)
<?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> <version>1.0-SNAPSHOT</version> <groupId>com.example</groupId> <artifactId>mail-spring-boot-starter</artifactId> <name>mail-spring-boot-starter</name> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>mail-spring-boot-autoconfigure</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
2.新建一个**-spring-boot-autoconfiguratio 的spring-boot项目,该项目包含配置类,读取配置文件的映射类,业务类
package com.mail.starter; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 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; @Configuration @ConditionalOnClass(MailProperties.class) @EnableConfigurationProperties(MailProperties.class) public class MailConfiguration { @Bean // @ConditionalOnProperty("czb.mail.enable") public ISendMail sendMail(MailProperties mailProperties){ return new SendMailImpl(mailProperties); } }
package com.mail.starter; import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "czb.mail") public class MailProperties { /** * 邮件发送地址 */ private String address; /** * 邮件抄送地址 */ private String cc; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getCc() { return cc; } public void setCc(String cc) { this.cc = cc; } }
发送邮件的接口和实现
package com.mail.starter; public interface ISendMail { /** * 发送邮件 * @param content 发送内容 */ public void send(String content); }
package com.mail.starter; public class SendMailImpl implements ISendMail{ private MailProperties mailProperties; public SendMailImpl(MailProperties mailProperties){ this.mailProperties = mailProperties; }; public SendMailImpl(){ } @Override public void send(String content) { System.out.println("=============start send mail================"); System.out.println("address:"+mailProperties.getAddress()+";cc:"+mailProperties.getCc()); System.out.println("==============end send mail================"); } }
resources新建META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.mail.starter.MailConfiguration
最后该项目的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> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.13</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>mail-spring-boot-autoconfigure</artifactId> <name>mail-spring-boot-autoconfigure</name> <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-autoconfigure</artifactId> </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> </plugin> </plugins>--> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
3.新建一个spring-boot test项目进行测试进行测试,该项目引入mail-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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.13</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>mail-test</artifactId> <name>mail-test</name> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.example</groupId> <artifactId>mail-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</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> </plugin> </plugins> </build> </project>
application.properties 配置 短信地址
#czb.mail.enable = false czb.mail.address = 124424@qq.com czb.mail.cc = 432424@qq.com
进行测试
package com.example.mail.test; import com.mail.starter.ISendMail; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class MailTestApplicationTests { @Autowired private ISendMail sendMail; @Test void testMail() { sendMail.send("this is a test mail !!!"); } }
打印结果:
=============start send mail================
address:124424@qq.com;cc:432424@qq.com
==============end send mail================
mail-spring-boot-starter
- spring-boot starter spring bootspring-boot starter spring druid spring-boot starter spring boot spring-boot spring-boot spring boot ldap spring-boot-starter spring-boot-starter-parent mybatis-plus spring-boot mybatis spring 静态spring-boot目录spring arthas-spring-boot-starter redisson-spring-boot-starter