Spring Boot免除了项目中大部分的手动配置,对于一些特定情况,可以通过修改全局配置文件以适应具体的开发或生产环境,但是有时候项目中不可避免地要使用默认配置文件之外的配置信息,这个时候就需要手动引入配置文件或配置类
Spring Boot项目中引入的配置文件通常有两类,**第一类为YAML或properties的属性配置文件****;**第二类为XML配置文件****。一般第一类配置文件可以使用**@PropertySource****引入,第二类配置文件可以使用**@ImportResource****引入。
**使用@PropertySource引入属性配置文件****
使用的是user.properties
@PropertySource注解标注在类上,可以指定引入的配置文件的位置和名称。如果需要将自定义配置文件中的属性值注入到对应类的属性中,可以使用@ConfigurationProperties或者@Value注解进行注入。
id=1021
name=liya
-
创建实体类。在org.example.day0301.domian包下创建用户实体类User,在类上使用@PropertySource引入配置文件user.properties,并使用@ConfigurationProperties注解将配置文件中的属性绑定到类的属性上。注:如果直接写入@PropertySource("classpath:user.properties")没有用的话,可以尝试写成:@PropertySources({ @PropertySource("classpath:user.properties") })
-
创建实体类。在 org.example.day0301.domian包下创建用户实体类User,在类上使用@PropertySource引入配置文件user.properties,并使用@ConfigurationProperties注解将配置文件中的属性绑定到类的属性上。
举个栗子:
-
package org.example.day0301.domian;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.stereotype.Component;
@Component
@PropertySource("classpath:user.properties")
@ConfigurationProperties
public class User {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
'}';
}
}
-
新增测试方法。在ApplicationTests 类中注入User对象,并新增测试方法testuser(),在测试方式中输出User对象
-
栗子表达:
@Autowired private User user; @Test void testuser() { System.out.println(user); }
**2. 使用@ImportResource引入XML配置文件****
传统Spring框架大多采用XML文件作为配置文件,但Spring Boot推荐使用Java配置类进行配置,**Spring Boot默认不能自动识别XML配置文件****,想让Spring的XML配置文件生效,可以使用**@ImportResource注解加载XML配置****文件。**@ImportResource注解标注在一个配置类上****,使用时需要指定引入XML配置文件的路径和名称
-
创建组件类。在org.example.day0301.service包下创建类MyService,在类中定义方法用于后续测试。
-
创建XML配置文件。resources文件夹下创建配置文件,在该配置文件中声明Bean。
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="myService" class="org.example.day0301.service.MyService"/>
</beans>
-
添加@ImportResource注解。编写完Spring的XML配置文件后,Spring Boot默认不会自动引入,为了保证XML配置文件生效,需要在项目启动类Application上添加@ImportResource注解来指定XML文件的位置
-
举个板栗:
package org.example.day0301; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; @SpringBootApplication @ImportResource("classpath:testXml.xml") public class Day0301Application { public static void main(String[] args) { SpringApplication.run(Day0301Application.class, args); } }
-
新增测试方法。在文件2-3的Day0301ApplicationTests类中注入MyService对象,并新增测试方法test(),在测试方式中使用MyService对象调用getById()方法,具体代码如下。
@Autowired
private MyService myService;
@Test
void test(){
myService.getByid("1");
}
**四、Profile****
**1. 单一文件中配置Profile****
在实际开发中,根据项目的开发进度,项目经常需要在不同的**部署环境间切换****,常见部署的环境有**开发环境****、**测试环境****、**生产环境****。不同环境使用的**配置信息****往往不同,而且项目的配置信息往往有很多,如果每次变更项目部署的环境时,都采用手动方式更改配置信息会很麻烦。针对这种情况,在Spring Boot中可以使用Profile解决这类问题,Profile使Spring Boot可以针对不同的环境提供不同的配置。在Spring Boot中可以将Profile配置在单一文件中和多个文件中,也可以通过@Profile注解指定Bean的生效环境。
SpringBoot中可以在配置文件使用**spring.config.activate.on-profile****指定Profile的名称,使用**spring.profiles.active****指定激活那个Profile,如果需要激活多个Profile,Profile名称之间使用**逗号间隔****即可
每个Profile中的配置信息都对应于一个部署环境,在单一YAML文件配置多个Profile时,可以通过**三个短横线号(---)****将不同的Profile分隔开。
-
配置Profile。在项目day0301的application.yml配置文件中配置3个Profile,名称分别为dev、test、pro,表示开发环境、测试环境和生产环境。
#激活profiles
spring:
profiles:
active: dev
---
#开发环境
server:
port: 80
spring:
config:
activate:
on-profile: dev
---
#生产环境
server:
port: 81
spring:
config:
activate:
on-profile: prod
---
#测试环境
server:
port: 82
spring:
config:
activate:
on-profile: test
-
创建控制器类。在com.gcxy.controller包下创建控制器类TestController,在类中注入Environment对象,并定义获取项目服务端口的方法。
package org.example.day0301.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
// 注入环境变量
private Environment environment;
@RequestMapping("/getPort")
public String getProt(){
String prot = environment.getProperty("server.port");
return "Prot"+prot;
}
}
-
测试程序效果。启动项目,控制台输出对应的信息。
(4)将配置文件中激活的Profile修改为dev、pro
**2. 多文件中配置Profile****
在实际开发中,项目中通常会包含多个组件或框架,如果将所有的配置信息都放在一个配置文件中,尤其是配置的部署环境都不一样时,配置文件会非常臃肿,不便于维护。针对此种情况,可以将一个配置文件拆分成多个配置文件。拆分后,可在不同的配置文件中不同环境的配置,主配置文件中指定激活的Profile。
拆分出的配置文件的名称格式为**application-{profile}.yml或application-{profile}.properties****,其中**{profile}****对应具体环境标示的**Profile名称****。例如,YAML格式的开发环境、测试环境和生产环境配置文件命名如下。
application-dev.yml // 开发环境配置文件
application-test.yml // 测试环境配置文件
application-pro.yml // 生产环境配置文件
-
拆分配置文件。将配置文件中开发环境、测试环境、生产环境的Profile拆分为3个文件。
# application.yml // 开发环境配置文件
spring:
profiles:
active: pro
# application-dev.yml // 开发环境配置文件
server:
port: 80
# application-test.yml // 测试环境配置文件
server:
port: 81
# application-pro.yml // 生产环境配置文件
server:
port: 82
-
测试程序效果。此时项目application.yml配置文件中指定激活的Profile为dev,启动项目,控制台输出dev的信息。
**3.其他启动方式****
1、idea中配置启动环境
可以在箭头选择那里直接输入dev
2、使用命令的方式cmd运行jar包
java -jar day0301-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev