Java 如何配置';动态键';在Spring中使用@PropertySource加载的属性文件中
使用Spring 4Java 如何配置';动态键';在Spring中使用@PropertySource加载的属性文件中,java,spring,Java,Spring,使用Spring 4@Configuration注释,以下是我在应用程序中配置属性文件和引用的方式: src/main/resources/errors.properties name.empty.error=name is empty department.empty.error=department is empty Java代码配置和使用: @Configuration @PropertySource("classpath:errors.properties") public class
@Configuration
注释,以下是我在应用程序中配置属性文件和引用的方式:
src/main/resources/errors.properties
name.empty.error=name is empty
department.empty.error=department is empty
Java代码配置和使用:
@Configuration
@PropertySource("classpath:errors.properties")
public class Sample {
@Autowired
private Environment env;
public void usage() {
String errorText = env.getProperty("name.empty.error");
}
我想按以下方式指定errors.properties的“key”:
{0}.empty.error={0} is empty
因此usage()
方法将调用
String errorText1 = env.getProperty("{}.empty.error", "name");
String errorText2 = env.getProperty("{}.empty.error", "department");
如何做到这一点?我记得Struts错误消息处理中存在类似的情况。我认为您可以这样做
String errorText1 = env.getProperty(String.format("%s.empty.error", "name"));
String errorText2 = env.getProperty(String.format("%s.empty.error", "department"));
编辑:
如果您使用它只是为了从属性文件中获取这些特定值,那么这是没有意义的
但是如果这些值是在运行时确定的(请确保所有这些属性都在属性文件中可用)
你可以用
String errorText1 = env.getProperty(String.format("%s.empty.error", dynamic-property-value-goes-here));
您需要使用
MessageSource
,这是一个解析消息的界面。是可以从属性文件读取的MessageSource
实现
您需要对其进行配置(我没有尝试编译源代码)
您必须连接它,并在代码中调用:
String errorText1 = source.getMessage("empty.error", "name", Locale.US);
在您的消息文件中,您将有:
empty.error = {0} is empty
作为奖励,您的应用程序将准备好进行内部化。我们可以使用
MessageSource
界面解析消息
ResourceBundleMessageSource
是可以从属性文件读取的MessageSource
实现
然后调用getMessage()
方法
String org.springframework.context.MessageSource.getMessage(字符串代码,对象[]args,字符串defaultMessage,区域设置)
messages.properties类路径[src/main/resources/]中的文件
empty.error = {0} is empty
弹簧配置文件
<bean id="messageSource1" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<value>messages</value>
</property>
</bean>
//在Java类方法中的确切用法
String [] params = new String[]{"value1"};
log.info(this.messageSource1.getMessage("empty.error", params ,"", Locale.US));
看起来是个好主意,+1您的代码可以简化为
stringerrortext1=env.getProperty(“name.empty.error”);字符串errorText2=env.getProperty(“department.empty.error”)代码>只需计算静态占位符。这似乎不是OP所要求的。@kryger是的,简化是正确的,但是,如果需要动态使用这个“名称”,那么我的方法是正确的。我是这样想的。@kryger我现在明白你在这里的意思了。实际上,我假设Diyoda回答说,值是动态传递的,而不是静态传递的,否则是的,这没有任何区别。关于OP,我的问题是如何通过属性资源使用它,尽管String.format
做类似的工作,如果没有完成,我会说你想得太多了,为什么要这样做,而不是添加“empty.error.suffix=is empty
”属性,并且在将字段发送到视图层之前只在字段名称前面加上前缀?您的建议也很有意义,我也可以这样做,但使用一些StringBuilder
或StringBuffer
或String.format
。假设我需要传递两个参数来构建一个值,比如“name在createEmployee中为空”。这里,name
和createEmployee
是两个参数。在本例中,如果我可以传递它们,并且在属性文件中如果我可以读取为{0}
和{1}
,那会更好,对吗?很好,我还将添加Spring配置方法。对不起,迟交了
@Autowired
private MessageSource messageSource1;
public MessageSource getMessageSource1() {
return messageSource1;
}
public void setMessageSource1(MessageSource messageSource1) {
this.messageSource1 = messageSource1;
}
String [] params = new String[]{"value1"};
log.info(this.messageSource1.getMessage("empty.error", params ,"", Locale.US));