Spring Boot: 外部 messages.properties 文件被添加但未被使用。

6 浏览
0 Comments

Spring Boot: 外部 messages.properties 文件被添加但未被使用。

我有两个`messages.properties`文件。其中一个位于`resources`文件夹内,另一个位于名为`etc`的目录中,不在我的.jar文件之内。

这是我的PropertiesConfiguration类:

@Configuration
public class PropertiesConfiguration {
    @Bean
    public PropertyPlaceholderConfigurer properties() {
        final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
        ppc.setIgnoreResourceNotFound(true);
        final List resourceLst = new ArrayList();
        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 - 从文件中加载属性文件 [C:\Users\deniss\IdeaProjects\repgen\etc\application.properties]

11:18:43.764 WARN [main] PropertyPlaceholderConfigurer - 无法从文件中加载属性文件 [C:\Users\deniss\IdeaProjects\repgen\etc\application.properties]: etc\application.properties(系统找不到指定的文件)

11:18:43.764 INFO [main] PropertyPlaceholderConfigurer - 从文件中加载属性文件 [C:\Users\deniss\IdeaProjects\repgen\etc\messages.properties]

11:18:43.764 INFO [main] PropertyPlaceholderConfigurer - 从文件中加载属性文件 [C:\Users\deniss\IdeaProjects\repgen\etc\messages_et.properties]

我理解我的`etc`文件夹下的`messages.properties`被加载了。然而,在应用程序运行时,它的值并没有被使用,而是来自位于`resources`项目文件夹内的默认`messages.properties`文件。我做错了什么吗?

0
0 Comments

Spring Boot中出现(Spring Boot: external messages.properties are being added but not used)这个问题的原因是配置设置错误。解决方法可以通过以下两种方式:

方式一:在POM.XML文件中,确认一下布局设置为"ZIP",以便使用PropertiesLauncher。代码如下:


    
        
            org.springframework.boot
            spring-boot-maven-plugin
              
                ZIP 
            
        
    

方式二:可以使用注解来引入外部属性文件,具体参考Spring Boot PropertySource。相关讨论可以参考以下链接:

- [Spring Boot: Is it possible to use external application.properties files in arbitrary directories with a fat jar?](https://stackoverflow.com/questions/26140784)

- [Spring file with PropertyPlaceholderConfigurer bean doesn't resolve annotation](https://stackoverflow.com/questions/33344895)

需要注意的是,这里的问题不是关于application.properties文件的,而是关于messages.properties文件的。在当前设置下,application.properties文件可以正常工作,不需要进行任何调整。

0
0 Comments

在使用Spring Boot时,遇到了一个问题:外部的messages.properties文件被添加了但是没有被使用。经过我的解决,可以通过以下方式解决该问题。

首先,创建一个Spring配置类,命名为SpringConfiguration。在该类中,我们需要创建一个MessageSource的bean,并设置其属性。具体代码如下:

public class SpringConfiguration {
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("file:/path/to/file/messages");
        messageSource.setCacheSeconds(10); //每10秒重新加载一次
        return messageSource;
    }
}

需要注意的是,basename属性需要设置为对应的properties文件的路径,并且不要包含文件后缀名。例如,如果有一个名为messages_et.properties的文件,那么basename应该设置为"file:/path/to/file/messages"。

接下来,我们需要从默认的messages.properties文件中获取消息。但是,MessageSource对象总是需要一个Locale参数,并且即使我们给定了Locale.getDefault()作为参数,也不会查询默认的properties文件。除非我们将默认值作为字符串指定,否则不会有任何回退,这将使默认的messages.properties文件变得无用。

在我的情况下,路径需要设置为"file:\\C:\\users\\user\\messages\\labels",我的文件名为labels.properties和labels_es.properties。我使用的是Windows 10操作系统。

通过以上步骤,我们可以解决外部messages.properties文件被添加但是没有被使用的问题。

0
0 Comments

在上面的问题中,Spring Boot应用程序中的外部messages.properties文件被添加但未被使用。出现这个问题的原因是messages.properties文件需要使用不同的方法来加载。

解决这个问题的方法是在application.properties文件中添加以下配置:

spring.messages.basename=messages

这将告诉Spring Boot应用程序使用messages作为messages.properties文件的基本名称。接下来,将messages.properties文件放置在src/main/resources目录下。

这样做后,Spring Boot应用程序将能够正确加载并使用外部的messages.properties文件。

0