Spring Boot 快速上手(二)基本配置
来源:互联网 发布:爱稀奇淘宝网 编辑:程序博客网 时间:2024/05/05 19:07
1.一般配置
① 入口类的位置
在上一篇中,我们已经知道,根包路径下的类 SpringBootDemoApplication 是Spring Boot 启动的入口类。在运行其中的main方法时,Spring Boot会自动扫描入口类所在的同级包及其子包中的Bean,所以声明Bean的类不应在入口类所在的包之外。
② 限制自动配置
入口类的注解@SpringBootApplication开启了Spring Boot的自动配置,如果需要限制某些自动配置,可以使用参数exclude,例如:
@SpringBootApplication(exclude = { MongoAutoConfiguration.class, MongoDataAutoConfiguration.class })
③ 加载XML文件
Spring Boot提倡无XML配置,如果确实需要加载必要的XML文件,可以在入口类上使用@ImportResource,例如:
@ImportResource({ "classpath:first-context.xml", "classpath:second-context.xml" })
2.配置文件
① 系统配置
src/main/resources下的application.properties是Spring Boot的全局配置文件,入口类启动时会自动加载该文件。如果在application.properties中配置如下参数:
# 访问路径server.context-path=/demo# 端口号server.port=8090那么上一篇中访问Hello Spring Boot 的路径就变成了http://localhost:8090/demo/hello
② 属性配置
有时需要在配置文件中配置某些业务属性,然后在类里获取属性值,例如:
# 属性值employee.name=Jackemployee.age=26a. 第一种获取属性的方式,可以使用Spring中的@Value获取配置文件里的属性值。新建类EmployeeController:
package net.xxpsw.demo.springboot.employee;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class EmployeeController {@Value("${employee.name}")private String employeeName;@Value("${employee.age}")private Integer employeeAge;@RequestMapping("employee")public String findEmployee() {return String.format("employeeName:%s ===== employeeAge:%s", employeeName, employeeAge);}}于浏览器地址栏中输入http://localhost:8090/demo/employee,结果输出如下:
employeeName:Jack ===== employeeAge:26b. 第二种获取属性的方式,Spring Boot还提供了一种基于Bean的解决方案,既通过@ConfigurationProperties将配置中的属性与Bean的属性相关联,新建实体类Employee:
package net.xxpsw.demo.springboot.employee;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;@Component@ConfigurationProperties(prefix = "employee")public class Employee {// 姓名private String name;// 年龄private Integer age;public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}}在上述类中,@ConfigurationProperties的属性prefix指定了配置文件中属性配置的前缀。现在更改EmployeeController中的代码为:
package net.xxpsw.demo.springboot.employee;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class EmployeeController {@Autowiredprivate Employee employee;@RequestMapping("employee")public String findEmployee() {return String.format("employeeName:%s ===== employeeAge:%s", employee.getName(), employee.getAge());}}于浏览器地址栏中输入http://localhost:8090/demo/employee,结果输出如下:
employeeName:Jack ===== employeeAge:26
③ 日志配置
Spring Boot使用Logback作为默认的日志框架,日志的配置可在配置文件中指定,
a. 指定日志输出位置:
# 指定日志文件位置logging.file=E:/spring-boot.logb. 指定日志输出级别,配置规则如下:logging.level.包路径=级别
# 指定日志输出级别logging.level.org.springframework.web=ERROR
④ 环境配置
假如Spring Boot项目存在多个运行环境,比如研发环境(dev)和生产环境(pro),那么可以通过配置项决定当前运行的环境。
首先,在src/main/resources下,新建研发环境配置文件application-dev.properties
# 端口号server.port=8088新建生产环境配置文件application-pro.properties
# 端口号server.port=80其次,在application.properties中追加以下配置:
# 环境配置spring.profiles.active=dev启动Spring Boot,日志打印如下:
[ main] n.x.d.s.SpringBootDemoApplication : The following profiles are active: dev[ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8088 (http)[ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup[ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8088 (http)[ main] n.x.d.s.SpringBootDemoApplication : Started SpringBootDemoApplication in 2.286 seconds (JVM running for 2.562)
可以看到,此时生效的是研发环境(dev),项目的端口号使用的是application-dev.properties中的端口配置。
然后,修改application.properties中环境配置为:
# 环境配置spring.profiles.active=pro启动Spring Boot,日志打印如下:
[ main] n.x.d.s.SpringBootDemoApplication : The following profiles are active: pro[ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 80 (http)[ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup[ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 80 (http)[ main] n.x.d.s.SpringBootDemoApplication : Started SpringBootDemoApplication in 2.246 seconds (JVM running for 2.523)
可以看到,此时生效的是生产环境(pro),项目的端口号使用的是application-pro.properties中的端口配置。
⑤ 文件类型
除了常规的properties配置文件,Spring Boot还支持yaml语言的配置文件,使用下面的application.yaml文件直接替换项目中原有的application.properties文件,效果是一样的。
server: # 访问路径 context-path: /demo# 属性值employee: name: Jack age: 26logging: # 指定日志文件位置 file: E:/spring-boot.log # 指定日志输出级别 level: org.apache: ERROR org.springframework.web: ERROR org.springframework.boot.web: ERROR# 环境配置spring: profiles: active: dev启动Spring Boot,日志打印如下:
[ main] n.x.d.s.SpringBootDemoApplication : The following profiles are active: dev[ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8088 (http)[ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup[ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8088 (http)[ main] n.x.d.s.SpringBootDemoApplication : Started SpringBootDemoApplication in 2.982 seconds (JVM running for 3.571)可以看到,项目启动时使用的是研发环境配置文件application-dev.properties中配置的端口号8088,在浏览器地址栏中输入http://localhost:8088/demo/employee,页面输出结果如下:
employeeName:Jack ===== employeeAge:26
- Spring Boot 快速上手(二)基本配置
- Spring Boot 快速上手(一)快速搭建
- [转]Spring Boot 揭秘与实战(一) 快速上手
- Spring Boot 快速上手(三)数据操作
- Spring Boot 快速上手(四)事务使用
- Spring Boot 快速上手(五)集成Redis
- Spring Boot 快速上手(六)集成MongoDB
- Spring Boot 快速上手(七)集成ActiveMQ
- Spring Boot 快速上手(八)集成Thymeleaf
- spring boot 框架实用技巧(二)初次上手
- Python快速上手(二)
- spring-boot笔记-属性配置(二)
- Spring Boot核心-基本配置
- Spring Boot核心--基本配置
- Maven快速使用教程(二) spring boot 项目构建
- Spring Boot B-JUI项目快速开发(二)--- spring boot
- spring boot(二)spring data jpa以及事物配置
- 快速上手Spring
- grep
- C#控制台程序实现鼠标左右手习惯切换
- 使用Qemu模拟Cortex-A9运行U-boot和Linux
- JAVA 把小数分成整数和小数
- Eclipse Mar 4.5.2版本安装Spring Tool Suite(STS)
- Spring Boot 快速上手(二)基本配置
- https协议
- 51nod 1804 小C的多边形
- ZAB协议的理解
- Java面向对象基础知识
- Leetcode-Minimum Path Sum(dp)
- Linux CPU 长期90%以上问题排查
- 最常用的linux命令
- 笔记 | 计算机网络基础知识