Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MessageSource always returns the default message

Tags:

java

spring

I'm trying to inject text message using MessageSource in Spring. The program I wrote compiles fine, but it always printing out the default message instead of fetching the right values from .properties file. I have no clues at all.

Here's the Circle class that tries to inject the text:

 @Component
    public class Circle implements Shape {

        @Resource
        private Point center;
        @Autowired
        private MessageSource messageSource;

        public MessageSource getMessageSource() {
            return messageSource;
        }

        public void setMessageSource(MessageSource messageSource) {
            this.messageSource = messageSource;
        }

        public Point getCenter() {
            return center;
        }

        public void setCenter(Point center) {
            this.center = center;
        }

        @Override
        public void draw() {
            System.out.println(this.messageSource.getMessage("greeting", null, "Default Greeting", null));
            System.out.println(this.messageSource.getMessage("draw.circle", new Object[] {center.getX(), center.getY()}, "Required Drawing", null));

        }

And here's part of the bean configuration file:

...
    <bean id="center" class="java.awt.Point">
        <constructor-arg value="0" />
        <constructor-arg value="-20" />
    </bean>

    <bean id="messageSourse" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename">
                <value>messages</value>
        </property>
    </bean>

    <context:component-scan base-package="com.myproject.spring"/>

    <context:annotation-config/>

And I store the greeting and draw.circle in messages.properties file and wrote a main class. But the outputs are always:

Default Greeting  
Required Drawing

Does anybody know what can possibly go wrong in my case?

Update Thanks for the advice. I was debugging and seems like something went wrong in here:

public String getMessage(String code, Object[] args, String defaultMessage, Locale locale) {
        if (this.parentMessageSource != null) {
            return this.parentMessageSource.getMessage(code, args, defaultMessage, locale);
        }
        else {
            return renderDefaultMessage(defaultMessage, args, locale);
        }
    }

this.parentMessageSource is null so it decided to render default message. But I still don't quite understand what is parentMesssageSource and how it can be (not) null?

like image 549
Punky Avatar asked Nov 08 '12 03:11

Punky


3 Answers

You have to use id="messageSource" instead id="messageSourse". For all that use Java configurations, you have to have "messageSource()" as a method name

    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource resourceBundleMessageSource =
                new ResourceBundleMessageSource();
        resourceBundleMessageSource.setBasename(I18_PATH_TEMPLATE + "email_welcome");
        resourceBundleMessageSource.setDefaultEncoding("UTF-8");
        return resourceBundleMessageSource;
    }
like image 52
Alexis Gamarra Avatar answered Nov 08 '22 03:11

Alexis Gamarra


Probably, the messageSource is not able to pick your messages file. ResourceBundleMessageSource has overriden toString() method to display the messageResource configuration. Try using it once. You can also inspect messageSource object just after it has been injected with the help of a debugging tool, to check whether it has loaded all the properties successfully or not.

like image 36
Sumit Desai Avatar answered Nov 08 '22 04:11

Sumit Desai


You have mis-spelled the bean id

Instead of 'messageSource', you have written 'messageSourse'.

You must have an id of the Message Resource bean, and it should always be 'messageSource'.

like image 1
Dharmender Rawat Avatar answered Nov 08 '22 02:11

Dharmender Rawat