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.
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.
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!!
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!
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.
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.
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.
@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?
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
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 :)
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
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
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?
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
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
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..
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?
It gives me an illegal argument exception at ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); when I use constructor injection. Please help
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");
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
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 ...'
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.
+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)
+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).
Your tutorials are brilliant even after 12 years. Many thanks to you Koushik!
This is legit the most comprehensive tutorial on Sping on the internet. Thanks a lot.
Never seen a beautiful explanation like this for Dependency injection ever!!!! You're rocking Koushik..
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.
welcome to developer community sometimes you inherit code, and this happens to many software engineers.
All the best buddy
I hope you got that job...
Did you get that job?
@@worldwide6626 +1
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!!
After a decade of this video is made, it still well helped me understanding the basic of Spring, thank you
123 thousand views and just 384 likes....people are lazy. You deserve more likes sir. Excellent tutorials. Thank you.
there should be giveaway.. if this video gets 1000 likes, will give away bag of coffee beans.
Koushik, I must say, your explanation is very good and I love the way you unfold things slowly and with the right context.
With the newer version of Spring, you can either use type="int" or name="height".
thanks for the info dude :D
Well explained! Questions come up to me just been solved later in the video! Really nice work! Appreciate it!
excellent stuff..you are a brilliant teacher, why oh why cant lecturers be more like this..
Awesome job man!! Best Spring tutorial online!!
3.15 : Sprint 4.1.6 has now included name as I can see. Used it and works well.
sir... superb tutorials!!! love to work on springs now!!
Koushks gr8 job man. The way u explain is just like the way Sachin hit cover drive :) a treat to watch.
Thanks man.
Ur way of teaching is really good , Thank you very much for making such videos:)
We salute you. Great tutorial !
Dude, I became ur fan...!! Keep going and educating us...!!
Thanks Koushik, the tutorials are very good
you are a greater teacher
thank you!
Thanks for the video. Another awesome video!
Thank-you for sharing your knowledge.
Excellent Explanation!
Excellent tutorial.
Excellent tutorials..
wonderful tutorial,...really helpful
Dhanyavad
excellent!!!
excellent tutorial.....thanks for sharing
thanks a lot
,you explain it so well .
awesome work!!
awesome way of teaching
thank you so much
Kaushik .. You the Best !!!
Great tuts :) Thank you !
Awsm!🙌
hi , if we are setting values in the xml file are we not making the whole thing hard coded in nature ???
Very useful. Thanks!!!
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!
And that is you can change the program without the need to re-compile it. Wow, that's pretty nice :)
very nice and descriptive in short time.
You are just awsome , Thanks a lot
great tutorial. Thanks.
Awesome job
I love you Man..you are the best....God bless
Great tutorial
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.
great tutorial
thanks for these!
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.
thank you very much.
great stuff
thanks!
Very useful.
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.
amazing!! thanks..
Informative!
what if i need to pass variables into spring.xml? i.e., i want to set constructors with dynamic values not static
How do i assign an object to a constructor in the tag???
index in bean define according to the order of the member variable declaration in class or randomly we can assign ??
Is this him? :) The best teacher in the world? I see some comments they are calling him like that.
thank you so much i can easy learn this ....:))
Very good
sir it can also run without getter() method that you have created for variable height.
Thank's a lot
@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?
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
Thx!
Is it possible to inject dynamic values in the XML ? Much more useful coz we might need to pass different values different time
Thnx :)
What if I want to pass Null to a String argument?
Which is better constructor injection or using setter ???
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 :)
so there's only 1 instance object of the Triangle at the same time?
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
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
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?
use name property or type property
nice
HIBERNATE and Spring Bridge how cld i? is it possibl?
how to inject values in to default constructor?
ok i got that later in the tutorial
Hi kowshik could you explain me what is the different between setter injection and constructor injection?
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
sana all constructor injection
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
aap bhagwaan aadmi ho yaar
what is advantage to use preset value in spring .xml , i need example plz .thank you
What happens if you have two constructors that both take integers: trianlge (int height) and triangle(int base)
vhz04 you cant have 2 constuctor with both of them having only one int argument(will compile error)
vhz04 go through the concept of constructor overloading
what about interface injecton?
@koushks, you are using Ubuntu, right? What programme are you using to screencast?
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..
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?
what about the triangle class constructor declaration.
It gives me an illegal argument exception at
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
when I use constructor injection. Please help
kindly help!
Check the spring xml file name and proper spring package imports
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");
Trangle obj2=obj1;
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
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 ...'
Code isn't visible clear & can't see output due to subtitles
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.
DI is OOP, Spring just does it for you. Spring uses Aspect Oriented Programming ALSO as OOP.
What will happen when i will define two constructor with different number of parameters?
Nice question, try your self you can create it.
I am learning in 2021
sir plz make videos on core java .
Nice videos Great... this code throws an error.
Hi kowshik, I have one question. I want to call both single argument constructor and double argument constructor, how can I do that? Thanks
Call double argument constructor and in that constructor definition include a call to the single argument constructor!!
+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)
+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).