W3Schools Learner's Blog

W3Schools Programming knowledge summary website



how to creating spring application in myeclipse IDE

Example of spring application in Myeclipse

Creating spring application in myeclipse IDE is simple. You don't need to be worried about the jar files required for spring application because myeclipse IDE takes care of it. Let's see the simple steps to create the spring application in myeclipse IDE.
  • create the java project
  • add spring capabilities
  • create the class
  • create the xml file to provide the values
  • create the test class

Steps to create spring application in Myeclipse IDE

Let's see the 5 steps to create the first spring application using myeclipse IDE.

1) Create the Java Project

Go to File menu - New - project - Java Project. Write the project name e.g. firstspring - Finish. Now the java project is created.

2) Add spring capabilities

Go to Myeclipse menu - Project Capabilities - Add spring capabilities - Finish. Now the spring jar files will be added. For the simple application we need only core library i.e. selected by default.

3) Create Java class

In such case, we are simply creating the Student class have name property. The name of the student will be provided by the xml file. It is just a simple example not the actual use of spring. We will see the actual use in Dependency Injection chapter. To create the java class, Right click on src - New class - Write the class name e.g. Student - finish. Write the following code:
  1. package com.javatpoint;  
  3. public class Student {  
  4. private String name;  
  6. public String getName() {  
  7.     return name;  
  8. }  
  10. public void setName(String name) {  
  11.     this.name = name;  
  12. }  
  14. public void displayInfo(){  
  15.     System.out.println("Hello: "+name);  
  16. }  
  17. }  
This is simple bean class, containing only one property name with its getters and setters method. This class contains one extra method named displayInfo() that prints the student name by the hello message.

4) Create the xml file

In case of myeclipse IDE, you don't need to create the xml file as myeclipse does this for yourselves. Open the applicationContext.xml file, and write the following code:
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:p="http://www.springframework.org/schema/p"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  9. <bean id="studentbean" class="com.javatpoint.Student">  
  10. <property name="name" value="Vimal Jaiswal"></property>  
  11. </bean>  
  13. </beans>  
The bean element is used to define the bean for the given class. The property subelement of bean specifies the property of the Student class named name. The value specified in the property element will be set in the Student class object by the IOC container.

5) Create the test class

Create the java class e.g. Test. Here we are getting the object of Student class from the IOC container using the getBean() method of BeanFactory. Let's see the code of test class.
  1. package com.javatpoint;  
  3. import org.springframework.beans.factory.BeanFactory;  
  4. import org.springframework.beans.factory.xml.XmlBeanFactory;  
  5. import org.springframework.core.io.ClassPathResource;  
  6. import org.springframework.core.io.Resource;  
  8. public class Test {  
  9. public static void main(String[] args) {  
  10.     Resource resource=new ClassPathResource("applicationContext.xml");  
  11.     BeanFactory factory=new XmlBeanFactory(resource);  
  13.     Student student=(Student)factory.getBean("studentbean");  
  14.     student.displayInfo();  
  15. }  
  16. }  
The Resource object represents the information of applicationContext.xml file. The Resource is the interface and the ClassPathResource is the implementation class of the Reource interface. The BeanFactory is responsible to return the bean. The XmlBeanFactory is the implementation class of the BeanFactory. There are many methods in the BeanFactory interface. One method is getBean(), which returns the object of the associated class.

Now run the Test class. You will get the output Hello: Vimal Jaiswal.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.