Get all updates of Java4s
Follow Us: Email Newsletter

Spring Hello World, Setter Injection With Primitive Values

Spring » On Aug 5, 2011 { 35 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

ClientLogic.java

spconfig.xml

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 Author ♦

Java4s_Author
sivateja@java4s.com
Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

35 Responses to “Spring Hello World, Setter Injection With Primitive Values”
  1. Mohammed Vaseem says:

    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

  2. Java4s says:

    @Vaseem

    Yeah that should be java4s.WelcomeBean, instead WelcomeBean, good observation, thank you [ Updated ].

    However In the downloaded example we have given correct :-)

  3. Manoj Ekanayaka says:

    Clear Tutorial Thank for you.
    Also we can load and create beans using
    like this

    ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext(“spconfig.xml”);
  4. Pradeep Kumar Choudhary says:

    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.

  5. Java4s says:

    @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.

  6. Irfan Md says:

    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…….!

  7. Sumith S says:

    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

  8. chandra says:

    thanks for providing this information….thanks a lot

  9. krishnakanth says:

    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.

  10. sree says:

    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…

  11. Arun CH says:

    How do we pass the value to the bean class dynamically from the config file

  12. raj says:

    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 ???

  13. 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”

  14. Ssp says:

    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.

  15. Veeramani M says:

    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

  16. Funny says:

    instead of . It is giving some error of systemID and public Id cannot be same type error while parsing xml file.

  17. mohammed says:

    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

  18. saurabh says:

    realy awesome tutorial for beginers

  19. vikash kumar says:

    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..

  20. Andreas Papandreas says:

    Nice tutorial, it seems easy and I hope that next ones will be simple too :))

  21. sakshi says:

    when i run this application it will give output = null; whether it is right or not

  22. manohar says:

    hi siva,

    the website you have done is awesome to freshers especially

  23. Satya Sharma says:

    Nice Tutorials…best real time examples..

  24. Tuan Vu says:

    Cám ơn bạn nhiều nhé.

  25. Pravin says:

    Best tutorial for beginers….

  26. Aishwarya Tiwair says:

    I haven’t seen such a clear & easy to understand explaination in any tutorial on the net. Thanks! :)

  27. reddy says:

    Hi Team,

    can you explain these concepts(dependency injection, Autowiring) using annotations.

  28. kiran says:

    why we are using .xml file for creating beans

  29. Gulshan says:

    Hi Team,
    Can u plz explain what Resource and ClassPathResource is???
    Thank u,
    Gulshan.

  30. SANGRAM KESHARI CHOUDHURY says:

    Explanation is more clear.

  31. Abhilash says:

    please provide us a link to download jar files,

  32. trinadh says:

    what is p namespace and c namespace in spring

  33. Rakesh Bhagat says:

    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")

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
Upcoming
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2016 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.