简体   繁体   English

Spring 引导强制 PropertySource 从内部读取 jar

[英]Spring boot force PropertySource to read from internal jar

I have my class that reads properties from application.properties in Spring Boot我有我的 class,它从 Spring 启动中的 application.properties 读取属性

@Configuration
@Component
@PropertySource("classpath:application.properties")
public class TestUtil {


    @Value("${usehttps}")
    private String usehttps="";

    @Value("${verifycertificate}")
    private String verifycertificate="";

}

The problem is that I want my code to read the application.properties from my internal jar file only.问题是我希望我的代码仅从我的内部 jar 文件中读取 application.properties。 application.properties is in src/main/resources. application.properties 位于 src/main/resources 中。 If I create an application.properties with same properties on the location where my jar resides, the properties get overwritten by the external file at runtime.如果我在 jar 所在的位置创建具有相同属性的 application.properties,则这些属性会在运行时被外部文件覆盖。 It can be a security issue in my case.就我而言,这可能是一个安全问题。

Can this be achieved?这可以实现吗?

The order in which your jar file and properties file are found in CLASSPATH matters here, as well as classloaders hierarchy (which JarLauncher gives you). jar 文件和属性文件在CLASSPATH中的顺序以及类加载器层次结构(JarLauncher 为您提供)很重要。

You can try to redefine bean for ResourcePatternResolver in order to change this behavior and manually scan all classloaders for the resource.您可以尝试为ResourcePatternResolver重新定义 bean 以更改此行为并手动扫描资源的所有类加载器。 But I would try a simple rearranging of CLASSPATH entries first.但我会先尝试简单地重新排列 CLASSPATH 条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM