Spring Hello world example in eclipse

This is 4 of 16 parts of tutorial series

Tutorial Content: Spring tutorial for beginners

If you want to be master in spring core, you can go for its certification too. You can go through spring certification questions which I found useful.

After having some basic understanding of spring framework now we are ready to create Hello world example.
Firstly download required jars and docs from Spring Downloads | SpringSource.org
Now, In eclipse IDE,click on File->new->Click on other and then select java project

 

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️
Click on next and  Write project name.I have writtern here “HelloWorldInSpring3”
Click on finish and now our project is created
Create new folder “jars” under src folder so for that right click on project->new->folder
Write folder name as “jars”
click on finish and empy jar folder will be created in src folder.
Now we will add the Spring 3. libraries to the project. Extract the “spring-framework-3.1.1.RELEASE-with-docs.zip” file if you have not extracted. Now go to the “dist” directory of the and then copy all the jar files (Ctrl+C) and paste on the jars directory (of our project) in the Eclipse IDE.
Then find the commons-logging.jar from extracted folder and also copy this file into Eclipse IDE. You will find this library into spring-framework-3.0.0.RELEASE-with-docsspring-framework-3.1.1.RELEASEprojectsspring-buildlibivy folder.
Now add all the jars to “Java Build Path”. Right click on the “Spring3HelloWorld” in project explorer and then select properties. Then select “Java Build Path” –> Libraries and then click on the “Add JARs” button. And add all the libraries to Java Build Path.
Create a new package org.arpit.javapostsforlearning to hold the java files. Right click on the “src” folder and then select New –> Package. Then provide the package name as org.arpit.javapostsforlearning and click on the “Finish” button.

Spring hello world example

Create a new Java file Spring3HelloWorld.java under the package org.arpit.javapostsforlearning and add the following code:

now create a Xml file called Spring3HelloWorld in src folder.Add following content to it
Here tag is for defining multiples beans.All beans of our program are defined in tag
tag is for defining a single bean.

“id” is for providing unique identification to bean.
“class” is fully qualified name of class
“property” is used for defining attribute of bean class.

The above xml file declares the spring bean “Spring3HelloWorldBean” of the class org.arpit.javapostsforlearningSpring3HelloWorld.

Create another java file named “Spring3HelloWorldMain.java” under package org.arpit.javapostsforlearning.Copy following content in “Spring3HelloWorldMain.java” file

In the above code, we have created the instance of ApplicationContext and the retrieved the “Spring3HelloWorldBean”. Then we called the printHello() method on the bean.

Run program:

right click on project->Run as->Java Application

Output is :

That’s all about Spring hello world example.
In next post, we will see dependency injection via setter method.


import_contacts

You may also like:

Related Posts

  • 24 June

    No qualifying bean of type in Spring or Spring Boot

    In this post, we will see about an exception: No qualifying bean of type. Exceptions are least expected but you might get it while working with Spring. or Spring boot. Did you get this exception: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type? If yes, let’s see different reasons for it. 💻 Awesome Tech Resources: Looking for […]

  • 17 June

    Spring XML configuration example

    In this post, we will see how to create Spring hello world XML based configuration example. Here are simple steps to create Spring XML configuration example. 1. Create a simple java maven project. 2. Maven dependency put spring and cglib maven dependency in pom.xml. 💻 Awesome Tech Resources: Looking for ⚒️ tech jobs? Go to our […]

  • 15 June

    Injecting Prototype bean into a Singleton bean in Spring

    In this post, we will see how to inject prototype bean scope into Singleton Instance in Spring. This is one of the most asked Spring interview questions. Problem When you inject prototype bean to singleton bean, prototype bean still behave like a singleton bean. Let’s understand this with the help of example. 1. Create a […]

  • 15 June

    Circular dependencies in Spring

    In this post, we will discuss one of the most asked Spring interview questions. What happens if there are circular dependencies in Spring. Problem What if there are circular dependencies in Spring? For example: Class A requires an instance of Class B and Class B requires an instance of Class A. Let’s create an example […]

  • 12 September

    Spring init-method and destroy-method example

    In this post, we will see about Spring init-method and destroy-method. When bean is instantiated or destroyed , there are some operations you may need to perform, so you can use init-method and destroy-method to call this methods while bean is being created or destroyed. Lets understand it with the help of simple example: For […]

  • 27 July

    Prefix “context” for element “context:component-scan” or “context:annotation-config” is not bound : Spring error

    When you are working on spring or spring mvc application, you may encounter below error message. org.xml.sax.SAXParseException: The prefix “context” for element “context:component-scan” is not bound . or org.xml.sax.SAXParseException: The prefix “context” for element “context:annotation-config” is not bound . This issue generally occurs when you do not include context namespace. For example: Lets say you have below […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.