簡體   English   中英

Spring Boot:添加了外部messages.properties但未使用

[英]Spring Boot: external messages.properties are being added but not used

我有兩個messages.properties文件。 一個是位於內部resources ,另外一個是外目錄稱為我的.jar文件etc

這是我的PropertiesConfiguration類:

@Configuration
public class PropertiesConfiguration {

    @Bean
    public PropertyPlaceholderConfigurer properties() {
        final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
        ppc.setIgnoreResourceNotFound(true);

        final List<Resource> resourceLst = new ArrayList<Resource>();

        resourceLst.add(new FileSystemResource("etc/application.properties"));
        resourceLst.add(new FileSystemResource("etc/messages.properties"));
        resourceLst.add(new FileSystemResource("etc/messages_et.properties"));

        ppc.setLocations(resourceLst.toArray(new Resource[]{}));

        return ppc;
    }
}

在日志中,我看到以下內容:

11:18:43.764  INFO [main] PropertyPlaceholderConfigurer              - Loading properties file from file [C:\Users\deniss\IdeaProjects\repgen\etc\application.properties]
11:18:43.764  WARN [main] PropertyPlaceholderConfigurer              - Could not load properties from file [C:\Users\deniss\IdeaProjects\repgen\etc\application.properties]: etc\application.properties (The system cannot find the file specified)
11:18:43.764  INFO [main] PropertyPlaceholderConfigurer              - Loading properties file from file [C:\Users\deniss\IdeaProjects\repgen\etc\messages.properties]
11:18:43.764  INFO [main] PropertyPlaceholderConfigurer              - Loading properties file from file [C:\Users\deniss\IdeaProjects\repgen\etc\messages_et.properties]

據我了解,我的messages.properties來自etc加載。 雖然在應用程序運行時,但不使用其值。 它們來自我的resources項目文件夾中的default messages.properties 難道我做錯了什么?

我的解決方案是:

@Configuration   
public class SpringConfiguration {

    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("file:/path/to/file/messages");
        messageSource.setCacheSeconds(10); //reload every 10 sec..
        return messageSource;
    }
}

它對我來說是完美的,請記住省略基本名稱的后綴_et.properties ,例如,如果您有一個名為messages_et.properties的文件,則設置基本名稱將導致: messageSource.setBasename("file:/path/to/file/messages");

我認為這里有一個答案: Spring Boot和多個外部配置文件

較早對我有用,因此值得嘗試!

首先,清除Spring Boot的配置設置。

在POM.XML中,一次確認布局為“ ZIP”

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>  <!-- added -->
                <layout>ZIP</layout> <!-- to use PropertiesLaunchar -->
            </configuration>
        </plugin>
    </plugins>
</build>

因為對於PropertiesLauncher,布局是“ ZIP”,請確保避免使用@EnableAutoConfiguration。

或將@PropertySource批注用於外部屬性。 (請參閱: Spring Boot PropertySource )。

StackOverflow已經討論了該主題。

參考:

Spring Boot:是否可以在帶有胖子的任意目錄中使用外部application.properties文件?

具有PropertyPlaceholderConfigurer Bean的Spring @Configuration文件無法解析@Value批注

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM