Spring Tutorial 06 - Using Constructor Injection

แชร์
ฝัง
  • เผยแพร่เมื่อ 18 มิ.ย. 2011
  • We'll now use constructors to set the member variable values for our Spring beans. We'll also look at some techniques to resolve conflicts in the case of overloaded constructors.

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

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

    Your tutorials are brilliant even after 12 years. Many thanks to you Koushik!

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

    This is legit the most comprehensive tutorial on Sping on the internet. Thanks a lot.

  • @rascalkd007
    @rascalkd007 7 ปีที่แล้ว +11

    Never seen a beautiful explanation like this for Dependency injection ever!!!! You're rocking Koushik..

  • @user-ke9xj2mh4f
    @user-ke9xj2mh4f 8 ปีที่แล้ว +48

    Thank you!!! Ive been a java programmer for 4 years, and now that i am unemployed, this is the only time i can study Spring. When i was developing String way back 2 years ago, i dont even know how Spring works. I just had the existing code as "template" to follow; no idea why they exist like that! Thank you so much!!! :) I need these knowledge on my job interview. I always get embarassed not being able to answer Spring qqs, eventhough i got Spring as one of my framework experience. And yeah, im so embarassing.

    • @sumitbhattacharya1720
      @sumitbhattacharya1720 6 ปีที่แล้ว +7

      welcome to developer community sometimes you inherit code, and this happens to many software engineers.

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

      All the best buddy

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

      I hope you got that job...

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

      Did you get that job?

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

      @@worldwide6626 +1

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

    This is highly concise , crisp and to the point , I would personally want to keep learning more from you, please keep making such tutorials , I would look forward and so would lot of my colleagues.GREAT Coachin!!

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

    After a decade of this video is made, it still well helped me understanding the basic of Spring, thank you

  • @seemantkaul
    @seemantkaul 10 ปีที่แล้ว +28

    123 thousand views and just 384 likes....people are lazy. You deserve more likes sir. Excellent tutorials. Thank you.

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

      there should be giveaway.. if this video gets 1000 likes, will give away bag of coffee beans.

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

    Koushik, I must say, your explanation is very good and I love the way you unfold things slowly and with the right context.

  • @tuelimishane
    @tuelimishane 7 ปีที่แล้ว +26

    With the newer version of Spring, you can either use type="int" or name="height".

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

    Well explained! Questions come up to me just been solved later in the video! Really nice work! Appreciate it!

  • @oggiemc83
    @oggiemc83 11 ปีที่แล้ว +1

    excellent stuff..you are a brilliant teacher, why oh why cant lecturers be more like this..

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

    Awesome job man!! Best Spring tutorial online!!

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

    3.15 : Sprint 4.1.6 has now included name as I can see. Used it and works well.

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

    sir... superb tutorials!!! love to work on springs now!!

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

    Koushks gr8 job man. The way u explain is just like the way Sachin hit cover drive :) a treat to watch.
    Thanks man.

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

    Ur way of teaching is really good , Thank you very much for making such videos:)

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

    We salute you. Great tutorial !

  • @srikanthg94
    @srikanthg94 11 ปีที่แล้ว +1

    Dude, I became ur fan...!! Keep going and educating us...!!

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

    Thanks Koushik, the tutorials are very good

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

    you are a greater teacher
    thank you!

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

    Thanks for the video. Another awesome video!

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

    Thank-you for sharing your knowledge.

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

    Excellent Explanation!

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

    Excellent tutorial.

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

    Excellent tutorials..

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

    wonderful tutorial,...really helpful

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

    Dhanyavad

  • @SwapnilJadhav-jk1nl
    @SwapnilJadhav-jk1nl 5 ปีที่แล้ว +1

    excellent!!!

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

    excellent tutorial.....thanks for sharing

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

    thanks a lot
    ,you explain it so well .

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

    awesome work!!

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

    awesome way of teaching
    thank you so much

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

    Kaushik .. You the Best !!!

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

    Great tuts :) Thank you !

  • @rahulsoni-lx5rb
    @rahulsoni-lx5rb 3 ปีที่แล้ว

    Awsm!🙌

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

    hi , if we are setting values in the xml file are we not making the whole thing hard coded in nature ???

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

    Very useful. Thanks!!!

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

    Thanks for the session! However, I have few doubts...
    1) What if there are multiple variables having similar data-types? In that case, how the xml configuration needs to be set up? Because, then the "type" attribute value of the tag will be confused on which variable to refer.
    2) When the Triangle class has multiple constructors (with different number of arguments) available, then how this xml configuration file will provide the way of creating such different type objects? I'm asking this because, I can see that the constructor-configuration is referring to only one type of constructor at a time.
    Thanks in advance!

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

    And that is you can change the program without the need to re-compile it. Wow, that's pretty nice :)

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

    very nice and descriptive in short time.

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

    You are just awsome , Thanks a lot

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

    great tutorial. Thanks.

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

    Awesome job

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

    I love you Man..you are the best....God bless

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

    Great tutorial

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

    You will never need that, unless you are doing something wrong. However, you will need different configurations, maybe even generated by maven or something. Spring allows property files where you can specify it.

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

    great tutorial

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

    thanks for these!

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

    It should have been noted that expecting Spring (or whatever framework) to automatically convert parameters is BAD PRACTICE, as it will lead to problems if, at a future time, someone adds a Triangle(String, String) constructor. Having your application fail just because you add a constructor that nobody calls is an unexpected result, and you should aim to follow the principle of least astonishment.

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

    thank you very much.

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

    great stuff
    thanks!

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

    Very useful.

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

    Sir if i have two construtor one having ex(string b, int a) and a second construtor ex(int s , string d) how to write it in construtor arg parameter to pass value to both construtor simultaneously.

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

    amazing!! thanks..

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

    Informative!

  • @ItachiUchiha-fo9zg
    @ItachiUchiha-fo9zg 2 ปีที่แล้ว +1

    what if i need to pass variables into spring.xml? i.e., i want to set constructors with dynamic values not static

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

    How do i assign an object to a constructor in the tag???

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

    index in bean define according to the order of the member variable declaration in class or randomly we can assign ??

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

    Is this him? :) The best teacher in the world? I see some comments they are calling him like that.

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

    thank you so much i can easy learn this ....:))

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

    Very good

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

    sir it can also run without getter() method that you have created for variable height.

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

    Thank's a lot

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

    @Java Brains what if you have "width" property too along with height. and you want to run the constructor which initializes only width property. how would you do that through XML?

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

    It gives me an illegal argument exception at
    ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
    when I use constructor injection. It isn't letting me make constructor

  • @1988neogeo
    @1988neogeo 11 ปีที่แล้ว

    Thx!

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

    Is it possible to inject dynamic values in the XML ? Much more useful coz we might need to pass different values different time

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

    Thnx :)

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

    What if I want to pass Null to a String argument?

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

    Which is better constructor injection or using setter ???

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

    The reason for this is that when you have a XML file opened in Eclipse and you click the Run button, it automatically creates or uses the last XSLT run configuration. That's why :) Read more about run configurations in Eclipse and you will understand why this happens. Regardless of the document you have opened in Eclipse, you can run the correct Run configuration by clicking on the arrow button next to the Run button and then selecting the desired Run config - in our case, this is DrawingApp :)

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

    so there's only 1 instance object of the Triangle at the same time?

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

    Can't we set the property values by using property and values command in xml file without using constructor method.. because I don't think this is exactly dependency injection using constructor.. nothing is dependent here.. only value initialization done by const

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

    Hi
    I did not felt the need of specifying type of data type in spring.xml. I created three constructors:
    constructor1(int)
    constructor2(String)
    constructor3(int, String)
    In spring.xml, I created three beans, one passing integer value, second passing string value and the last one passing both integer and string values.
    There was no problem for the compiler, and it gave the correct output.
    I have attached the code below. Please help by telling how specifying the type in spring.xml file is usefull.
    DrawingApp.java
    package javabrains;
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFactory;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.core.io.FileSystemResource;
    public class DrawingApp {
    public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
    Square s = (Square) context.getBean("square");
    s.draw();
    s.drawing();
    s.draws();
    }
    }
    spring,xml





    Square.java
    package javabrains;
    public class Square {
    public String type;
    public int area;
    public int getArea() {
    return area;
    }
    public Square(String type){
    this.type = type;
    }
    public Square(String type, int area){
    this.type=type;
    this.area=area;
    }
    public Square(int area){
    this.area=area;
    }
    public String getType() {
    return type;
    }
    public void drawing(){
    System.out.println("Draw "+getType()+" Square and area = "+getArea()+" units.");
    }
    public void draw(){
    System.out.println("Draw "+getType()+" square");
    }
    public void draws(){
    System.out.println("Area of square is : "+getArea());
    }
    }
    Output:
    Draw Regular square
    Draw Regular Square and area = 100 units.
    Area of square is : 100

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

    How do we know what value is set when we have two constructors but with change in order of arguments. For example:-
    public Triangle(String type,int height)
    {
    this.type=type;
    this.height=height;
    }
    public Triangle(int height,String type)
    {
    this.height=height;
    this.type=type;
    }
    How do you control this?

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

      use name property or type property

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

    nice

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

    HIBERNATE and Spring Bridge how cld i? is it possibl?

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

    how to inject values in to default constructor?

  • @codegigs
    @codegigs 11 ปีที่แล้ว +1

    ok i got that later in the tutorial

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

    Hi kowshik could you explain me what is the different between setter injection and constructor injection?

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

    Hi Koushik, thanks again for the exellent tutorials. You are doing a great service to the community by investing your time in creating these videos and offering them for free
    One question though - would you know why Spring framework does not give a feature to put the name of the member variable in the constructor- arg tag. This I thought would have been a foolproof way of passing the right value to the right member variable. Is there some design principle that would be violated if Spring provided this feature?
    Thanks

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

    sana all constructor injection

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

    Hello Koushik, I have a question here. How do we invoke overloaded constructor - i am understanding there is noway to do it from main() to perform IOC? In other word, if an application needs to use overloaded construction (for some reason), do we need to configure spring.xml and invoke from there - whats the real world world scenarion. Tthanks for the series of nice videos. God bless you

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

    aap bhagwaan aadmi ho yaar

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

    what is advantage to use preset value in spring .xml , i need example plz .thank you

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

    What happens if you have two constructors that both take integers: trianlge (int height) and triangle(int base)

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

      vhz04 you cant have 2 constuctor with both of them having only one int argument(will compile error)

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

      vhz04 go through the concept of constructor overloading

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

    what about interface injecton?

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

    @koushks, you are using Ubuntu, right? What programme are you using to screencast?

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

    Just a info for these tutorial while trying example you might face problem like its showing error while running the project "no embedded stylesheet instruction for file" then just select other file or close the xml file before running the project... why it is so might be koushk can give better ans..

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

    hello with spring 3.2.0 when i am using in the spring.xml i am seeing error..Exception in thread "main" java.lang.IllegalArgumentException
    for the line from main....."context = new ClassPathXmlApplicationContext("spring.xml");"Where this works fine . In main context = new ClassPathXmlApplicationContext("spring.xml");
    Triangle triangle = (Triangle) context.getBean("triangle");
    triangle.draw();
    i also had tried: this, nothing fruitful. context been declared as private. Can you please provide a path forward here?

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

      what about the triangle class constructor declaration.

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

    It gives me an illegal argument exception at
    ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
    when I use constructor injection. Please help

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

      kindly help!

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

      Check the spring xml file name and proper spring package imports

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

    Sir my question is if we create two objects of trangle through contextbean do they both refer to the same object or spring container will create two seprate object?
    Trangle obj1= (Trangle)context.getBean("traingle");
    Trangle obj2=(Trangle)context.getBean("trangle");

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

      Trangle obj2=obj1;

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

    If i have a constructer as follows;
    public triangle (String Type,int height,....., int large){...}
    I want to use is like this
    triangle("my type",null,null,null,..,10);
    How can i do it without calling all the arguments in my string.xml
    Thanks

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

      for non string wrapper data types like Integer, Long,etc you can set value="" . For primitive types you should set values otherwise you will get compile time error UnsatisfiedDependencyException and you will get real time compiler warning saying 'cannot convert empty String to type ...'

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

    Code isn't visible clear & can't see output due to subtitles

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

    one basic question could you please clarify? If DI solves all the purpose of making a new object then spring does not really uses OOP? then what it is really? I have heard spring is called as aspect oriented programming? what does it really mean? what is the difference between these two terms? dependency injection and aspect oriented programming? or these two defines the same meaning? I am confused! help me.
    Thanks.

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

      DI is OOP, Spring just does it for you. Spring uses Aspect Oriented Programming ALSO as OOP.

  • @ChandanKumar-rl6df
    @ChandanKumar-rl6df 5 ปีที่แล้ว

    What will happen when i will define two constructor with different number of parameters?

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

      Nice question, try your self you can create it.

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

    I am learning in 2021

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

    sir plz make videos on core java .

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

    Nice videos Great... this code throws an error.

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

    Hi kowshik, I have one question. I want to call both single argument constructor and double argument constructor, how can I do that? Thanks

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

      Call double argument constructor and in that constructor definition include a call to the single argument constructor!!

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

      +Prashant Patel
      In this case if we want to call a single argument constructor with different parameter than in double argument constructor, how do we do that?
      single argument - float (5.0)
      double argument - (string, int)

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

      +vamsi lata: well in that case, you would have to make 3 arguments constructor. Use one argument for passing into single argument constructor and remaining 2 arguments would be used just like in 2-arguments constructor. for example, in your case you would have to make constructor like (float, string, int).