springboot在jar包外部读取配置文件

    大家都知道springboot运行web程序都是直接打包成一个jar包然后直接java -jar <jar包路径>就可以跑起来了,但是如果我们需要修改jar包里面的配置文件呢?

1.在源码中修改完再打包一次,然后重新部署;

2.允许jar包运行时去读取jar包外部的配置文件,那么直接修改一下配置文件就重新启动一下就OK;

很明显,第二种方法是比较符合方便定理的;

    那么怎么去读取呢?

1)完全没有框架,使用文件流来读取配置文件(springboot引用了一个底层的自己写的项目);

Properties dataSourceConfig = new Properties();
String absolutePath = new File("").getAbsolutePath(); //获取jar包当前的目录
String filePath = absolutePath + "/db.properties"; // 
filePath = filePath.replace("\\", "/");// 拼好文件路径
File file = new File(filePath); 
InputStream in; // 输入流
if (!file.exists()) {
    in = Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties");
} else {
    in = new FileInputStream(file);
}
dataSourceConfig.load(in);//properties文件转为Properties对象
in.close();//关闭输入流

获取到了Properties对象后就可以拿去读配置出来了!

2)使用spring框架

    首先springboot默认会先去jar包同路径下寻找application.properties文件,如果找不到就会去resource下找application.properties文件,这是springboot默认的配置文件加载机制;

    如果你需要修改application.properties的文件名,那么还有一种默认的加载机制;在application.properties文件中指定另外一个配置文件的后缀名

application.properties文件中配置这么一句:

spring.profiles.active: xx

那么springboot还会按先外再内的规则去加载application-xx.properties文件;

或者直接使用其他名称配置:spring.config.name=xx

有或者指定一个目录:spring.config.location=/a/b

springboot加载配置文件的优先级:

    1.加载jar包目录下的/config文件夹

    2.加载jar包目录下的配置文件

    3.加载resource文件夹下的config文件夹

    4.加载resource目录下的

    当需要自定义配置文件的名称时:

1.建一个配置类,然后通过注解@PropertySource来注入

@Component
@ConfigurationProperties(prefix = DataSourceProperties.PREFIX) //指定前缀
@PropertySource(value = {DataSourceProperties.configPath + "datasource-application.properties"})
public class DataSourceProperties implements BeanClassLoaderAware{
    public static final String PREFIX = "spring.datasource";
    public final static String configPath = "classpath:";

    private ClassLoader classLoader;
    private String scanLocation;
    private Integer maxActive;
    private Integer initialSize;
    private Integer maxWait
    @Override
    public void setBeanClassLoader(ClassLoader classLoader) {
       this.classLoader = classLoader;
    }
    .............
}

2.在启动类中加载配置文件

@Configuration
@ComponentScan("com.liangliang")
@EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class) // 排除springboot内部的配置
//@EnableTransactionManagement
public class ApplicationServer {

    public static void main(String[] args) throws IOException {
        Properties configInfo = new Properties();
        InputStream in = ApplicationServer.class.getClassLoader().getResourceAsStream("xx.properties");
        configInfo.load(in);
        SpringApplication app = new SpringApplication(ApplicationServer.class);
        app.setDefaultProperties(configInfo);
        app.run(args);
    }
}

注意:springboot默认回去加载spring.datasource开头的数据库连接信息,如果你不需要需要在启动类中使用@EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class)排除该属性的加载;

转载于:https://my.oschina.net/u/3477913/blog/983235

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值