【问题标题】:Find absolute path of *.properties file in Spring context在 Spring 上下文中查找 *.properties 文件的绝对路径
【发布时间】:2014-12-22 15:36:35
【问题描述】:

我有一些遗留的 jars 我正试图在 spring 上下文中工作。

在我的 applicationContext.xml 中,我使用以下方法加载属性文件:

<context:property-placeholder location="classpath*:META-INF/spring/*.properties" />

...它在 spring 上下文中完美运行。

在旧代码中,我需要获取该配置文件的绝对路径,并且当我运行 mvn tomcat:run 以及将其打包到 war 文件并部署到 Tomcat 容器时它应该可以工作(以防你'想知道,是的,spring 和遗留代码共享同一个 application.properties 配置文件)。

@Service
public class Startup {    

    Logger log = LoggerFactory.getLogger(Startup.class);   

    @PostConstruct
    public void startup() throws Exception {
        if (!LegacyCode.isInit()){
            // this works using a hardcoded path
            LegacyCode.init("/home/user/absolute/path/to/application.properties"); 
            // this doesn't work, but would the preferred solution if it did
            // LegacyCode.init("classpath*:META-INF/spring/*.properties"); 
        }
    }
}

我考虑过使用:

    String config[] = { "classpath*:META-INF/spring/applicationContext.xml" };
    ApplicationContext ctx = new ClassPathXmlApplicationContext(config);

然后使用ctx.getResource劫持路径,但是除了为了获取application.properties的绝对路径第二次加载applicationContext效率很低之外,还会导致@PostConstruct的无限循环正在执行。

遗留代码使用 Commons Configuration(据我所见并基于依赖错误)来设置其配置,我正在寻找的是 Commons Configuration 加载正确 application.properties 文件的方法,无论它是否正在运行在 Linux、Windows 上,来自 WAR 文件或嵌入式 Tomcat 等。

【问题讨论】:

  • Spring xml 中有多个属性文件,但 LegacyCode 只接受一个?
  • 没错,遗留的东西我需要的配置是分段的,所以对于jar1我需要一个特定的配置,对于jar2我需要一个不同的配置,两者都使用Commons Configuration,Spring需要两者,你的答案看起来它可以完成这项工作,稍后会进行测试并获得您的答案。

标签: java spring maven tomcat apache-commons-config


【解决方案1】:

您的 legacyCode 似乎想要一个正确的 filePath,并且不了解 Spring 资源 ("classpath*:META-INF/spring/applicationContext.xml")

我个人会做以下事情:

LegacyCode.init(new File(this.getClass().getClassLoader().getResource("application.properties").toURI()).getPath());

getResource("application.properties") 是 Spring 表示法的 Java 等价物

【讨论】:

  • 工作 100%,这是我最后使用的: LegacyCode.init(new File(this.getClass().getClassLoader().getResource( "META-INF/spring/application.properties) ").toURI()).getPath());谢谢yunandtidus!!
【解决方案2】:

在 spring 上下文中添加这个 beans

<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer" />
<bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer" />

创建类后

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

@Configuration
@PropertySource("classpath:mypropfile.properties")
public class Config {
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}

然后注释你的变量

@Value("${cert.path}") 私有字符串证书路径;

【讨论】:

    猜你喜欢
    • 2011-10-01
    • 2021-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-19
    • 2020-01-20
    相关资源
    最近更新 更多