Announcement Announcement Module
Collapse
No announcement yet.
Accessing messages.properties in service layer Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Accessing messages.properties in service layer

    In my application I am sending out emails, for example, when a user asks for a password reset. I don't want to hardcode the text of the email in my manager (FYI I extracted a bunch of code out from my Roo-generated controllers to service-layer managers so I could add security).

    I know that the web context has access to a message source because Roo uses lots of <spring:message> tags, but I want the same message properties in my application context.

    I can't seem to set up the message source correctly:

    Here is what I declared in my applicationContext.xml:
    Code:
    <bean id="messageSource2" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    		<property name="basenames">
    			<list>
    				<value>WEB-INF/i18n/messages</value>
    			</list>
    		</property>
    	</bean>
    Then I autowire that into my manager:
    Code:
    	@Autowired
    	private MessageSource messageSource2;
    and then I try to use it to get a message:

    Code:
    messageSource2.getMessage("researcher.passwordReset", new String[] {temporaryPassword, applicationUrl}, null, Locale.getDefault());
    but I always get null back.

    My messages.properties file is in WEB-INF/i18n and it has this line in it:
    Code:
    researcher.passwordReset=Your password has been reset. Your new password is {0} \n\nPlease log in to {1} and change your password as soon as possible.
    What am I missing? Is there a better way to do this?

  • #2
    Have you taken a look at the new @Value annotation wich leverages the new expression support? http://static.springsource.org/sprin...new-feature-el

    Also, make sure that your component scan includes your service.

    -Stefan

    Comment


    • #3
      Hi Stefan,

      I know about @Value, but it only works with the properties files under META-INF/spring, not the ones that are in WEB-INF/i18n. I think that is because of this in my application context:
      Code:
      <context:property-placeholder location="classpath*:META-INF/spring/*.properties" />
      I can make it work with @Value by putting my messages in a .properties file under META-INF/spring, but then you don't get the nice locale/language switching that you would with a MessageSource.

      Thanks anyway,
      Audrey

      Comment


      • #4
        Look like I had the same problem, but when I removed the prefix classpath: infront of the bean declaration
        <bean id="messageSource" class="org.springframework.context.support.Resourc eBundleMessageSource">
        <property name="basenames" value="resource-bundle/messages" />
        </bean>

        I had it work.

        My *.properties files are in WEB-INF/classes/resource-bundle folder and

        in a service, I declared

        @Autowired
        private MessageSource messageSource;

        then I could call messageSource.getMessage() successfully.

        Hope can help

        Comment

        Working...
        X