SpringBoot加载指定配置文件
SpringBoot局部配置文件
1、使用 @ConfigurationProperties 注解给JavaBean注入属性值,默认是从全局配置文件(application.properties/application.yml)中获取值,但是所有的配置数据全部写在全局配置文件中,会显得太臃肿,所以可以抽取出来,放在其他局部配置文件中
2、定义一个emp.properties配置文件,内容如下:
emp.last-name=李四
emp.age=30
emp.birthday=1989/9/12
emp.boss=false
emp.salary=23000
emp.map.key1=value1
emp.map.key2=value2
emp.list=one, two, three
emp.forte.name=python
emp.forte.time=3
3、在对应的实体javaBean中,使用 @PropertySource(value = {“classpath:emp.properties”}) 来指明 当前javaBean是从 emp.properties 配置文件中取出属性值来注入
springBoot注入xml文件
1、SpringBoot提倡零配置, 即无xml配置,但是在实际开发中,可能有一些特殊要求必须使用 xml 配置;这时我们可以通过 Spring 提供的 @ImportResource 来加载 xml 配置文件
2、创建Emp的业务类:EmpService
public class EmpService {
public void add() {
System.out.println("add()...");
}
}
这里也可以直接使用spring的注解 @Service 将该类作为一个组件扫描到spring容器中,但是这里使用xml的bean标签的方式来注入
3、构建xml文件:spring01.xml
<?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.xsd">
<bean id="empService" class="com.dss.springboot.service.EmpService"></bean>
</beans>
4、将 spring01.xml 配置文件加载到spring容器中:即在springboot项目的启动类上加上 @ImportResource 注解:
/**
* @ImportResource(locations = {"classpath:spring01.xml"})
* 表示将 spring01.xml 配置文件加载到spring 容器中
* @author asong
*
*/
@ImportResource(locations = {"classpath:spring01.xml"})
@SpringBootApplication
public class SpringBootConfigApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootConfigApplication.class, args);
}
}
5、测试EmpService组件是否注入到spring容器中
@Autowired
private ApplicationContext context;
@Test
public void testXml() {
EmpService empService = (EmpService)context.getBean("empService");
System.out.println(empService);
empService.add();
}
测试结果:EmpService组件被注入到spring容器中。
通过自定义配置类向spring容器注入组件
Spring Boot 推荐使用注解的方式向容器中注入组件, 操作如下:
1、使用 @Configuration 来定义配置类
/**
* @Configuration 用于标识当前类是一个配置类,可以通过这个类来向spring容器注入相应的bean,相当于spring的xml配置文件
* @author asong
*
*/
@Configuration
public class EmpConfig {
/**
* @Bean 注解 用于标识 当前方法是向spring容器 注入组件
*
* 1、方法的返回值,是向容器注入的组件的对象
* 2、方法名,是注入的这个组件的 id 值
*
* @return
*/
@Bean
public EmpService empService1() {
System.out.println("@Bean 注解已经将 EmpService 组件注入spring容器中");
return new EmpService();
}
}
2、去掉springboot项目启动类中的 @ImportResource(locations = {“classpath:spring01.xml”}) 注解
3、测试EmpService组件 ,id为 empService1 的bean是否被注入到spring容器中
@Autowired
private ApplicationContext context;
@Test
public void testXml() {
/*EmpService empService = (EmpService)context.getBean("empService");
System.out.println(empService);
empService.add();*/
EmpService empService = (EmpService)context.getBean("empService1");
System.out.println(empService);
empService.add();
}
测试结果:id为 empService1 的EmpService对象被成功注入spring容器中
Profile多环境支持
在实际开发应用中,在开发环境中的配置和在生成环境中的配置是不一样的,所以,可以通过profile多环境的方式来区分不同环境的不同配置
1、properties文件方式:
1)、定义三个配置文件:application.properties / application-dev.properties / application-prod.properties
2)、application.properties文件中的内容为:
#默认的启动端口
server.port=8080
application-dev.properties文件内容:
#开发环境启动端口
server.port=8081
application-prod.properties文件内容
#生产环境启动端口
server.port=8082
3、此时如果启动springboot项目,启动端口是默认端口 8080,但是如果在 application.properties 文件中加上: spring.profiles.active=dev ,则项目的启动端口就变成了8081,同理,改成spring.profiles.active=prod 则启动端口是8082,通过这个方式来改变项目启动时,使用不同环境的配置
4、使用yml文件来达成profile多环境
server:
port: 8080 #默认端口号
spring:
profiles:
active: prod
---
server:
port: 8081
spring:
profiles: dev #开发环境使用
---
server:
port: 8082
spring:
profiles: prod #生产环境使用
5、除了在配置文件中,通过 spring.profiles.active来指定 profile 外,还可以在启动项目的时候,
通过传入命令行参数:–spring.profiles.active=dev,
或者指定虚拟机参数:-Dspring.profiles.active=dev
来指定profile
配置文件的加载位置
SpringBoot 启动时,会扫描以下位置的 application.properties 或者 application.yml 文件作为
Spring Boot的默认配置文件:
配置文件位置 | 说明 |
file:./config/ | 当前项目的config目录下(最高级别) |
file:./ | 当前项目的根目录下 |
classpath:/config/ | 类路径的config目录下 |
classpath:/ | 类路径的根目录下(最低级别) |
说明:以上按照优先级从低到高的顺序,将所有位置的配置文件全部加载,高优先级的配置内容会覆盖低优先级的配置内容