Spring项目配置管理

配置相关

常见的几种配置方式和优先级说明见下面所示:

  1. 命令行参数
  2. 来自java:comp/env的JNDI属性
  3. Java系统属性System.getProperties()
  4. 操作系统环境变量
  5. RandomValuePropertySource配置的random.*属性值
  6. jar外部的application-{profile}.properties或application.yml(带spring.profile)配置文件
  7. jar内部的application-{profile}.properties或application.yml(带spring.profile)配置文件
  8. jar外部的application.properties或application.yml(不带spring.profile)配置文件
  9. jar内部的application.properties或application.yml(不带spring.profile)配置文件
  10. @Configuration注解类上的@PropertySource
  11. 通过SpringApplication.setDefaultProperties指定的默认属性

命令行参数

@Component
public class UserProperties {  
    @Value("${myname}")  
    private  String myname;
}

启动命令: java -jar *.jar --myname="KevinSun"
其他java 系统属性,操作系统属性都是类似处理方案如果不想通过命令行启动,可以用下面方法取消。

SpringApplication.setAddCommandLineProperties(false);

application-{profile}.properties参数加载

不同的环境可能需要不同配置,可以通过
application-{profile}.properties来解决这个问题.
首先:新建application-dev.properties 文件,增加需要参数启动命令:

java -jar *.jar --spring.profiles.active=dev
application-{profile}.properties 文件和默认的application.properties的加载方式本质是一致的,可以参照4中的内容.需要注意的是:application.properties 会首先被加载.

然后:从application-{profile}.properties中获取替换,所以一些基本固定的值可以写在application.properties, 然后个性化配置写在application-{profile}.properties中。

application.properties 参数加载

SpringBoot中默认的从application.properties文件中加载参数,大量情况下默认是写在这个文件中的.

  1. application.properties 读取的优先级
  2. file:./config/ 当前jar目录的config
  3. file:./ 当前jar目录
  4. classpath:/config/ jar包中classpath的 config目录下
  5. classpath:/ jar包中classpath 路径下
    排列的顺序 就是 加载的优先级,application.properties只会被加载一次

使用自定义文件

如果你觉得 application.properties不够酷,你可以定义自己的文件名, 这里也有两种方式

  1. 使用命令

java -jar *.jar --spring.config.name=myproject
这时就会加载myproject.propertie并且 application.properties不会被加载的.

  1. 使用命令

java -jar *.jar spring.config.location=classpath:/myproject.properties
这种情况下 application.properties也是会被加载,使用方式application-{profile}.properties相同.

使用${}进行属性占位符替换

spring.data.mongodb.host=192.168.1.1
spring.data.mongodb.port=1234
spring.data1.mongodb.host=${spring.data.mongodb.host}_test
spring.data1.mongodb.port=${spring.data.mongodb.port}_test
spring.data1.mongodb.database=${random.value}_test
注意最后一个配置,这使用random 生产随机值的方式, 在测试中可以用来造数据。

类型安全加载

使用@value 进行属性的注入有的时候可能比较笨重, spring 提供一种强类型的bean 来替代这种方式

@Configuration
@EnableConfigurationProperties(UserProperties.class)
public class MyConfiguration{

}

@Component
@ConfigurationProperties(prefix = "spring.data.mongodb")
public class UserProperties {

private  String host;
private  int prot;

}
spring.data.mongodb.host=192.168.1.1
spring.data.mongodb.port=1234

使用application.yaml 参数加载

YAML文档也可以比较好的被支持, YamlPropertiesFactoryBean会将yaml文件当初properties来解析的。

spring:
data.mongodb:

 host:192.168.1.1
 port:27017

但是YAML文件不能通过@PropertySource注解加载. 所以,在这种情况下,如果需要使用@PropertySource注解的方式加载值,那就要使用properties文件。

注解配置

  • @Value
  • @Configuration
  • @Bean
  • @AutoConfiguration
  • @Profile
  • @Conditional
  • @ConfigurationProperties

Value

使用Value可以为类的属性注入初始值,包括注入普通字符、注入操作系统属性、注入表达式结果、注入其他Bean的属性、注入文件内容、注入网址、注入属性文件

Bean

@Bean用于方法中,@Bean(initMethod="init", destroyMethod="destroy")

Profile

一般和@Bean一起使用,在不同的profile下使用不同的配置类,profile可以通过context.getEnvironment.setActiveProfiles("prod");来设置,通常的profile有dev、local、prod、online。

Conditional

在特定条件下创建特定的Bean。使用时实现Condition接口,创建条件类

public class WindowsCondition implements Condition {
    //实现matches方法
}

public class LinuxCondition implements Condition {
    //实现matches方法
}

使用时在不同的条件下配置不同的方法:

@Configuration
public class ConditionConfig {

    @Bean
    @Conditional(WindowsCondition.class)
    public OsConfig windowsConfig(){
    
    }

    @Bean
    @Conditional(WindowsCondition.class)
    public OsConfig linuxConfig(){
    
    }
}

ConfigurationProperties

一般和POJO对象一起使用,例如:

@Component
@ConfigurationProperties(prefix="author", locations={"classpath:config/author.properties"})
class Author {
    private String name;
    private int age;
    
    //getter and setter
}

其他配置

profile配置(多个配置)

application-prod.properties
application.properties
在application.properties中配置spring.profiles.active=prod

Bean相关

  • @Scope
  • @Service
  • @Component
  • @Repository
  • @Controller

Scope

用于描述Spring容器如何创建Bean实例的。

  • Singleton, 默认是单例
  • Prototype
  • Request
  • Session
  • GlobalSession

测试相关

Spring单元测试框架不依赖于任何其他的单元测试框架,可以是Junit也可以支持TestNG。

@RunWith(SpringJunitClassRunner.class)
@ContextConfiguration(Application.class)
@ActiveProfiles("prod")
public DemoClassTest {
    //@Autowired 

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值