简体   繁体   English

如何在Spring中使用@PropertySource加载的属性文件中配置“动态键”

[英]how to configure 'dynamic key' in properties file loaded using @PropertySource in Spring

Using Spring 4 @Configuration annotation, this is how I configure a properties file and refer in my application: 使用Spring 4 @Configuration批注,这是我配置属性文件并在我的应用程序中引用的方式:

src/main/resources/errors.properties SRC /主/资源/ errors.properties

name.empty.error=name is empty
department.empty.error=department is empty

Java code configuration and usage: Java代码配置和用法:

@Configuration
@PropertySource("classpath:errors.properties")
public class Sample {
    @Autowired
    private Environment env;

    public void usage() {
        String errorText = env.getProperty("name.empty.error");
    }

I'd like to specify 'key' of my errors.properties in the following manner: 我想通过以下方式指定errors.properties的“键”:

{0}.empty.error={0} is empty

so that the usage() method would call something like 这样usage()方法将调用类似

String errorText1 = env.getProperty("{}.empty.error", "name");
String errorText2 = env.getProperty("{}.empty.error", "department");

How to do this? 这个怎么做? I remember something like this existed in Struts error message handling. 我记得Struts错误消息处理中存在类似的东西。

You need to use MessageSource , which is an interface for resolving messages. 您需要使用MessageSource ,它是解析消息的接口。 ResourceBundleMessageSource is MessageSource implementation which can read from properties file. ResourceBundleMessageSourceMessageSource实现,可以从属性文件读取。

You'll need to configure it (I have not tried to compile the source) 您将需要配置它(我还没有尝试编译源代码)

@Bean
MessageSource messageSource() {
     ResourceBundleMessageSource source=new  ResourceBundleMessageSource();
     source.setBasenames("messages");
     return source;
}

You'll have to wire it and in the code you would call: 您必须将其连接起来,并在代码中调用:

String errorText1 = source.getMessage("empty.error", "name", Locale.US);

and in your messages file you would have: 在您的消息文件中,您将拥有:

empty.error = {0} is empty

As a bonus, your app will be ready for internalization. 作为奖励,您的应用将可以进行内部化。

I think you can do something like this, 我想你可以做这样的事情,

 String errorText1 = env.getProperty(String.format("%s.empty.error", "name"));
 String errorText2 = env.getProperty(String.format("%s.empty.error", "department"));

Edit: 编辑:

It makes no sense, if you use this just to get these specific values from the property file. 如果仅使用此方法从属性文件中获取这些特定值,则没有任何意义。

But if these values are decided during the run-time (give that all these properties are available in the property file). 但是,如果这些值是在运行时确定的(鉴于所有这些属性在属性文件中均可用)。

You can use, 您可以使用,

 String errorText1 = env.getProperty(String.format("%s.empty.error", dynamic-property-value-goes-here));

We can use MessageSource interface for resolving messages. 我们可以使用MessageSource接口来解析消息。

ResourceBundleMessageSource is MessageSource implementation which can read from properties file. ResourceBundleMessageSourceMessageSource实现,可以从属性文件读取。

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/support/ResourceBundleMessageSource.html https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/support/ResourceBundleMessageSource.html

and then call the getMessage() method. 然后调用getMessage()方法。

String org.springframework.context.MessageSource.getMessage(String code, Object[] args, String defaultMessage, Locale locale)


messages.properties file in classpath [src/main/resources/] classpath [src / main / resources /]中的messages.properties文件

empty.error = {0} is empty

Spring-config file Spring配置文件

<bean id="messageSource1" class="org.springframework.context.support.ResourceBundleMessageSource">
  <property name="basenames">
    <value>messages</value>
  </property>
</bean>

Usage in Java class: 在Java类中的用法:

@Autowired
private MessageSource messageSource1;

public MessageSource getMessageSource1() {
  return messageSource1;
}

public void setMessageSource1(MessageSource messageSource1) {
  this.messageSource1 = messageSource1;
}

// exact usage in your Java class method // Java类方法中的确切用法

String [] params = new String[]{"value1"};
log.info(this.messageSource1.getMessage("empty.error", params ,"", Locale.US)); 

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

相关问题 使用@PropertySource的Spring属性配置 - Spring properties configuration using @PropertySource 使用Spring PropertySource无法通过绝对路径找到属性文件 - Cannot find a properties file by absolute path using Spring PropertySource 使用@PropertySource和外部JSON文件配置Spring属性 - Spring properties configuration using @PropertySource with external JSON file 无法使用@PropertySource将.properties文件注入Spring MVC 4.3 - Not able to inject .properties file into Spring MVC 4.3 using @PropertySource 在测试中未加载Spring PropertySource - Spring PropertySource is not loaded in tests 使用相应的专用“属性”文件(使用@PropertySource)设置时会覆盖单个弹簧“beans”属性值 - Individual spring "beans" properties value overridden while setting with corresponding dedicated "properties" file (using @PropertySource) 如何在使用spring boot时配置动态属性? - How to configure dynamic properties while using spring boot? 如何在 Spring 中使用 @PropertySource 加载不同的 xml 文件 - How to load different xml file using @PropertySource in Spring Spring PropertySource找不到的属性 - Properties not found with Spring PropertySource 如何模拟在@Configuration 中注入@PropertySource 的.properties 文件? - How to mock a .properties file injected with @PropertySource in @Configuration?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM