@SpringBootApplication配置详解:
他是一个组合注解,他内部主要包含三个子注解:@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan
@SpringBootConfiguration:他继承@Configuration,说明这是一个配置类,什么是配置类呢?就相当于我们以前写的xml配置,例如我们我们的bean标签,用来实例化一个bean。那么在这个配置类中就是实现以前我们xml配置的功能
@EnableAutoConfiguration:开启自动配置功能,他会扫描带有@Configuration的类,然后初始化这些配置类中的信息并且加入到应用上下文中去,同时完成一些基本的初始化工作
@ComponentScan:组件包扫描,也就是我现在需要扫描哪些包下面的注解,可自动发现和装配一些bean。默认扫描当前启动类所在包下面的类和下面的所有子包
关于springboot的热部署使用插件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
Spring Boot属性配置文件
application.properties 配置文件中声明参数
teacher.id=1
teacher.name=zhangsan
teacher.info=Teacher
{teacher.id}
方法中使用情况
@Value("${teacher.name}")
private String teacherName;
@RequestMapping("/")
@ResponseBody
public String home() {
return "Hello World!" + this.teacherName;
}
}
模板引擎:Thymeleaf(spring boot推荐), FreeMarker
Spring boot默认的模板配置路径为:src/main/resources/templates。当然也可以修改这个路径
集成Thymeleaf步骤:
1.修改pom.xml, 增加如下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.编写Controller
@Controller
public class SampleController {
@RequestMapping("/testThymeleaf")
public String testThymeleaf(ModelMap map) {
// 设置属性
map.addAttribute("name", "zhangsan");
// testThymeleaf:为模板文件的名称
// 对应src/main/resources/templates/testThymeleaf.html
return "testThymeleaf";
}
}
3.在src/main/resources/下面建立**templates/testThymeleaf.html**
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title>testThymeleaf</title>
</head>
<body>
<h1 th:text="${name}">ABC</h1>
</body>
</html>
4.运行spring boot,浏览器输入:[http://localhost:8081/testThymeleaf](http://localhost:8081/testThymeleaf)
集成Swagger2构建RESTful API文档
1.修改pom.xml, 添加Swagger2依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
2.创建Swagger2配置类
在spring boot启动类所在包或子包中创建Swagger配置类SwaggerConfig.java,如下:
![](https://img.haomeiwen.com/i12695031/d592992ba07f4341.png)
SwaggerConfig.java内容如下:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.boot"))// 指定扫描包下面的注解
.paths(PathSelectors.any())
.build();
}
// 创建api的基本信息
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("集成Swagger2构建RESTful APIs")
.description("集成Swagger2构建RESTful APIs")
.termsOfServiceUrl("https://www.baidu.com")
.contact("zhangsan")
.version("1.0.0")
.build();
}
}
3.创建Controller: SwaggerController.java
@RestController
@RequestMapping(value="/swagger")
public class SwaggerController {
@ApiOperation(value="获取用户信息", notes="根据id来获取用户详细信息")
@ApiImplicitParam(name="id", value="用户ID", required=true, dataType="String")
@RequestMapping(value="/{id}", method=RequestMethod.GET)
public Map<String,String> getInfo(@PathVariable String id) {
Map<String ,String> map = new HashMap<String, String>();
map.put("name", "张三");
map.put("age", "34");
return map;
}
}
4.启动Spring boot,访问Swagger UI界面:[http://localhost:8081/swagger-ui.html](http://localhost:8081/swagger-ui.html)
网友评论