POJ 1328 Radar Installation
1、問題描述
在做springboot專案中,我們會發現經常依賴spring-boot-starter-xxx
的pom,知道這種starter為了解決某種業務場景的問題內部其實做了很多maven依賴的封裝,使得引用者不需要配置繁雜的pom,帶著好奇心研究了下這個技術,網上找到一些資料,這裡摘抄一段:SpringBoot中的starter是一種非常重要的機制,能夠拋棄以前繁雜的配置,將其統一整合進starter,應用者只需要在maven中引入starter依賴,SpringBoot就能自動掃描到要載入的資訊並啟動相應的預設配置。starter讓我們擺脫了各種依賴庫的處理,需要配置各種資訊的困擾。SpringBoot會自動通過classpath路徑下的類發現需要的Bean,並註冊進IOC容器。SpringBoot提供了針對日常企業應用研發各種場景的spring-boot-starter依賴模組。所有這些依賴模組都遵循著約定成俗的預設配置,並允許我們調整這些配置,即遵循“約定大於配置”的理念。(引用https://www.cnblogs.com/hello-shf/p/10864977.html)
命名規則:SpringBoot提供的starter以spring-boot-starter-xxx
的方式命名的。官方建議自定義的starter使用xxx-spring-boot-starter
命名規則。以區分SpringBoot生態提供的starter。SpringBoot提供的常見的starter有:(參考:https://www.cnblogs.com/selfchange/p/10014312.html)
- spring-boot-starter
- spring-boot-starter-activemq
- spring-boot-starter-amqp
- spring-boot-starter-aop
- spring-boot-starter-cache
- spring-boot-starter-data-elasticsearch
- spring-boot-starter-data-jdbc
- spring-boot-starter-data-mongodb
- spring-boot-starter-data-redis
2、操作方法
1、demo思路
通過封裝一個郵件傳送的starter,可以支援國外常見的AWS SES, SendGrid兩種方式,通過引用email-springboot-starter後能夠自動裝載配置的方式例項化物件供外部直接呼叫。
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.4.1</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.demo</groupId> <artifactId>email-springboot-starter</artifactId> <version>0.0.1-SNAPSHOT</version> <name>email-springboot-starter</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-autoconfigure</artifactId> </dependency> <!--添加了該模組以來的模組,再配置yml時候會有本模組配置屬性的提示資訊--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!--阿里json操作--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.73</version> </dependency> </dependencies> </project>
3、配置實體類
@ConfigurationProperties(prefix = "email")這個會自動對應到email開頭的配置專案
package email.starter.properties; import lombok.Getter; import lombok.Setter; import lombok.ToString; import org.springframework.boot.context.properties.ConfigurationProperties; /** * @Description: 配置資訊實體 */ @ConfigurationProperties(prefix = "email") public class EmailProperties { private MailConfig awsSesConfig = new MailConfig(); private MailConfig sendGridConfig = new MailConfig(); public MailConfig getAwsSesConfig() { return awsSesConfig; } public MailConfig getSendGridConfig() { return sendGridConfig; } @Getter @Setter @ToString public class MailConfig{ private String userName; private String password; private String sender; } }
4、定義一個介面
package email.starter.service; /** * Email介面 */ public interface EmailService { boolean sendMail(String message); }
5、AWS SES實現類
package email.starter.service; import email.starter.properties.EmailProperties; import org.springframework.beans.factory.annotation.Autowired; /** * AWS SES郵件傳送實現 */ public class AwsSesEmailServiceImpl implements EmailService{ private EmailProperties.MailConfig mailConfig; public AwsSesEmailServiceImpl(EmailProperties.MailConfig mailConfig) { this.mailConfig = mailConfig; } @Override public boolean sendMail(String message) { System.out.println(mailConfig.toString()+" SendEmail->message:"+message); return true; } }
6、SendGrid實現類
package email.starter.service; import email.starter.properties.EmailProperties; import org.springframework.beans.factory.annotation.Autowired; /** * SendGrid郵件傳送實現 */ public class SendGridEmailServiceImpl implements EmailService{ private EmailProperties.MailConfig mailConfig; public SendGridEmailServiceImpl(EmailProperties.MailConfig mailConfig) { this.mailConfig = mailConfig; } @Override public boolean sendMail(String message) { System.out.println(mailConfig.toString()+" SendEmail->message:"+message); return true; } }
7、裝載配置並例項化服務類
@Configuration :宣告這是一個配置物件
@EnableConfigurationProperties(EmailProperties.class) :使EmailProperties物件生效,將其注入IOC容器之中。
package email.starter.config; import email.starter.properties.EmailProperties; import email.starter.service.AwsSesEmailServiceImpl; import email.starter.service.SendGridEmailServiceImpl; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @EnableConfigurationProperties(EmailProperties.class) public class EmailAutoConfiguration { @Bean public AwsSesEmailServiceImpl getAwsSesEmailServiceImpl(EmailProperties emailProperties){ return new AwsSesEmailServiceImpl(emailProperties.getAwsSesConfig()); } @Bean public SendGridEmailServiceImpl getSendGridEmailServiceImpl(EmailProperties emailProperties){ return new SendGridEmailServiceImpl(emailProperties.getSendGridConfig()); } }
8、maveninstall
9、呼叫方引用
<dependency> <groupId>com.demo</groupId> <artifactId>email-springboot-starter</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
10、配置檔案
email: aws-ses-config: sender: '[email protected]' password: 'admin' user-name: 'AwsSes' send-grid-config: sender: '[email protected]' password: 'admin' user-name: 'SendGrid'
11、呼叫程式碼
package email; import email.starter.service.AwsSesEmailServiceImpl; import email.starter.service.EmailService; import email.starter.service.SendGridEmailServiceImpl; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication public class Application { public static void main(String[] args) { ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args); EmailService awsSesEmailService = applicationContext.getBean(AwsSesEmailServiceImpl.class); awsSesEmailService.sendMail("Hello World !"); EmailService sendGridEmailService = applicationContext.getBean(SendGridEmailServiceImpl.class); sendGridEmailService.sendMail("Hello World !"); } }
12、執行效果
這樣就很簡單的可以使用email-springboot-starter呼叫郵件傳送介面了。
"C:\Program Files (x86)\Java\jdk1.8.0_192\bin\java.exe" -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=51328 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=localhost -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true -javaagent:F:\work\ideaIU-2018.1.6.win\lib\idea_rt.jar=51329:F:\work\ideaIU-2018.1.6.win\bin -Dfile.encoding=UTF-8 -classpath "C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\charsets.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\deploy.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\ext\access-bridge-32.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\ext\cldrdata.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\ext\dnsns.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\ext\jaccess.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\ext\jfxrt.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\ext\localedata.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\ext\nashorn.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\ext\sunec.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\ext\sunjce_provider.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\ext\sunmscapi.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\ext\sunpkcs11.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\ext\zipfs.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\javaws.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\jce.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\jfr.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\jfxswt.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\jsse.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\management-agent.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\plugin.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\resources.jar;C:\Program Files (x86)\Java\jdk1.8.0_192\jre\lib\rt.jar;F:\work\workspace-demo\email-springboot-test\target\classes;F:\work\repository\org\springframework\boot\spring-boot-starter-web\2.4.1\spring-boot-starter-web-2.4.1.jar;F:\work\repository\org\springframework\boot\spring-boot-starter\2.4.1\spring-boot-starter-2.4.1.jar;F:\work\repository\org\springframework\boot\spring-boot\2.4.1\spring-boot-2.4.1.jar;F:\work\repository\org\springframework\boot\spring-boot-starter-logging\2.4.1\spring-boot-starter-logging-2.4.1.jar;F:\work\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;F:\work\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;F:\work\repository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;F:\work\repository\org\apache\logging\log4j\log4j-to-slf4j\2.13.3\log4j-to-slf4j-2.13.3.jar;F:\work\repository\org\apache\logging\log4j\log4j-api\2.13.3\log4j-api-2.13.3.jar;F:\work\repository\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;F:\work\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;F:\work\repository\org\springframework\spring-core\5.3.2\spring-core-5.3.2.jar;F:\work\repository\org\springframework\spring-jcl\5.3.2\spring-jcl-5.3.2.jar;F:\work\repository\org\yaml\snakeyaml\1.27\snakeyaml-1.27.jar;F:\work\repository\org\springframework\boot\spring-boot-starter-json\2.4.1\spring-boot-starter-json-2.4.1.jar;F:\work\repository\com\fasterxml\jackson\core\jackson-databind\2.11.3\jackson-databind-2.11.3.jar;F:\work\repository\com\fasterxml\jackson\core\jackson-annotations\2.11.3\jackson-annotations-2.11.3.jar;F:\work\repository\com\fasterxml\jackson\core\jackson-core\2.11.3\jackson-core-2.11.3.jar;F:\work\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.11.3\jackson-datatype-jdk8-2.11.3.jar;F:\work\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.11.3\jackson-datatype-jsr310-2.11.3.jar;F:\work\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.11.3\jackson-module-parameter-names-2.11.3.jar;F:\work\repository\org\springframework\boot\spring-boot-starter-tomcat\2.4.1\spring-boot-starter-tomcat-2.4.1.jar;F:\work\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.41\tomcat-embed-core-9.0.41.jar;F:\work\repository\org\glassfish\jakarta.el\3.0.3\jakarta.el-3.0.3.jar;F:\work\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.41\tomcat-embed-websocket-9.0.41.jar;F:\work\repository\org\springframework\spring-web\5.3.2\spring-web-5.3.2.jar;F:\work\repository\org\springframework\spring-beans\5.3.2\spring-beans-5.3.2.jar;F:\work\repository\org\springframework\spring-webmvc\5.3.2\spring-webmvc-5.3.2.jar;F:\work\repository\org\springframework\spring-aop\5.3.2\spring-aop-5.3.2.jar;F:\work\repository\org\springframework\spring-context\5.3.2\spring-context-5.3.2.jar;F:\work\repository\org\springframework\spring-expression\5.3.2\spring-expression-5.3.2.jar;F:\work\workspace-demo\email-springboot-starter\target\classes;F:\work\repository\org\springframework\boot\spring-boot-autoconfigure\2.4.1\spring-boot-autoconfigure-2.4.1.jar;F:\work\repository\com\alibaba\fastjson\1.2.73\fastjson-1.2.73.jar" email.Application . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.4.1) 2022-03-05 10:22:26.041 INFO 3552 --- [ main] email.Application : Starting Application using Java 1.8.0_192 on PC-20220219NVTS with PID 3552 (F:\work\workspace-demo\email-springboot-test\target\classes started by MAO in F:\work\workspace-demo) 2022-03-05 10:22:26.041 INFO 3552 --- [ main] email.Application : No active profile set, falling back to default profiles: default 2022-03-05 10:22:26.682 INFO 3552 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2022-03-05 10:22:26.697 INFO 3552 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2022-03-05 10:22:26.697 INFO 3552 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.41] 2022-03-05 10:22:26.760 INFO 3552 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2022-03-05 10:22:26.760 INFO 3552 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 688 ms 2022-03-05 10:22:26.916 INFO 3552 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2022-03-05 10:22:27.041 INFO 3552 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2022-03-05 10:22:27.041 INFO 3552 --- [ main] email.Application : Started Application in 1.36 seconds (JVM running for 2.26) EmailProperties.MailConfig(userName=AwsSes, password=admin, [email protected]) SendEmail->message:Hello World ! EmailProperties.MailConfig(userName=SendGrid, password=admin, [email protected]) SendEmail->message:Hello World !
13、原始碼下載
https://github.com/mao2080/email-springboot-starter
3、參考網站
https://www.cnblogs.com/hello-shf/p/10864977.html
個性簽名:1.01的365次方=37.78343433289 >>>1 0.99的365次方= 0.02551796445229 <<<1 每天進步一點點的目標,貴在堅持…