如何在JAR文件外部访问Spring配置

如何在JAR文件外部访问Spring配置,spring,spring-boot,Spring,Spring Boot,我是Spring Boot开发的新手,目前无法继续讨论如何在jar文件之外加载Spring应用程序配置的问题 我现有的代码如下所示 private ApplicationContext context; public static void main(String[] args){ SpringApplication.run(SMPPEngine.c1ass); new SMPPEngine(); } public SMPPEngine(){ loadConfigur

我是Spring Boot开发的新手,目前无法继续讨论如何在jar文件之外加载Spring应用程序配置的问题

我现有的代码如下所示

private ApplicationContext context;

public static void main(String[] args){
    SpringApplication.run(SMPPEngine.c1ass);
    new SMPPEngine();
}

public SMPPEngine(){
    loadConfiguration();
    process();
}

private void loadConfiguration(){
    context = new ClassPatthlApplicationContext(”application-context.xm1”);
}
我想要实现的是将jar文件放在一个目录中application-context.xml旁边,这样当配置发生更改时,我就不需要重新编译代码来反映application-context.xml上的更改

根据我在互联网上读到的内容,这可以通过使用file://directory/application.xml'而不是类路径。但我使用后者的问题是,当您将jar和文件放置到其他位置时,我需要进行代码更改以反映新目录,这并不能解决避免代码重新编译的问题

我希望我把我的问题说清楚,并立即得到你们的回应:)
提前感谢:)

有很多方法可以做到这一点,标准的,您可以使用spring
文件:
前缀来访问文件系统路径

但是使用spring boot,您可以在
application.properties
中使用

spring.config.location
property,或者您可以在运行spring启动jar文件时在命令行中添加它,如
java-jarmyproject.jar--spring.config.location=classpath:/default.properties,classpath:/override.properties

但是对于您的代码,实际上您不需要从配置文件中重新创建spring上下文,但是您想要获得上下文实例,只需要注入它

@Autowired
private ApplicationContext context;

另一种方法,如果您有基础设施的话。将是使用。启动应用程序配置为从中读取后,可以随时修改它们,而无需重新编译或重新启动