logo

Spring Application In MyEclipse IDE


Show

It is quite easy to create a Spring Application in MyEclipse IDE. The most interesting part is that you need not think about the jar as MyEclipse IDE itself handles this. So, let's take a look at the following example of creating a Spring Application in MyEclipse IDE.

An overview of creating Spring Applications in MyEclipse IDE is listed below:

  • Setup Java Development Kit (JDK)
  • Download MyEclipse IDE
  • create the java project
  • add spring capabilities
  • create the class
  • create the XML file
  • create the test class

Now we can perform these steps by taking a brief look below:

Setup Java Development Kit (JDK)

First of all, you have to download the latest version of JDK from Oracle's Java site. Now you will find instructions for installing JDK in downloaded files. Now you can install and configure the JDK set up by following the given instructions. At last, set PATH and JAVA_HOME environment variables on your system.

  • Download MyEclipse IDE

Now you are ready to download the latest version of MyEclipse IDE.

  • Create the Java Project

Now you can create a simple Java Project using MyEclipse IDE. Just follow the below steps:

  • Go to File menu - New - project - Java Project (select Java Project wizard from the wizard list)
  • Write the project name (for e.g firstspring)
  • Now click on finish

Your java project is now created.

  • Add spring capabilities

Go to Myeclipse menu > click on Project Capabilities > click on Add spring capabilities > click on Finish

After this, the Spring jar files will be added.

For the simple application, we need only the core library i.e. selected by default.

  • Create Java class file

Now you can create a simple java file with the name of your choice. In this example, we are using the student as our java class file. The name of the student will be displayed by the XML file. It is a simple example of the Spring framework. The actual use lies in the Dependency injection.

To create the java class, just perform the below steps:

  • In package explorer, right-click on src.
  • New -> Package. and then Right click on package name (for e.g student)
  • Finish

Now write the following code:

package com.intellinuts;  
public class Student {  
private String name;  

public String getName() {  
    return name;  
}  
public void setName(String name) {  
    this.name = name;  
}  
public void displayInfo(){  
    System.out.println("Hello: "+name);  
}  
} 

It is a simple bean class that contains only property names with its getters and setters method. This class includes an extra method displayInfo() that gives the output of the student name by the hello message.

  • Create the XML file

Now, when you are using MyEclipse IDE, you don't need to create the XML file as MyEclipse will take care of it. Just open the applicationContext.xml file, and write the below-given code:

  
<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="studentbean" class="com.intellinuts.Student">  
<property name="name" value="Nitin Kaushik"></property>  
</bean>  

</beans>

Here the bean element is used to define the bean for the class given. The property of the Student class named name is specified by the property subelement of bean. The IOC container will specify the value in the property element and set it in the Student class object.

  • Create the test/Demo class

Now Create the java Test class, in this example, we are receiving the object of the Student class from the IOC container using the getBean() method of BeanFactory.

package com.intellinuts;  

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 resource=new ClassPathResource("applicationContext.xml");  
    BeanFactory factory=new XmlBeanFactory(resource);  

    Student student=(Student)factory.getBean("studentbean");  
    student.displayInfo();  
}  
} 


In the above code, the Resource object represents the information of applicationContext.xml file. The ClassPathResource is the implementation class of the Resource interface. The BeanFactory is used for returning the bean and the XmlBeanFactory is the implementation class of the BeanFactory.

Out of many methods that are present in the BeanFactory interface, one method which returns the object of the associated class is the getBean() method.

At last, run the test class to view the following output:

Hello: Nitin Kaushik