Spring 全家桶之 Spring Boot 2.6.4(二)- Configuration(Part B)(上)

简介: Spring 全家桶之 Spring Boot 2.6.4(二)- Configuration(Part B)

三、@PropertySource和@ImportSource

  • @ImportResource注解可以读取指定的外部配置文件
  • @PropertySource注解可以加载指定的配置文件

与Spring Boot无关的配置文件放在非主配置文件中,例如person相关的配置可以放在新建的person.properties中,可以使用@PropertySource注解来加载这个单独的配置文件,该注解仍然需要和@ConfigurationProperties注解搭配使用。

@PropertySource注解的使用

image.png

Person实体类上使用@PropertySource注解指定配置文件,配置全都写在person.properties中

@ConfigurationProperties(prefix = "person")
@PropertySource(value = {"classpath:person.properties"})
@Component
@Data
public class Person {
    private String lastName;
    private Integer age;
    private Boolean boss;
    private Date birth;
    private Map<String,Object> maps;
    private List<Object> list;
    private Dog dog;
}
复制代码

person.properties配置文件

person.lastName=stark1
person.age=19
person.birth=1980/12/12
person.boss=true
person.maps.k1=v1
person.maps.k2=v2
person.list=a,b,c
person.dog.name=pipi
person.dog.age=2
复制代码

value是一个数组,可以写多个配置文件,@ConfigurationProperties(prefix = "person")默认是指application.properties配置文件,如果需要加载其他配置文件需要@PropertySource注解指定。

执行测试

image.png

@ImportResource注解的使用

@ImportResource注解的作用是可以导入Spring的配置文件,让配置文件里面的配置生效

@ImportResource注解的属性

image.png

value是一个数组,说明可以一次导入多个配置文件。

创建一个service包,创建HelloService,在Spring配置文件spring.xml中配置

public class HelloService {
}
复制代码

在resources目录下创建一个Spring的配置文件spring.xml,用bean标签配置一个Java Bean注册到Spring容器中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="helloService" class="com.lilith.configuration.service.HelloService">
    </bean>
</beans>
复制代码

在主程序上使用@ImportResources注解,导入spring.xml配置文件

@SpringBootApplication
@ImportResource(locations = {"classpath:spring.xml"})
public class ConfigurationApplication {
    // 中间内容不变
}
复制代码

在test包下增加HelloServiceTest,从容器中获取HelloService对象。

@SpringBootTest
public class HelloServiceTest {
    @Autowired
    private HelloService helloService;
    @Test
    public void testBean(){
        System.out.println(helloService);
    }
}
复制代码

执行测试

image.png

控制台成功输出HelloSerivce对象,说明@ImportResource成功导入了spring.xml配置文件。

相比这种通过配置文件方式注册Bean或者组件,Spring Boot 推荐的方式是通过配置类的方式给容器中注入组件,通过全注解的方式注入。

新建config包,增加CustConfig配置类,增加@Configuration表明当前类是一个配置类,替代spring.xml配置文件,@Bean注解作用在方法上,方法的返回值就是注入到容器中的组件,方法名就是组件在容器中的名称。

@Configuration //表明当前类是一个配置类,替代spring.xml配置文件
public class CustConfig {
    // 代替bean标签,将方法返回值注入容器中,默认方法名就是导入容器中的组件的名字
    // 也可以通过name属性指定组件的名称
    @Bean(name = "helloService")
    public HelloService helloService(){
        System.out.println("@Bean注解将HelloService组件添加到容器中!");
        return new HelloService();
    }
}
复制代码

再次执行HelloServiceTest

image.png

通过配置类的方式也可以将组件注入容器中

四、配置文件的占位符

配置文件中可以使用占位符,支持配置文件中的属性引用。

Person实体类上读取主配置文件application.properties

# 使用随机值
person.last-name=stark1${random.value}
person.age=${random.int}
person.birth=1980/12/12
person.boss=true
person.maps.k1=v1
person.maps.k2=v2
person.list=a,b,c
# 引用前面配置过的属性
person.dog.name=${person.last-name}_pipi
person.dog.age=2
复制代码

执行PersonTest测试

image.png

引用不存在的属性

当引用不存在的属性时会原样输出,修改配置文件,引用不存在的person.hello属性

person.dog.name=${person.hello}_pipi
复制代码

执行PersonTest测试

image.png

{person.hello}原样输出。

为引用的不存在的属性设置默认值

属性不存在时为了避免原样输出也可以可以设置默认值,不存在的时候取默认值,在“:”后面设置默认值

person.dog.name=${person.hello:stark}_pipi
复制代码

再次执行PersonTest测试

image.png

成功获取到设置的默认值

五、Profiles多环境支持

Profiles是Spring对不同环境提供不同配置功能的支持,可以通过激活或者指定参数的形式快速切换环境。

application.properties格式的多profiles文件方式

# 使用dev环境
spring.profiles.active=dev
复制代码

application.yml格式多文档块方式

spring:
  profiles:
  # 激活test环境
    active: test
复制代码

命令行方式

java -jar xxx.jar --spring.prifles.active=dev
复制代码

jvm参数方式

-Dspring.profiles.active=dev


相关文章
|
7天前
|
Java Maven 开发工具
IDEA使用Spring Initializr流畅的创建springboot项目
IDEA使用Spring Initializr流畅的创建springboot项目
30 0
|
11天前
|
druid Java 关系型数据库
Spring Boot2 系列教程(二十五)Spring Boot 整合 Jpa 多数据源
Spring Boot2 系列教程(二十五)Spring Boot 整合 Jpa 多数据源
|
4天前
|
NoSQL 前端开发 Java
技术笔记:springboot分布式锁组件spring
技术笔记:springboot分布式锁组件spring
10 1
|
5天前
|
Java API 数据格式
Spring三兄弟:Spring、Spring Boot、Spring Cloud的100个常用注解大盘点
Spring三兄弟:Spring、Spring Boot、Spring Cloud的100个常用注解大盘点
|
8天前
|
运维 Java 关系型数据库
Spring运维之boot项目bean属性的绑定读取与校验
Spring运维之boot项目bean属性的绑定读取与校验
13 2
|
8天前
|
存储 运维 Java
Spring运维之boot项目开发关键之日志操作以及用文件记录日志
Spring运维之boot项目开发关键之日志操作以及用文件记录日志
19 2
|
7天前
|
Java Maven
springboot项目打jar包后,如何部署到服务器
springboot项目打jar包后,如何部署到服务器
24 1
|
8天前
|
XML 运维 Java
Spring运维之boot项目打包jar和插件运行并且设置启动时临时属性和自定义配置文件
Spring运维之boot项目打包jar和插件运行并且设置启动时临时属性和自定义配置文件
13 1
|
3天前
|
SQL Java 数据库
使用Spring Boot和Spring Data JPA进行数据库操作
使用Spring Boot和Spring Data JPA进行数据库操作
|
4天前
|
Java Linux 程序员
技术笔记:Spring生态研习【五】:Springboot中bean的条件注入
技术笔记:Spring生态研习【五】:Springboot中bean的条件注入