首页 > 其他分享 >SpringBoot注解~@PropertySource

SpringBoot注解~@PropertySource

时间:2022-12-17 15:45:29浏览次数:54  
标签:PropertySource String redis properties public 注解 class SpringBoot

1. @PropertySource

SpringBoot读取配置信息的方式有以下几种。但实际开发中一个配置文件是不够用的,比如项目中集成mongo redis kafka等需要多个配置文件,这样有利于开发以及维护管理。Springboot通过PropertySource或者PropertySources来实现多配置文件

a)application.properties

b)application-test.properties

c)System.getProperties()

d)操作系统环境变量,如下图示

 

 如下例中,在resources中添加redis.properties mongo.properties文件。只有添加@PropertySource或者@PropertySources指定这两个properties文件才会读取其中内容

 

 

@SpringBootApplication(
exclude = {
MongoAutoConfiguration.class,
MongoReactiveAutoConfiguration.class,
MongoReactiveDataAutoConfiguration.class,
MongoDataAutoConfiguration.class
})
@EnableTransactionManagement
//@PropertySource(value={"classpath:mongo.properties","classpath:redis.properties"})
@PropertySources({@PropertySource("classpath:mongo.properties"),
@PropertySource("classpath:redis.properties")})
public class Application {

public static void main(String[] args) throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
SpringApplication.run(Application.class, args);
}
}
@RestController
@RequestMapping("/test")
public class QueryController {
  @Value("${mongo.property}")
  private String mongoProperty;
  @Value(("${redis.property}"))
  private String redisProperty; 
  @RequestMapping(value="hello10")
  public String say10(){
  System.out.println("sourceProperties:"+mongoProperty);
  return mongoProperty;
  }
  @RequestMapping(value="hello11")
  public String say11(){
   System.out.println("sourceProperties:"+redisProperty);
  return redisProperty;
  }

 

标签:PropertySource,String,redis,properties,public,注解,class,SpringBoot
From: https://www.cnblogs.com/enhance/p/16989047.html

相关文章

  • SpringBoot启动流程
    1.简述Springboot启动是通过Application启动类实现@SpringBootApplication(exclude={MongoAutoConfiguration.class,MongoDataAutoConfiguration.class},......
  • 【SpringBoot】Spring Data Redis封装和Spring Cache
    一、参考资料​​RedisUtil:最全的Java操作Redis的工具类,使用StringRedisTemplate实现,封装了对Redis五种基本类型的各种操作!​​​​SpringCache-简书​​​​redis分布......
  • 【SpringBoot】封装自定义的starter
    一、参考资料​​SpringBoot封装自己的Starter-码农教程​​​​[Gradle]发布构件到本地仓库​​​​Gradle插件之maven-publish:发布androidlibrary到maven仓库-知乎......
  • 注解在Android中的使用场景
    Android课程学习记录注解是在JavaSE5这个版本中引入的1、什么是注解在代码中最常见的一个就是@Override,看一下它的语法定义:@Target(ElementType.METHOD)@Reten......
  • SpringBoot2.x 优秀开源项目
    前后端分离vue开源项目:项目名开源地址eladmin-web​​https://gitee.com/elunez/eladmin-web​​eladmin​​https://gitee.com/elunez/eladmin​​RuoYi-Vue​​https://gi......
  • 阿里云服务器部署springboot项目
    一、安装jdk​​安装jdk​​二、安装mysql下载安装包:​​​rpm-ivhhttp://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm​​设置开机自启动:​​​yum......
  • SSM常用注解与结构
    Mybatis:(一级缓存与二级缓存)注解:@param<selectid=""parameterType=""resultType=""resultMap="">....select,update,insert,delete</select>一对多与多对......
  • SpringBoot(六):配置文件的位置以及优先级
    SpringApplication 从以下位置的 application.properties 文件中加载属性(properties),并将它们添加到Spring Environment 中:项目目录的 /config 子目录项目目录的......
  • 如何在SpringBoot中优雅地重试调用第三方API?
    前言作为后端程序员,我们的日常工作就是调用一些第三方服务,将数据存入数据库,返回信息给前端。但你不能保证所有的事情一直都很顺利。像有些第三方API,偶尔会出现超时。此时,......
  • IDEA中 maven webapp项目和springboot项目 配置热加载(热交换)1.4Tomcat部署时war和war
    目录​​1、提前说明​​​​1.1、idea汉化​​​​1.2idea的项目类型说明​​​​1.3ideawebapp配置tomcat并启动 ​​​​1.4Tomcat部署时war和warexploded区别​​......