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

  • Accessing 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:
    <bean id="messageSource2" class="">
    		<property name="basenames">
    Then I autowire that into my manager:
    	private MessageSource messageSource2;
    and then I try to use it to get a message:

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

    My file is in WEB-INF/i18n and it has this line in it:
    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?

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



    • #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:
      <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,


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

        I had it work.

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

        in a service, I declared

        private MessageSource messageSource;

        then I could call messageSource.getMessage() successfully.

        Hope can help