Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I believe you mixed two concepts:</p> <ul> <li>property files</li> <li>message resource bundles</li> </ul> <hr> <p>Property files contains properties (locale independent). In Spring they can be loaded for example via <code>util:properties</code> and can be used in <code>@Value</code> annotations.</p> <p>But message resource bundles (which are bases on files that look like property files) are language dependend. In Spring you can load them via <code>org.springframework.context.support.ResourceBundleMessageSource</code>. But not inject in a String via <code>@Value</code>. You can not inject them because the <code>@Value</code> injection is done once per bean, the <code>@Value</code> 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.</p> <hr> <p>But you can build it easely by your own!</p> <p>The only things you need is this class:</p> <pre><code>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(); } } </code></pre> <p>Then you can use it in this way:</p> <pre><code>@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); } } </code></pre> <p>To get it running, you need to enable <code>&lt;context:spring-configured /&gt;</code> 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 <code>ReloadableResourceBundleMessageSource</code> 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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload