Spring Tutorial 05 - ApplicationContext and Property Initialization

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 ส.ค. 2024
  • We'll first replace the BeanFactory with the more powerful ApplicationContext. We'll then make Spring initialize a property of an object by specifying the value in the configuration XML.

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

  • @ehsanr720
    @ehsanr720 4 ปีที่แล้ว +32

    watching in 2019 :) till this year this guy is the best in the field

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

      same here bro.

    • @ThangNguyen-ko5ze
      @ThangNguyen-ko5ze 4 ปีที่แล้ว +3

      All the other tutorials are a pain in the ass

    • @satyasahoo9573
      @satyasahoo9573 4 ปีที่แล้ว +7

      True in 2020 even

    • @venkychowdary
      @venkychowdary 3 ปีที่แล้ว +4

      @@satyasahoo9573 in 2021 as well

    • @Armaan944
      @Armaan944 3 ปีที่แล้ว

      isnt this series outdated ?? shall i see some newly uploaded spring series?? or will this be good enough?

  • @0x28
    @0x28 12 ปีที่แล้ว

    This man is one of the best teachers in the world.

  • @funkybuddha1598
    @funkybuddha1598 4 ปีที่แล้ว +3

    watching in 2020.. best spring framework intro course!! Thank you!!

  • @8o8inSquares
    @8o8inSquares 8 ปีที่แล้ว +12

    Oh now spring makes a lot of sense for me!

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

    Hello Andrej, As Koushik mentioned that we can do all these locally of course. The reason we are using spring is to able to inject these from the outside and change the properties without having to recompile and redeploying the application again and for more control and flexibility.
    hope that helps.

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

    Your tutorials are a life saver! Amazing videos.

  • @MukulTripathi
    @MukulTripathi 13 ปีที่แล้ว +7

    Awesome ... I'm awake at 3:30 am watching this ...

  • @bittooaggarwal5776
    @bittooaggarwal5776 3 ปีที่แล้ว +1

    basically here we can set template for multiple objects with different values preset for member variables otherwise in our main class we need to make multiple and set properties for them one by one.
    eg:

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

    Hello everyone, if you are facing the errors like spring.xml couldn't be found or triangle.java couldn't be found, try this-
    PS: I'm using Spring 4.2.4, Maven with JDK 8 and Eclipse Oxygen 3A
    1. Move Spring.xml to the place where your triangle.java and drawingapp.java are.
    2. In DrawingApp.java write-
    ApplicationContext context = new ClassPathXmlApplicationContext("file:src/main/java/Spring.xml");
    3. In Spring.xml write-

    That's it, this worked smoothly for me.

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

      Been stuck on a "file not found" exception since the last video and tried everything--moving my xml file, updating class path, etc. Thank you for posting this fix! Is it good practice for the bean file to be in the same folder? My issue was with the xml file. I'm also using Maven and JDK 8.

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

    Excellent tutorial ............that gave me a lot of concept on spring....Thank you

  • @7Shol
    @7Shol 4 ปีที่แล้ว +5

    Does the Property form the xml file, use the Setter?
    Edit: Yes according to the next playlist's video 👍

  • @lingasodanapalli615
    @lingasodanapalli615 3 ปีที่แล้ว +1

    Can someone explain me this please? If the xml file itself assigns the value of "Equilateral" to the member variable "type" of that object which is being returned, then why are we again writing the setType(String type) method?

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

    How does preconfiguring Beans via xml differ from just assigning these values in the constructor of the class? i have serious issues understanding the advantage of this.

  • @walidbousseta2650
    @walidbousseta2650 4 ปีที่แล้ว

    the way you explain think are great, in additional to your great tuto you should share some resource with geeks for exampe the XML file you just create or at laest the code lines in it, and thank you for this hard work you are doing in this tuto.

  • @Java.Brains
    @Java.Brains  13 ปีที่แล้ว

    @shoibmd Any package name should do. Regarding the Spring XML error, check if you have the hierarchy right. First you need to have a beans element and under that, individual bean elements.

  • @AniketSomwanshi-ll7mz
    @AniketSomwanshi-ll7mz 3 ปีที่แล้ว +2

    Considering this is 10 years old, is it okay to learn Spring 3 or are there major changes in Spring 5?

    • @Anonymous-fy2el
      @Anonymous-fy2el 2 ปีที่แล้ว

      Core Spring is still the same with some newer features.

  • @shimondoodkin
    @shimondoodkin 7 ปีที่แล้ว +4

    context usually should be closed
    to do it need to be casted to
    ConfigurableApplicationContext
    that has that close method.
    ApplicationContext context=new ClassPathXmlApplicationContext("file:spring.xml");
    Triangle triangle=(Triangle)context.getBean("triangle");
    triangle.draw();
    ((ConfigurableApplicationContext)context).close();

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

    Hi ,with ApplicationContext object reference i didnt get the value of pre set member variable of the bean which we set in spring.xml . ...i got the value with BeanFactory's object reference in the same case where in this tutorial it suggests to use ApplicationContext object reference ...can you tell me why

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

    wow excellent work koushik

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

    I have a question , why can't just make a simple class Triangle and we can extend this class whenever we need. What's the difference ?

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

    why we are moving "spring.xml" into src folder. ... is that FileSystemResource searching into the whole application ... and ClassPathXmlApplicationContext is searching for "spring.xml" only in the src folder ?

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

    hi Koushik,
    Can't i set the 'type' variable mentioned in this tutorial without using getter and setter methods like this ....
    public class triangle {
    public String type;

    public void draw(){
    System.out.println(type + " Triangle drawn");
    }
    }
    In doing so i am getting an error saying cannot set the value for type...

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

      The variable type you are trying to call is a local variable. To call the type variable you defined before, use "this", so will be like this: this.type + "Triangle drawn"

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

    you are just EXCELLENT . thank you very much

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

    Is the ApplicationContext using the setter method in assigning the value? or is it somehow directly assigning the value to the private attribute?

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

      Do you know reflection of Java? reflection can assign value to private variable.

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

    Please increase your video resolution. 720p or even 480p is good enough. It's clearly visible.

  • @MukulTripathi
    @MukulTripathi 13 ปีที่แล้ว

    Waiting for more tutorials in the series!

  • @AKASHYADAV-gq8sc
    @AKASHYADAV-gq8sc 6 ปีที่แล้ว

    A very basic question: Why use getType()? why not type variable directly?what difference does it makes

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

    Still don't see the point of assigning values to the Triangle members in the XML file. Just do it in the object itself and when the bean factory creates the object for you the values will be there with it

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

    For file not found problem:
    1) if spring.xml is at the root of the project, type: ClassPathXmlApplicationContext("file:spring.xml");
    because file: prefix points to file system resources, not the classpath
    2) if spring.xml is in src/main/resources/, type: ClassPathXmlApplicationContext("spring.xml");
    without prefix. It will work without specifiying the path.

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

      Addendum: 2) if spring.xml is in src/main/resources/, or in src

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

    your tutorials are AWESOME!
    I went through the JSP and servlet ones in two days. it was enjoyable studying with them.
    but I have a question, wouldn't it be the same if in the triangle class we just had a constructor like this:
    public Triangle(){
    this.type="Equilateral";
    }
    the result would be the same... I mean, everything that we can initialize in xml, we can also do in the constructor. I am sure that there is a reason for this, but I dont understand it yet

  • @Suraj-pf6ic
    @Suraj-pf6ic 7 ปีที่แล้ว +1

    It did not work for me. I did same thing as explained here:
    ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
    Triangle triangle = (Triangle) context.getBean("triangle");
    triangle.draw();
    and my path I moved to source path. Also I changed the path as new ClassPathXmlApplicationContext("file:src/spring.xml");
    nothing worked.

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

      just add this line
      ((AbstractApplicationContext) context).close();
      after triangle.draw();
      Hope this will work..

    • @Suraj-pf6ic
      @Suraj-pf6ic 6 ปีที่แล้ว

      Thanks for the reply. It finally worked for me. What I did was scraped the whole project and created a new one, it somehow worked.

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

    EXCELENT tutorial, on pair with the newboston tutorials :)
    best on YT

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

    good one and liked the word 'big brother' :) 0,26

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

    Since XmlBeanFactory is deprecated, what should we use instead of that? Like, what replaced it?

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

      +4surprisingly new ClassPathXmlApplicationContext("Spring.xml");

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

      One of this:
      BeanFactory context = new ClassPathXmlApplicationContext("spring.xml");
      //or
      ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

    • @abhishekmedha1098
      @abhishekmedha1098 7 ปีที่แล้ว +1

      DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
      XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
      reader.loadBeanDefinitions(new FileSystemResource("spring.xml"));
      Triangle triangle=(Triangle)xbf.getBean("triangle");
      triangle.draw();

  • @HH_Linkmusic
    @HH_Linkmusic 4 ปีที่แล้ว

    couldn't see what's the benefit of not using constructor directly

  • @HeavilyWise
    @HeavilyWise 4 ปีที่แล้ว

    Sir what happens if we don't create a package then what we will configure in class in spring. Xml

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

    if draw method has arguments means then how can we call it on drawing app through spring

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

    U told tht trianle is simple object so it doesnt matter tht much to use spring.xml for this but can u give us an example of real time complex object for which using spring.xml is really beneficial?

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

    Excellent tutorial thanks

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

    If constructor initialization fails with illegalargumentexception (like that) check your spring version and JRE version.... I used spring 3.0.5 and JRE 1.8 and got this error.... I changed JRE to 1.6 and it worked fine for me.

  • @sebon11
    @sebon11 ปีที่แล้ว

    Nice dude

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

    Dude its showing error that jar is no source attachment? what i have to do?

  • @05deeps
    @05deeps 12 ปีที่แล้ว

    you are using triangle object where did polymorphism go?

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

    can we use any other access modifier for triangle member ? ,

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

    Just make sure your spring.xml is closed when you are running the application.

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

    nice tutorial

  • @0x28
    @0x28 12 ปีที่แล้ว

    Not always true about being much more competent.. the problem is that they don't share the information and that they are not nice about it.. they are the 'google-it' types. Unfortunately that's how higher education is. The professor will not tell you how things are.. you have to decide what they are and if what you think is the answer.. is a valid one. I followed all the tutorials of koushik and thenewboston.. but professors always ask for more difficult concepts than that.. much harder.

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

    Thanks for the tutorial. I just started watching this and it is really great. I do have a question though. If I want two instances of the Triangle class with one as type = equilateral and one as type = isoceles, how do I do? The spring xml has only one definition for the bean "triangle". Please help me understand.

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

      I know its been too late! you can set two bean configuration in the spring.xml file. One with the equilateral property and the other with the isosceles. Similarly, you can set two different IDs to extract the bean. Hope this helps!.

    • @ArunKumar-ut4rq
      @ArunKumar-ut4rq 3 ปีที่แล้ว

      ​@@bumblebee7370 of course didn't him but helped me dude.

  • @12345678330ful
    @12345678330ful 9 ปีที่แล้ว

    It is giving me error that getBean() method not present in ApplicationContext please help

    • @k-dari-23
      @k-dari-23 8 ปีที่แล้ว

      +Aniket Maryapgol Maybe the xml parameter that you have insert does not see the original because you typed wrong the name , or is not in default directory (src/main/resources)... If the xml is not in the default place you must write the path...

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

    In main() if I access triangle.getType() it does not show any result but if I try; triangle.draw() then it shows result from getType().....does anyone knows why direct calling of getType() doesn't give result on console?

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

      +Sharmistha Sakar
      Assuming you have copied the code from the video the same way i did:
      Calling triangle.getType() from main() does not output anything on the console because getType() actually returns a String value and does not call System.out.println() whereas draw() does.

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

      Thank you on answer, I was wondering why that dont work, now I know ;) :)

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

    Got a problem here - maven can't find my spring.xml file. I've tried to put in everywhere! Where it should be placed?

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

      +Anatolii Stepaniuk I also had this problem. Try creating the XML file inside src. You will notice that it will also be copied inside the bin folder. I hope it works.

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

      ApplicationContext cpontext = new ClassPathXmlApplicationContext("file:src/main/resources/spring.xml");

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

      create your xml file in your source folder

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

      put it under src/main/java folder

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

    code is not visible well, plz try o zoom in

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

    Hey..Your tutorial is great. Very well explained.
    The variable 'type' that you defined has a private access in your code but if I define it as private my code shows an Error
    Error: A JNI error has occurred, please check your installation and try again
    Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/c
    ore/io/Resource
    If I declare it to be Public.. then it works fine..
    Can you plz explain ??

  • @psykoblast
    @psykoblast 10 ปีที่แล้ว +1

    you are my Lord ! thank you ! plz increaze rezolution ;)

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

    great tutorial although you can sometimes use ctrl+space or ctrl+shift+o :)

  • @AjayKumar-fd9mv
    @AjayKumar-fd9mv 3 ปีที่แล้ว

    Great👍

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

    siraaaa

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

    those getting such error "Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [****.xml]; "
    can follow this it may help :
    he just added the package in the directory to be : "org/kou***/javabrains/spring.xml" instead of "spring.xml"
    stackoverflow.com/questions/19651080/exception-in-thread-main-org-springframework-beans-factory-beandefinitionstore

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

    how to initialize properties

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

      when you run main method, this line of code: ApplicationContext context = new Class,PathXMLApplicationContext("spring.xml"), then the spring container will do the following things:
      1,scan the class path to find a file named "spring.xml", spring will find it.
      2,spring will parse the xml file in specific rule like beans, bean, property
      3,use java reflection to create a bean, and set id as "triangle"
      4,use setMethod to set class variable type
      5,store the bean and waiting for use.

  • @llitfkitfk
    @llitfkitfk 4 ปีที่แล้ว

    Watching in 2020

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

    what OS is that ?

    • @dusanjovanov
      @dusanjovanov 7 ปีที่แล้ว +1

      shreeranga kv its linux fedora i think

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

      Linux, maybe Ubuntu

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

    Awesome

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

    Спасибо!

  • @botrate412
    @botrate412 2 ปีที่แล้ว

    getting null for getType()

    • @botrate412
      @botrate412 2 ปีที่แล้ว

      my mistake accidently made changes in spring.xml which is outside the src ..hence I was getting null since there was no property tag in spring.xml which is inside src

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

    subtitles are disastrous mate. what the hell are they ??! o_0

  • @Rioisthep134
    @Rioisthep134 หลายเดือนก่อน

    Wow😂