spring boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。用我的话来理解,就是 spring boot 其实不是什么新的框架,它默认配置了很多框架的使用方式,就像 maven 整合了所有的 jar 包,spring boot 几乎整合了所有的框架。
一、pon.xml
首先构建一个 maven 项目,其 pom.xml 配置详情如下:
<?xml version="1.0" encoding="UTF8"?>
<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/maven4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>The first Spring Boot project</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency><!-- 核心模块,包括自动配置支持、日志和YAML -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency><!-- 若构建非 web 项目,去掉此web模块即可 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
二、控制层
@RestController
public class HelloController {
@GetMapping(value = "hello")
public String hello() {
return "index";
}
}
如下对其部分注解的释义:
@RestController
:官方文档的意思是:该注解相当于@ResponseBody
+@Controller
合在一起的作用。
- 如果只是使用 @RestController 注解 Controller,则 Controller 中的方法无法返回 jsp 页面,配置的视图解析器 InternalResourceViewResolver 不起作用,返回的内容就是 return 里的内容。
例如:本来应该跳转到 success.jsp 页面,但却显示 success。 - 如果需要返回到指定页面,则需要用 @Controller 配合视图解析器 InternalResourceViewResolver 才行。
- 如果需要返回 JSON,XML 或自定义 mediaType 内容到页面,则需要在对应的方法上加上 @ResponseBody 注解。
@GetMapping
:组合注解,是 @RequestMapping(method = RequestMethod.GET) 的缩写。该注解将 HTTP Get 映射到 特定的处理方法上。
三、启动类
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
如下对其部分注解的释义:@SpringBootApplication
:该注解相当于@Configuration
+ @EnableAutoConfiguration
+ @ComponentScan
@Configuration 的注解类标识这个类可以使用 Spring IoC 容器作为 bean 定义的来源, @Bean 注解告诉 Spring,一个带有 @Bean 的注解方法将返回一个对象,该对象应该被注册为在 Spring 应用程序上下文中的 bean。
@EnableAutoConfiguration:能够自动配置 spring 的上下文,试图猜测和配置你想要的 bean 类,通常会自动根据你的类路径和你的 bean 定义自动配置。
@ComponentScan:会自动扫描指定包下的全部标有 @Component 的类,并注册成bean,当然包括 @Component 下的子注解 @Service,@Repository,@Controller。
至此,一个基本的 spring-boot 项目就算是构建完成了,直接运行该启动类,从控制台中可见如下信息:
Registering beans for JMX exposure on startup
Tomcat started on port(s): 8080 (http)
Started Application in 7.595 seconds (JVM running for 8.892)
表明该项目已正常启动,且默认的 tomcat 端口为 8080。
可直接使用 http://localhost:8080 访问(访问的结果是一个报 404 的错误页面)
四、修改配置
resources 下,新建 application.properties
# 上下文根
server.context-path=/demo
#端口号
server.port=8088
spring-boot 默认的端口为8080且不带上下文根路径,所以直接使用 IP + 端口 即可访问,但对于实际项目很不方便。
修改后即可使用 http://localhost:8088/demo 访问项目。
若是访问前面写的控制层 demo,可用:http://localhost:8088/demo/hello
注意:所返回的是字符串,不是页面。