简体   繁体   中英

How to pass parameters using spring message PropertyPlaceholderConfigurer?

The code written is working fine if the property message are hard coded.Instead i want to pass dynamic data as parameters and get the message. My developed code is :

public class SpringPropertiesUtil extends PropertyPlaceholderConfigurer {

private static Map<String, String> propertiesMap;
// Default as in PropertyPlaceholderConfigurer
private int springSystemPropertiesMode = SYSTEM_PROPERTIES_MODE_FALLBACK; //Check system properties if not resolvable in the specified properties.

static final Logger logger = LogManager.getLogger(SpringPropertiesUtil.class);

@Override
public void setSystemPropertiesMode(int systemPropertiesMode) {
    super.setSystemPropertiesMode(systemPropertiesMode);
    springSystemPropertiesMode = systemPropertiesMode;
}

@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props) throws BeansException {
    super.processProperties(beanFactory, props);

    propertiesMap = new HashMap<String, String>();
    for (Object key : props.keySet()) {
        String keyStr = key.toString();
        String valueStr = resolvePlaceholder(keyStr, props, springSystemPropertiesMode);
        propertiesMap.put(keyStr, valueStr);
    }
}

public static String getProperty(String name) {
    return propertiesMap.get(name).toString();
}

Message properties file consist of :

myProperty={0} how are you

Now, i want to pass name in the place of {0}. But i couldnot able to.Please let me know if any methods to be defined.

you can autowire the message source and make use of the getMessage method, for example in a controller:

...

@Autowired
private MessageSource messageSource;

...

final String[] params = new String[]{"Obama"};
final String msg = messageSource.getMessage("myProperty", params, LocaleContextHolder.getLocale());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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