Spring in Eclipse

Here, i am going to present how to develop a simple spring application using eclipse IDE. The following steps are required to develop spring application.

  • create the java project
  • add spring jar files
  • create the class
  • create the xml file to provide the values
  • create the test class

Let us see a demo program , which is going to be the setter injection with some primitive values.

Required Files

  • WelcomeBean.java
  • spconfig.xml [ spring configuration file ]
  • Test.java

Step-1

Open eclipse then go to File menu - New - project - Java Project. Write the project name e.g. First - Finish. Now the java project is created.

Step-2

Add spring jar files:

There are mainly three jar files required to run this application.

  • org.springframework.core-3.0.1.RELEASE-A
  • com.springsource.org.apache.commons.logging-1.1.1
  • org.springframework.beans-3.0.1.RELEASE-A

To run this example, we need to load only spring core jar files. To load the jar files in eclipse IDE, Right click on your project - Build Path - Add external archives - select all the required jar files - finish.

Step-3

Then we will create a java class like WelcomeBean class have msg property. The value of msg will be provided by the xml file. To create the java class, Right click on src - New - class - Write the class name e.g. WelcomeBean - finish.

WelcomeBean.java

package java8s;
public class WelcomeBean {
    private String message;
    public void setMessage(String message {
        this.message = message;
    }
    public void show() {
        System.out.println(message);
    }
}

Here WelcomeBean is a simple bean class, containing one property msg having getters and setters method. This class contains one extra method named print() that displays the value of the msg.

Step-4

Create the xml file: To create the xml file click on src - new - file - give the file name such as spconfig.xml - finish. Open the spconfig.xml file, and write the following code:

spconfig.xml <<
    ? xml version = "1.0"
encoding = "UTF-8" ? >
    <
    beans
xmlns = "http://www.springframework.org/schema/beans"
xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns: p = "http://www.springframework.org/schema/p"
xsi: schemaLocation = "http://www.springframework.org/schema/beans
http: //www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <
    bean id = "id1"
class = "java8s.WelcomeBean" >
    <
    property name = "message"
value = "Welcome 2 SILAN TECHNOLOGY" > < /property> <
    /bean>

    <
    /beans>

The bean element is used to define the bean for the given class. The property is the sub element of bean specifies the property of the TestBean class named name. The value specified in the property element will be set in the TestBean class object by the IOC container.

Step-5Create the test class ; Test.java

package java8s;

Test.java

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 Test {
    public static void main(String[] args) {
        Resource res = new ClassPathResource("spconfig.xml");
        BeanFactory bf = new XmlBeanFactory(res);
        Object ob = bf.getBean("id1");
        WelcomeBean wb = (WelcomeBean) ob;
        wb.show();
    }
}

Output


 

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

SILAN Technology, BBSR
Call to: 0674-2361252

Comment