简体   繁体   English

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

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

I have two messages.properties files. 我有两个messages.properties文件。 One is located inside resources and another one is outside my .jar file in a directory called etc . 一个是位于内部resources ,另外一个是外目录称为我的.jar文件etc

This is my PropertiesConfiguration class: 这是我的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;
    }
}

In the logs I see this: 在日志中,我看到以下内容:

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]

As I understand my messages.properties from etc is loaded. 据我了解,我的messages.properties来自etc加载。 Although when the application is working, the values from it are not used. 虽然在应用程序运行时,但不使用其值。 They are coming from default messages.properties inside my resources project folder. 它们来自我的resources项目文件夹中的default messages.properties Am I doing something wrong? 难道我做错了什么?

my solution was: 我的解决方案是:

@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;
    }
}

it works perfect on me, remember to omitt the suffix _et.properties on the base name, for example if you have a file named messages_et.properties set base name will result : messageSource.setBasename("file:/path/to/file/messages"); 它对我来说是完美的,请记住省略基本名称的后缀_et.properties ,例如,如果您有一个名为messages_et.properties的文件,则设置基本名称将导致: messageSource.setBasename("file:/path/to/file/messages");

I think there is an answer for this question here: Spring Boot and multiple external configuration files 我认为这里有一个答案: Spring Boot和多个外部配置文件

That worked for me earlier, so it is worth to give it a try! 较早对我有用,因此值得尝试!

First Of all , clear about configuration setting of Spring Boot. 首先,清除Spring Boot的配置设置。

In POM.XML, Confirm once Layout as "ZIP" 在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>

Because ,For the PropertiesLauncher the layout is "ZIP" and make sure avoid to use @EnableAutoConfiguration. 因为对于PropertiesLauncher,布局是“ ZIP”,请确保避免使用@EnableAutoConfiguration。

or Use @PropertySource annotation for external properties. 或将@PropertySource批注用于外部属性。 ( Refer : Spring Boot PropertySource ). (请参阅: Spring Boot PropertySource )。

This Topic already discussed by StackOverflow. StackOverflow已经讨论了该主题。

Refer : 参考:

Spring Boot: Is it possible to use external application.properties files in arbitrary directories with a fat jar? Spring Boot:是否可以在带有胖子的任意目录中使用外部application.properties文件?

Spring @Configuration file with PropertyPlaceholderConfigurer bean doesn't resolve @Value annotation 具有PropertyPlaceholderConfigurer Bean的Spring @Configuration文件无法解析@Value批注

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

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