starter 包含 自動設定及相關 library
官方都是以 spring-boot-starter-*
命名。第三方要以 *-spring-boot-starter
命名,ex: druid-spring-boot-starter
分類
application
name | desc |
---|---|
spring-boot-starter | 核心 starter,含自動設定、log 及支援 YAML |
spring-boot-starter-amqp | Spring AMQP, Rabbit MQ |
spring-boot-starter-aop | Spring AOP |
spring-boot-starter-artemis | Apache Artemis,支援 JMS 的 MQ |
spring-boot-starter-batch | Spring Batch |
spring-boot-starter-cache | Spring Cache |
spring-boot-starter-data-cassandra | Cassandra + Spring Data Cassandra |
spring-boot-starter-data-cassandra-reactive | Cassandra + Spring Data Cassandra Reactive |
spring-boot-starter-data-couchbase | Couchbase + Spring Data Couchbase |
spring-boot-starter-data-couchbase-reactive | Couchbase + Spring Data Couchbase Reactive |
spring-boot-starter-data-elasticsearch | Elasticsearch + Spring Data Elasticsearch |
spring-boot-starter-data-jdbc | Spring Data JDBC |
spring-boot-starter-data-jpa | Spring Data JPA + Hibernate |
spring-boot-starter-data-ldap | Spring Data LDAP |
spring-boot-starter-data-mongodb | MongoDB + Spring Data MongoDB |
spring-boot-starter-data-mongodb-reactive | MongoDB + Spring Data MongoDB Reactive |
spring-boot-starter-data-neo4j | Neo4J + Spring Data Neo4J |
spring-boot-starter-data-r2dbc | Spring Data R2DBC |
spring-boot-starter-data-redis | Redis + Spring Data Redis + Lettuce |
spring-boot-starter-data-redis-reactive | Redis + Spring Data Redis Reactive + Lettuce Client |
spring-boot-starter-data-rest | Spring Data REST + Spring Data repositories,輸出 REST |
spring-boot-starter-freemarker | 以 FreeMarker View 建立 Spring Web application |
spring-boot-starter-graphql | Spring GraphQL |
spring-boot-starter-grovvy-templates | Groovy View 建立 Spring Web application |
spring-boot-starter-hateoas | Spring MVC + Spring HATEOAS 建立 RESTful Web application |
spring-boot-starter-integration | Spring Integration |
spring-boot-starter-jdbc | JDBC + HikariCP connection pool |
spring-boot-starter-jersey | JAX-RS + Jersey 建立 RESTful Web application,可替代 spring-boot-starter-web |
spring-boot-starter-jooq | jOOQ 存取 SQL database。可替代 spring-boot-starter-data-jpa 或 spring-boot-starter-jdbc |
spring-boot-starter-json | 讀寫 JSON |
spring-boot-starter-mail | Java Mail + Spring Mail Sender |
spring-boot-starter-mustache | 以 Mustache view 建立 Web Application |
spring-boot-starter-oauth2-client | Spring Security's OAuth2/OpenID 客戶端連線 |
spring-boot-starter-oauth2-resource-server | Spring Security's OAuth2 資源伺服器 |
spring-boot-starter-quartz | Quartz |
spring-boot-starter-rsocket | RSocket client and server |
spring-boot-starter-security | Spring Security |
spring-boot-starter-test | JUnit Jupiter + Hamcrest + Mockito |
spring-boot-starter-thymeleaf | Thymeleaf View 建立 MVC web application |
spring-boot-starter-validation | Java Bean Validation + Hibernate Validator |
spring-boot-starter-web | Spring MVC 建立 RESTful Web application,以 Tomcat 為內嵌伺服器 |
spring-boot-starter-web-services | Spring Web Services |
spring-boot-starter-webflux | Spring Reactive Web 建立 WebFlux application |
spring-boot-starter-websocket | Spring WebSocket |
如果官方沒有的 starter,可使用第三方自制的 Spring Boot Starter,ex: Dubbo, ZooKeeper, MyBatis
production
name | desc |
---|---|
spring-boot-starter-actuator | Sprign Boot Actuator,正式環境的監控與應用管理 |
technical
可排除或替換預設的技術套件
name | desc |
---|---|
spring-boot-starter-jetty | 以 Jetty 為 servlet container,替代 spring-boot-starter-tomcat |
spring-boot-starter-log4j2 | log4j2,替代 spring-boot-starter-logging |
spring-boot-starter-logging | Logback |
spring-boot-starter-reactor-netty | 以 Netty 作為內嵌的 reactive http server |
spring-boot-starter-tomcat | 以 Tomcat 為內嵌的 servlet container,這是預設的,也被用在 spring-boot-starter-web |
spring-boot-starter-undertow | 以 Undertow 作為內嵌的 servlet container,可替代 spring-boot-starter-tomcat |
ex: 使用 Jetty 替代 tomcat
修改pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- exclude tomcat -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
<!-- <artifactId>spring-boot-starter-undertow</artifactId> -->
</dependency>
自動設定
所有自動設定的類別都是由 spring-boot-autoconfigure 模組提供的
ref: # 深入理解自動配置原理之@SpringApplcation
MailSender
spring-boot-start-mail 提供了
org.springframework.mail.javamail.JavaMailSender
org.springframework.mail.javamail.JavaMailSenderImpl
另外有一個自動設定類別
- org.springframework.boot.autoconfigure.mail.MailSnederAutoConfiguration
該類別被註冊到這個檔案裡面,檔案內容就是自動設定類別的字串
/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
application.yml
spring:
mail:
host: smtp.gmail.com
username: service@larzio.com
password: XXXXXX
properties:
"[mail.smtp.socketFactory.class]": javax.net.ssl.SSLSocketFactory
"[mail.smtp.socketFactory.fallback]": false
"[mail.smtp.socketFactory.port]": 465
"[mail.smtp.connectiontimeout]": 5000
"[mail.smtp.timeout]": 3000
"[mail.smtp.writetimeout]": 5000
mail:
from: service@test.com
fromname: service
bcc:
subject: Spring Boot Mail Test
Demo1Application.java
package tw.com.test.demo1;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.RestController;
@EnableConfigurationProperties({MailProperties.class})
@RequiredArgsConstructor
@SpringBootApplication
@RestController
public class Demo1Application {
public static void main(String[] args) {
SpringApplication.run(Demo1Application.class);
}
}
MailProperties.java
package tw.com.test.demo1;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Data
@ConfigurationProperties(prefix = "mail")
public class MailProperties {
private String from;
private String fromname;
private String bcc;
private String subject;
}
EmailController.java
package tw.com.test.demo1;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.io.UnsupportedEncodingException;
@Slf4j
@RequiredArgsConstructor
@RestController
public class EmailController {
private final JavaMailSender javaMailSender;
private final MailProperties mailProperties;
@RequestMapping("/sendSimpleEmail")
@ResponseBody
public boolean sendSimpleEmail(@RequestParam("email") String email, @RequestParam("text") String text) {
try {
SimpleMailMessage msg = createSimpleMsg(email, text);
javaMailSender.send(msg);
} catch (Exception ex) {
log.error("Error:", ex);
return false;
}
return true;
}
@RequestMapping("/sendMimeEmail")
@ResponseBody
public boolean sendMimeEmail(@RequestParam("email") String email, @RequestParam("text") String text) {
try {
MimeMessage msg = createMimeMsg(email, text, "java.png");
javaMailSender.send(msg);
} catch (Exception ex) {
log.error("Error:", ex);
return false;
}
return true;
}
/**
* @param email
* @param text
* @param attachmentClassPathFilename
* @return
* @throws MessagingException
* @throws UnsupportedEncodingException
*/
private MimeMessage createMimeMsg(String email, String text, String attachmentClassPathFilename) throws MessagingException, UnsupportedEncodingException {
MimeMessage msg = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(msg, true);
mimeMessageHelper.setFrom(mailProperties.getFrom(), mailProperties.getFromname());
mimeMessageHelper.setTo(email);
if (!mailProperties.getBcc().equals("")) {
mimeMessageHelper.setBcc(mailProperties.getBcc());
}
mimeMessageHelper.setSubject(mailProperties.getSubject());
mimeMessageHelper.setText(text);
mimeMessageHelper.addAttachment(attachmentClassPathFilename, new ClassPathResource(attachmentClassPathFilename));
return msg;
}
/**
* @param email
* @param text
* @return
*/
private SimpleMailMessage createSimpleMsg(String email, String text) {
SimpleMailMessage msg = new SimpleMailMessage();
msg.setFrom(mailProperties.getFrom());
msg.setTo(email);
if (!mailProperties.getBcc().equals("")) {
msg.setBcc(mailProperties.getBcc());
}
msg.setSubject(mailProperties.getSubject());
msg.setText(text);
return msg;
}
}
測試
http://localhost:8080/sendMimeEmail?email=charley@maxkit.com.tw&text=hello
http://localhost:8080/sendSimpleEmail?email=charley@maxkit.com.tw&text=hello
沒有留言:
張貼留言