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=Jack
employee.age=26
a. 第一种获取属性的方式,可以使用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;
@RestController
public 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:26
b. 第二种获取属性的方式,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;
@RestController
public class EmployeeController {
@Autowired
private 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.log
b. 指定日志输出级别,配置规则如下: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: 26
logging:
# 指定日志文件位置
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