开发者

Get message in Java code ( Spring 3 )

开发者 https://www.devze.com 2023-03-08 05:06 出处:网络
<bean id=\"messageSource\" class=\"org.springframework.context.support.ResourceBundleMessageSource\"
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"
        p:basename="messages" />

bean and obviously in my velocity files I can use #springMessage() to get needed message. But what if I want 开发者_高级运维to get that message in my *.java controller? Is there some annotation that I can use like ?

@Annotation('message')
private String message;

Or I need to do it in different way?

Thanks


As long as you don't need any internationalization, then you can use the messages.properties file with a PropertyPlaceHolderConfigurer (see the docs), along with the @Value annotation

In XML:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="locations" value="classpath:messages.properties"/>
</bean>

And in Java:

@Value('message')
private String message;

You'll also need <context:annotation-config/> to make this work (see docs)

0

精彩评论

暂无评论...
验证码 换一张
取 消