15

Is it possible to do this ? Currently it is done like this :

<bean id="resource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>content.Language</value> 
        </list>
    </property>
</bean>

@Autowired
protected MessageSource resource;

protected String getMessage(String code, Object[] object, Locale locale) {
    return resource.getMessage(code, object, locale);
}

Is there a way for it to be like getting properties via @Value annotation ?

<util:properties id="generals" location="classpath:portlet.properties" />

    @Value("#{generals['supported.lang.codes']}")
    public String langCodes;

Because having to call the method is usually fine, but for instance when unit testing, this is pain in ... ... Well in some cases, webdriver's PageObject pattern where objects don't have no initialization, this would be really helpful

3
  • I also wonder why this feature doesn't exist, in 3.1 and possibly 3.2 there are no sings of this to be implemented.
    – lisak
    Jun 5, 2011 at 23:54
  • Is: supported.lang.codes a key-value-pair of the property file, or should it be a programatic calculates value of all supported languages?
    – Ralph
    Jun 6, 2011 at 10:24
  • It is just a key-value of property file. It is actually very convenient way of getting properties. I'd expect something like this from Spring regarding Localization...like @Localize("#{langCode['some.error.message]}")
    – lisak
    Jun 6, 2011 at 10:28

2 Answers 2

28

I believe you mixed two concepts:

  • property files
  • message resource bundles

Property files contains properties (locale independent). In Spring they can be loaded for example via util:properties and can be used in @Value annotations.

But message resource bundles (which are bases on files that look like property files) are language dependend. In Spring you can load them via org.springframework.context.support.ResourceBundleMessageSource. But not inject in a String via @Value. You can not inject them because the @Value injection is done once per bean, the @Value will be evaluated once (most at start time), and the calculated value will be injected. But this is not what you normally need when you use Message Resource Bundles. Because then you need to evaluate the value every time the variable is used, depending on the language of an user.


But you can build it easely by your own!

The only things you need is this class:

import java.util.Locale;    
import javax.annotation.Resource;    
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;

@Configurable
public class MSG {

    private String key;

    @Resource(name = "messageSource")
    private MessageSource messageSource;

    public MSG(String key) {
        super();
        this.key = key;        
    }

    public String value() {
        Locale locale = LocaleContextHolder.getLocale();                        
        return messageSource.getMessage(key, new Object[0], locale);
    }

    @Override
    public String toString() {
        return value();
    }
}

Then you can use it in this way:

@Service
public class Demo {

    @Value("demo.output.hallo")
    private MSG hallo;

    @Value("demo.output.world")
    private MSG world;

    public void demo(){
        System.out.println("demo: " + hello + " " + world);
    }    
}

To get it running, you need to enable <context:spring-configured /> to turn on AspectJ @Configurable support, and (That is importent) you need to instanciate the Ressouce Bundle Message Source in the same application context (for example in web apps you put the ReloadableResourceBundleMessageSource definition in most cases in the web app context, but this does not work in this case, because the MSG object is in the "normal" application context.

2
  • Ralph, I didn't mixed up anything, I'm using both for 3 years. I just said that it would be convenient for Unit testing if Spring had it. I did this as Edgar said, I'm using BeanPostProcessor to populate the annotated fields from ResourceBundle. I think it's a nicer solution than using @Value annotation for this case. But thanks a lot for showing this use case of @Value annotation, I didn't know I could do that.
    – lisak
    Jun 6, 2011 at 11:49
  • Ou I see, you were confused by "via @Value annotation" I didn't mean to use @Value annotations per se, I meant "via annotations"
    – lisak
    Jun 6, 2011 at 11:53
5

The point is that this is really useful only for Unit Testing. In real application, Locale is a runtime information that cannot be hardcoded in the annotation. Locale is decided based on Users locales in Runtime.

Btw you can easily implement this by yourself, something like :

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface Localize {

    String value();

}

And

public class CustomAnnotationBeanPostProcessor implements BeanPostProcessor {

    public Object postProcessAfterInitialization(Object bean, String beanName) {
        return bean;
    }

    public Object postProcessBeforeInitialization(Object bean, String beanName) {
        Class clazz = bean.getClass();
        do {
            for (Field field : clazz.getDeclaredFields()) {
                if (field.isAnnotationPresent(Localize.class)) {
                    // get message from ResourceBundle and populate the field with it
                }
            }
            clazz = clazz.getSuperclass();
        } while (clazz != null);
        return bean;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.