Spring快速使用及配置
参考文档 Spring Boot Reference Guide
添加pom依赖
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.example</groupId> 7 <artifactId>myproject</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 10 <!-- Inherit defaults from Spring Boot --> 11 <parent> 12 <groupId>org.springframework.boot</groupId> 13 <artifactId>spring-boot-starter-parent</artifactId> 14 <version>2.1.6.RELEASE</version> 15 </parent> 16 17 <!-- Add typical dependencies for a web application --> 18 <dependencies> 19 <dependency> 20 <groupId>org.springframework.boot</groupId> 21 <artifactId>spring-boot-starter-web</artifactId> 22 </dependency> 23 </dependencies> 24 25 <!-- Package as an executable jar --> 26 <build> 27 <plugins> 28 <plugin> 29 <groupId>org.springframework.boot</groupId> 30 <artifactId>spring-boot-maven-plugin</artifactId> 31 </plugin> 32 </plugins> 33 </build> 34 35 </project>
编写代码
要完成我们的应用程序,我们需要创建一个Java文件。默认情况下,Maven 从 编译源,因此您需要创建该文件夹结构,然后添加一个名为包含以下代码的文件:src/main/java
src/main/java/Example.java
1 import org.springframework.boot.*; 2 import org.springframework.boot.autoconfigure.*; 3 import org.springframework.web.bind.annotation.*; 4 5 @RestController 6 @EnableAutoConfiguration 7 public class Example { 8 9 @RequestMapping("/") 10 String home() { 11 return "Hello World!"; 12 } 13 14 public static void main(String[] args) { 15 SpringApplication.run(Example.class, args); 16 } 17 }
运行示例
此时,您的应用程序应该可以正常工作。由于您使用了 POM,因此您有一个有用的目标,可用于启动应用程序。从根项目目录中键入以启动应用程序。您应看到类似于以下内容的输出:spring-boot-starter-parent
run
mvn spring-boot:run
$ mvn spring-boot:run . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.6.RELEASE) ....... . . . ....... . . . (log output here) ....... . . . ........ Started Example in 2.222 seconds (JVM running for 6.514)
如果将 Web 浏览器打开 为,则应看到以下输出:localhost:8080
Hello World!
Table 13.1. Spring Boot application starters启动类
Name | Description | Pom |
---|---|---|
Core starter, including auto-configuration support, logging and YAML |
||
Starter for JMS messaging using Apache ActiveMQ |
||
Starter for using Spring AMQP and Rabbit MQ |
||
Starter for aspect-oriented programming with Spring AOP and AspectJ |
||
Starter for JMS messaging using Apache Artemis |
||
Starter for using Spring Batch |
||
Starter for using Spring Framework’s caching support |
||
Starter for using Spring Cloud Connectors which simplifies connecting to services in cloud platforms like Cloud Foundry and Heroku |
||
Starter for using Cassandra distributed database and Spring Data Cassandra |
||
Starter for using Cassandra distributed database and Spring Data Cassandra Reactive |
||
Starter for using Couchbase document-oriented database and Spring Data Couchbase |
||
Starter for using Couchbase document-oriented database and Spring Data Couchbase Reactive |
||
Starter for using Elasticsearch search and analytics engine and Spring Data Elasticsearch |
||
Starter for using Spring Data JDBC |
||
Starter for using Spring Data JPA with Hibernate |
||
Starter for using Spring Data LDAP |
||
Starter for using MongoDB document-oriented database and Spring Data MongoDB |
||
Starter for using MongoDB document-oriented database and Spring Data MongoDB Reactive |
||
Starter for using Neo4j graph database and Spring Data Neo4j |
||
Starter for using Redis key-value data store with Spring Data Redis and the Lettuce client |
||
Starter for using Redis key-value data store with Spring Data Redis reactive and the Lettuce client |
||
Starter for exposing Spring Data repositories over REST using Spring Data REST |
||
Starter for using the Apache Solr search platform with Spring Data Solr |
||
Starter for building MVC web applications using FreeMarker views |
||
Starter for building MVC web applications using Groovy Templates views |
||
Starter for building hypermedia-based RESTful web application with Spring MVC and Spring HATEOAS |
||
Starter for using Spring Integration |
||
Starter for using JDBC with the HikariCP connection pool |
||
Starter for building RESTful web applications using JAX-RS and Jersey. An alternative to |
||
Starter for using jOOQ to access SQL databases. An alternative to |
||
Starter for reading and writing json |
||
Starter for JTA transactions using Atomikos |
||
Starter for JTA transactions using Bitronix |
||
Starter for using Java Mail and Spring Framework’s email sending support |
||
Starter for building web applications using Mustache views |
||
Starter for using Spring Security’s OAuth2/OpenID Connect client features |
||
Starter for using Spring Security’s OAuth2 resource server features |
||
Starter for using the Quartz scheduler |
||
Starter for using Spring Security |
||
Starter for testing Spring Boot applications with libraries including JUnit, Hamcrest and Mockito |
||
Starter for building MVC web applications using Thymeleaf views |
||
Starter for using Java Bean Validation with Hibernate Validator |
||
Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container |
||
Starter for using Spring Web Services |
||
Starter for building WebFlux applications using Spring Framework’s Reactive Web support |
||
Starter for building WebSocket applications using Spring Framework’s WebSocket support |