Spring Tutorial 22 - Using MessageSource To Get Text From Property Files

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 ก.ค. 2024
  • In this tutorial, we'll use the MessageSource interface to retrieve messages out of property files.

ความคิดเห็น • 67

  • @jagansirigiri
    @jagansirigiri 11 ปีที่แล้ว +9

    Teaching is a sacred job and people like you make it even sacred.Thank you for your awesome tutorials .Keep up good work.

    • @parthsalat3915
      @parthsalat3915 2 ปีที่แล้ว +1

      Then why do I feel scared?

    • @truthbetold_25
      @truthbetold_25 13 วันที่ผ่านมา

      ​@@parthsalat3915 Because both your attempted humor and unwarranted comment is out of context.

  • @girishkumar518
    @girishkumar518 10 ปีที่แล้ว +5

    awesome explanation ..... thanks for the efforts you made for clean understanding of each concepts in spring

  • @GurnBograt1986
    @GurnBograt1986 5 ปีที่แล้ว +1

    This whole tutorial series is Awesome!

  • @parthsalat3915
    @parthsalat3915 2 ปีที่แล้ว +2

    It was I who called Koushik at 15:44
    _(You can hear his mobile vibrate)_

  • @write2sach
    @write2sach 8 ปีที่แล้ว +3

    Hi Kaushik, Thanks for such a great tutorial.

  • @rtech8779
    @rtech8779 4 ปีที่แล้ว +1

    this is awesome tutorial, just completed spring core now moving to AOP.

  • @sagar100ish
    @sagar100ish 7 ปีที่แล้ว

    This works perfectly:
    It's not mandatory to have list always.

  • @reloaderify
    @reloaderify 7 ปีที่แล้ว

    Thanks for your kind effort for teaching us!

  • @hedjerciyakoub328
    @hedjerciyakoub328 6 ปีที่แล้ว +1

    for every one have problem of print the default message the problems is :
    1- be sure from the name of your properties in the value of property "basenames"
    2- be sure from your file mymessages.properties it exisit in the same path of your xml file "under src"
    i hope you ll solve your problem soon

  • @TheUnixHead
    @TheUnixHead 11 ปีที่แล้ว +3

    Excellent tutorial :)
    P.S. Repeating some of the issues is a good convention :) It allow me to solidify gained knowledge.

  • @apt621
    @apt621 9 ปีที่แล้ว +1

    Java Brains I am getting null pointer exception when i tried to print the message in Circle class (Drawing Circle!) with same exact code in the video. Then, i removed @Autowired annotation and implemented MessageSourceAware in Circle class, implemented setMessageSource method to set this.messageSource to messageSource and it works like a charm. Just to let you know, I'm using spring 4.0.6 JARs in my eclipse project.
    Finally, Kudos to you for helping us learn Spring Basics! Job well done.

    • @anjanguharoy
      @anjanguharoy 9 ปีที่แล้ว

      apt621 and Java Brains , same case here.

    • @akvirus2
      @akvirus2 9 ปีที่แล้ว

      Anjan Guha Royoy: the correct way to do is_ MessageSource messageSource = new messageSource(){};

  • @SoeaOu
    @SoeaOu 12 ปีที่แล้ว

    But I like that part of this videos that he repeats what he does.
    English is not my first language so it helps alot to hear it again in a slightly different way ;)

  • @twitchmix9415
    @twitchmix9415 6 ปีที่แล้ว

    You are awesome!

  • @LeBadman
    @LeBadman 9 ปีที่แล้ว +2

    Koushik is amazing!

    • @jaroslavbeno
      @jaroslavbeno 8 ปีที่แล้ว +1

      +LeBadman yes he can teach in very easy way

  • @blackowl7009
    @blackowl7009 9 ปีที่แล้ว +4

    excellent.

  • @TheGuroguro12
    @TheGuroguro12 7 ปีที่แล้ว

    THANK YOU VERY MUCH

  • @mateuszalmannai5865
    @mateuszalmannai5865 12 ปีที่แล้ว

    Way to go Koushik!

  • @MandeepSingh-bv2zw
    @MandeepSingh-bv2zw 11 ปีที่แล้ว

    u might have figured out the problem by now, but for the benefit of anyone facing the same problem... the property 'name' must be 'basenames' when u r using a "list", as in this tutorial. But if you are using the value attribute inside the property-tag to define a filename, the 'name' of the property must be 'basename'. Hope it helps.

  • @damnyoufk
    @damnyoufk 8 ปีที่แล้ว +3

    cant inject message source to circle object, it shows default message , not picking up mymessagesproperties file message..help plz

  • @sandeepgabhale
    @sandeepgabhale 12 ปีที่แล้ว

    Nice Explanation!
    I've one query here, in case if we've more than 1 properties files, then can we can add that file names in the "value" tag of the same "list" tag where we've file name "mymessages" ?

  • @jinendrakumarjain9160
    @jinendrakumarjain9160 7 ปีที่แล้ว

    awesome learning here. thankx java brains. same code i have tried but its giving me null pointer exception. then in the center class i have implemented MessageSourceAware, its work fine i dont know the reason why??

  • @iKarmaCrafts
    @iKarmaCrafts 12 ปีที่แล้ว

    Mr. koushik, I get null pointer exception when I try to do the following in my draw() of Circle. The messageSource property is autowired. I am able to access the message when I do it from my main() using the context.
    System.out.println(this.messageSource.getMessage("greeting", null, "Default Greeting", null));

  • @mohdazharrazaqadri4638
    @mohdazharrazaqadri4638 5 ปีที่แล้ว

    Hello Everyone!
    Where can I find documentation for exploring other features of spring.

  • @MandeepSingh-bv2zw
    @MandeepSingh-bv2zw 11 ปีที่แล้ว

    Even I faced the same problem. I had set the 'bean id' to some custom name, just to see if it works. It didn't. So I changed it to "messageSource" and it still didn't work. All I had to do was to "Clean" the project.
    Are you sure your bean id = "messageSource" ? If its not, change it to "messageSource". I learnt the hard way that it needs to be exactly this and not anything else.
    My config changes were not picked up by eclipse, so I had to clean the project and then it just worked like charm.

  • @tejach1329
    @tejach1329 7 ปีที่แล้ว

    can we use ReloadableResourceBundleMessageSource instead of ResourcebundleMessageSource in messageSource bean?

  • @user-ui7cq5qs8q
    @user-ui7cq5qs8q ปีที่แล้ว

    Now everythng make sense!

  • @cueballdoe4779
    @cueballdoe4779 8 ปีที่แล้ว

    What is the difference between putting the autowired notation in the setter method and the variable itself?

  • @958ice47dcube
    @958ice47dcube 11 ปีที่แล้ว

    Is your (bean id="messageSource"...) spelt correctly? I noticed that I had mine misspelled, and it worked as soon as I fixed it.

  • @MartinMarchev
    @MartinMarchev 11 ปีที่แล้ว

    Coming from the Java SE world, I was thinking the same: why do I need Spring to handle translation when Java has already an API for this thing?

  • @batahi7
    @batahi7 11 ปีที่แล้ว

    I ve done everything like the tutorial, and I keep getting the default message

  • @lachuseenu1305
    @lachuseenu1305 6 ปีที่แล้ว

    We have @Autowired to messageSource. Then,Why do we need to have setters and getter methods for MessageSource here? any purpose? Can any one comment on this.

  • @tubeyou69ish
    @tubeyou69ish 12 ปีที่แล้ว

    I must say its a nice collection of videos that you have made very informative and a must see for Spring beginners but I must also say that you are repeating the same things over and over again like once when you have defined what you will do and after writing the code you go and repeat what you have done which for me was really really boring. But whatever, these tutorials are really nice and more than worth a look. Thanks for the tutorial....THUMBS up............

  • @kamalgupta3394
    @kamalgupta3394 10 ปีที่แล้ว +2

    @batahi7 I also got the same issue but after changing the location of mymessages file to project directory instead of package it got resolved

  • @mmnajar
    @mmnajar 11 ปีที่แล้ว

    mine didn't work. It threw null pointer exception...!

  • @rajarsh8345
    @rajarsh8345 9 ปีที่แล้ว

    Do check that, you use the name of propertie tag as "basenames" itslef, else it's throwing BeanCreatingException. Can somebody please explain me why it does so?

    • @kenji2787
      @kenji2787 9 ปีที่แล้ว

      RaJ Arsh if you remember the List tutorial, the is asking for the property for the class (ResourceBundleMessageSource) and this class have a variable call private List basenames

  • @reddred4200
    @reddred4200 9 ปีที่แล้ว

    S.o.p("Circle Point is: (" + getCenter.getX() + " , " + getCenter.getY() + ")");
    was replaced by
    S.o.p(this.messageSource.getMessage("drawing.point", new Object {center.getX() , center.getY()}, "Default Point Message" null));
    How come the messageSource.getMessage can access the center object directly to call its getX & getY methods, but the Circle class had to first use the getter method to access the center object's methods?
    Also, if messageSource is a member variable of the Circle class, why use this.messageSource.getMessage instead setMessageSource.getMessage or of just messageSource.getMessage ?
    And Last, what is the purpose of the getter & setter methods for the messageSource variable ?
    Enjoying your tutorials, they are very well done. Thank you for educating us.

  • @floggerN
    @floggerN 9 ปีที่แล้ว

    it could be a weird question but I am still a beginner with Spring:
    what is the sense of using the parameters array in the getMessage method?
    In particular, looking at this.messageSource.getMessage("circle.points", new Object[] {this.getCenter(), this.getRadius()}, "Default greeting", null) we are passing parameters that we already own! Why passing parameters to the property file (mymessage.properties) and get them backwards again? Is there a more useful case of use? Thanks!

    • @pingchengruan9726
      @pingchengruan9726 9 ปีที่แล้ว

      I think it is intended to demo the parameter passing function~

    • @bindubindu608
      @bindubindu608 9 ปีที่แล้ว +2

      Nicola Pianta Imagine the application is meant for using different locale. Then its good to display the messages from property files specific for that locale, say messages_EN.properties, messages_FR.properties etc. In the example its display a general message(based on locale) + some dynamic content like getX(). If you want you can mention certain predefined values for Point x and y in the properties files and based on the requirement condition you can retrieve those set of values from the properties file and set them to X and Y. Hope u understand. I am not so good at explaining.

  • @rehanshi_ammu
    @rehanshi_ammu 8 ปีที่แล้ว

    in properties file u are hard coding x and y values to '0' and '1' but output it's printed as 20 , 0 ????

    • @pratheepece
      @pratheepece 7 ปีที่แล้ว

      0 and 1 represents the parameters position, but not the values. If you look at the SOP, a new object is passed with X,Y coordinates, so Spring substitutes the values of X,Y to the positions specified in the properties file.

  • @274617143
    @274617143 8 ปีที่แล้ว

    Hi Kaushik,
    Thanks for your tutorials, However I am getting null pointer exception when injecting MessageSource in my class using @Autowired annotation. Could you please help?

    • @jaroslavbeno
      @jaroslavbeno 8 ปีที่แล้ว

      +sanjay choudhary do you have a proper name for property file in spring.xml ?

    • @274617143
      @274617143 8 ปีที่แล้ว

      +EasyTutorials , I forgot to add tag in my spring.xml , now issue has been resolved. Thanks.

  • @dlrstgytort5541
    @dlrstgytort5541 7 ปีที่แล้ว

    Initialization of messageSource bean failed
    Using Spring 4.3.5, everything worked as per tutorial until this
    would like help
    WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'messsageSource' defined in class path resource [spring.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.util.ArrayList' to required type 'java.lang.String' for property 'basename'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.util.ArrayList' to required type 'java.lang.String' for property 'basename': no matching editors or conversion strategy found
    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'messsageSource' defined in class path resource [spring.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.util.ArrayList' to required type 'java.lang.String' for property 'basename'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.util.ArrayList' to required type 'java.lang.String' for property 'basename': no matching editors or conversion strategy found
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:563)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
    at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
    at org.shubham.javabrains.DrawingApp.main(DrawingApp.java:11)
    Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.util.ArrayList' to required type 'java.lang.String' for property 'basename'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.util.ArrayList' to required type 'java.lang.String' for property 'basename': no matching editors or conversion strategy found
    at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:591)
    at org.springframework.beans.AbstractNestablePropertyAccessor.convertForProperty(AbstractNestablePropertyAccessor.java:603)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:216)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1538)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1497)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1237)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:552)
    ... 11 more
    Caused by: java.lang.IllegalStateException: Cannot convert value of type 'java.util.ArrayList' to required type 'java.lang.String' for property 'basename': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:306)
    at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:576)
    ... 17 more

  • @MrMagnat86
    @MrMagnat86 9 ปีที่แล้ว +3

    Not work.
    Always return default message.

    • @2009lk
      @2009lk 9 ปีที่แล้ว

      +Pucvirok Make sure the name in the value tag of the list tag of in spring.xml matches your properties file name( the file where you define de messages)

    • @ART000747
      @ART000747 8 ปีที่แล้ว +3

      +Pucvirok Try putting the messages.properties in /src on the same level as spring.xml. Helped in my case...

    • @hedjerciyakoub328
      @hedjerciyakoub328 6 ปีที่แล้ว

      thank you
      you solve my problem ;)

  • @abdulsattarshaikh9758
    @abdulsattarshaikh9758 6 ปีที่แล้ว +4

    Dear Sir, Your explanation is simply outstanding,
    encouraging
    but u have only one tone...
    It could be more better if u could add some toning differentiation during explanation..
    Overall I like your methodology compare to other Programming lectures ...

  • @parthsalat3915
    @parthsalat3915 2 ปีที่แล้ว +2

    Good tutorial.
    BUT, PLEASE PUT LINKS TO THE RESOURCES THAT YOU USED TO LEARN SPRING, SO THAT THOSE WHO WANT TO DIVE DEEPER CAN DO SO.

  • @manojbabu999
    @manojbabu999 8 ปีที่แล้ว +2

    I am getting default message and following warning
    org.springframework.context.support.ResourceBundleMessageSource getResourceBundle
    WARNING: ResourceBundle [messages] not found for MessageSource: Can't find bundle for base name messages, locale en_US
    Please solve my doubt

    • @Glacious83
      @Glacious83 8 ปีที่แล้ว

      are you using mymessages as value or mymessages.properties ?

    • @SankalpAnand
      @SankalpAnand 6 ปีที่แล้ว

      The value inside tag should be the name of your properties file.

  • @xKrzysztof1985
    @xKrzysztof1985 11 ปีที่แล้ว

    I have problem, I did all yours video tutorials, and sometimes you make a mess. But this is not the point, this tutorials are great.
    But in this chapter I have following error:
    nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.util.ArrayList' to required type 'java.lang.String' for property 'basename'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.util.ArrayList] to required type

    • @shrtchndr
      @shrtchndr 5 ปีที่แล้ว

      Make sure you are using this:

      mymessages

  • @mehulkatpara1421
    @mehulkatpara1421 10 ปีที่แล้ว

    import SpringTutorial;
    @kaushik
    public class SpringTutorial implements JavaTutorial {
    public SpringFramework framework;
    public SpringTutorial (SpringFramework framework) {
    this.framework = framework;
    }
    public SpringFramework getFramework () {
    return framework;
    }
    @Override
    public void writeMessage () {
    System.out.Println("Spring Framework is done! Now upload the video for Spring MVC!");
    }
    }