Newsletter |
Spring Hello World, Setter Injection With Primitive Values
Spring » on Aug 5, 2011 { 39 Comments } By Sivateja
Let us see the first program in spring, which is going to be the setter injection with some primitive values…
Files required..
- WelcomeBean.java
- ClientLogic.java [ Our logic ]
- spconfig.xml [ spring configuration file, it can be of any name ]
Mates am going to give you the program directly in the eclipse 🙂
Open Eclipse –> File –> New –> (Select Java project, if not found just click on) Other
Now choose –> java project and click on next
Now it will ask you to give you application name, so give what ever name you want and click finish…
That’s it.., now you will see your application will looks like this..
Of course i have been created the files in java4s package…..
Let us see the program files logic
WelcomeBean.java
package java4s; public class WelcomeBean { private String message; public void setMessage(String message) { this.message = message; } public void show() { System.out.println(message); } }
ClientLogic.java
package java4s; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class ClientLogic { public static void main(String[] args) { Resource res = new ClassPathResource("spconfig.xml"); BeanFactory factory = new XmlBeanFactory(res); Object o = factory.getBean("id1"); WelcomeBean wb = (WelcomeBean)o; wb.show(); } }
spconfig.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <bean id="id1" class="java4s.WelcomeBean"> <property name="message" value="Welcome to spring" /> </bean> </beans>
Explanation
- see WelcomeBean.java, i have written setter method for the property message (primitive), spring container will inject some value in that property at run time
- In ClientLogic.java first we need to load the configuration file, so we done this at line number 12, so res, contains all the information about the configuration xml.
- And give this res object to BeanFactory [ Spring container ] with XmlBeanFactory, so now factory knows all the beans in the xml file so we can now call any bean with bean id.
- In ClientLogic.java, if we call getBean(“id1”) then internally the spring framework executes the following statements
WelcomeBean wb = new WelcomeBean();
wb.setMessage(“Welcome to spring”); - And now will gives WelcomeBean object back [at line number 15,] in the form of Object class object, and i typecast into WelcomeBean class at line number 16
- Remember, by default every spring bean is the singleton class. Spring IOC container makes a spring bean as singleton automatically
- Return type of getBean() is always super class object, which is Object class object
- i have given that primitive type as String, you can use int, float, double what ever you want
- See in spconfig.xml, line number 7 we have written the property element right, here <property /> means we are saying to the spring container that we have written setter method in our bean class [WelcomeBean.java 7 to 9 lines ], in the that property we assigned value as an attribute, which means the setter injection is in the form of primitive values [ may be int, string, float bla bla.. ]
Hope you got this tutorial, if not so please go back and see the basic tutorials first.
You Might Also Like
::. About the Author .:: | ||
Comments
39 Responses to “Spring Hello World, Setter Injection With Primitive Values”
Hello Java4s Team,
I found a mistake in spconfig.xml file.
In bean tag, the value of class attribute should be “java4s.WelcomeBean”.
Thanks for providing wonderful tutorials….
Vaseem
@Vaseem
Yeah that should be java4s.WelcomeBean, instead WelcomeBean, good observation, thank you [ Updated ].
However In the downloaded example we have given correct 🙂
Clear Tutorial Thank for you.
Also we can load and create beans using
like this
Thanks java4s team,
Its crystal clear and explained nicely in short..! There is one minor spelling mistake in the second last paragraph in the sentence “i have given spring type as primitive, you can use int, float, double what ever you want”.
In this line in place of spring String should be there.
@Pradeep Kumar Choudhary
You bet, yeah you are correct 🙂 i have updated.
That should be ‘primitive type as string‘ not spring lolll.
Thanks for this update.
Hi Java4s Team,
This is Irfan and Thing to say is that “java4s.com” is a Tutorial which I found very much easy to understand things which are really tough to take In….
that’s it from my side to say I fell short of words to explain.!
Keep going…….!
i have included spring library, spring jar file and common logging jar file but when i am trying to write a code for client it is showing org.sp-fr.beans.beans.factory.xml and likewise other package does not exist. I am working on netbeans
thanks for providing this information….thanks a lot
No way java.lang.String is primitive type!
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
thanks for giving spring tutorial in detail,really awesome very thankful to you,these tutorials really making it easier to learn the frameworks on our own ,i would rather go through these tutorials
than going for institutions.
excelnt explanation sir…i m totaly in confusion..when i m starting spring, totaly i m confused core, but i got clear idea..with your explanation…thank you so much sir…
How do we pass the value to the bean class dynamically from the config file
Siva,
One observation, the Setter method setMessage is following hungarian notation however the property name has to be specified in lowecase, if your property name is “Message” instead of “message”, The compiler gives an error, could you elaborate on that ???
Nice Tutorial Thanks
Nice tutorial.
But string is said as primitive which is wrong. (I agree with @Betlista)
The line
“i have given that primitive type as String, you can use int, float, double what ever you want”
should be changed to something like this,
“In this example, the “WelcomeBean” class as property “message”‘ which is of of type String, It can even be primitive type like int, float, double, etc”
Nice tutorial..
But One thing I want to suggest to you that specify the Spring version (2.x/3.x/4) in example so beginners can understand easily.
Hi Team,
Thanks for the Tutorial…
I have started learing spring through this tutorial..
I had downloaded spring jars and added then to reference library. Downloaded the project. all settled. Now how to run this example..
We should run as java application or through run configuration..?
Pls guide me
instead of . It is giving some error of systemID and public Id cannot be same type error while parsing xml file.
hiii . . . .ur tutorials are very useful.i request you to plz gv the examples in netbeans also or gv d source code in netbeans IDE, please
thanq in advance
realy awesome tutorial for beginers
to java4s team,
thanks for creating this wonderful tutorial for spring .. i am regular reader of this website for spring tutorial..this tutorial is really awesome..the quality of your tutorial is that you explain all concept in very simple word … i really appreciate your effort..
Nice tutorial, it seems easy and I hope that next ones will be simple too :))
when i run this application it will give output = null; whether it is right or not
hi siva,
the website you have done is awesome to freshers especially
Nice Tutorials…best real time examples..
Cám ơn bạn nhiều nhé.
Best tutorial for beginers….
I haven’t seen such a clear & easy to understand explaination in any tutorial on the net. Thanks! 🙂
Hi Team,
can you explain these concepts(dependency injection, Autowiring) using annotations.
why we are using .xml file for creating beans
Hi Team,
Can u plz explain what Resource and ClassPathResource is???
Thank u,
Gulshan.
Explanation is more clear.
please provide us a link to download jar files,
what is p namespace and c namespace in spring
In explanation you told if we call getBean("id") it will execute
WelcomeBean wb = new WelcomeBean();
wb.setMessage(“Welcome to spring”);
if we have three properties like name,address,phone then it will execute setter method of all three properties.??
like this..
wb.setName("something");
w.setAddress("something")
Very good explanation… Thanks
Thanks java4s team for crystal clear tutorial.
Superb explanation sir. Keep it up.
Nice Explanation bro, it is an understand easy to for all new beginners….
Thanks
Jagadeesh