Contact us...Newsletter

 
 
 

Creating spring application in Eclipse IDE

 
 

Creating spring application in Eclipse IDE

 
Here, we are going to create a simple application of spring framework using eclipse IDE. Let's see the simple steps to create the spring application in Eclipse IDE.
  • Create the java project
  • Add spring jar files
  • Create the class
  • Create the xml file to provide the values
  • Create the test class
  •  

    Steps to create spring application in Eclipse IDE

     
    Let's see the 5 steps to create the first spring application using eclipse 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 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
  • For the future use, You can download the required jar files for spring core application.
     

    download the core jar files for spring

     
     

    download the all jar files for spring including aop, mvc, j2ee, remoting, oxm, etc.

     
    To run this example, you 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..
     

    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:
    package com.dzosoft;  
      
    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);  
    	}  
    }  
    

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

    To create the xml file click on src - new - file - give the file name such as applicationContext.xml - finish. Open the applicationContext.xml file, and write the following code:
    <ul> ?xml version="1.0" encoding="UTF-8"?a>
    <ul> 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"a>
      
    <ul> bean id="studentbean" class="com.javatpoint.Student">  
    <ul> property name="name" value="dzosoft"><ul>/propertya>
    <ul>/beana>  
      
    <ul>/beansa>  
    

    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.
    package com.dzosoft;  
      
    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();  
    }  
    }
    

    Now run this class. You will get the output Hello: dzosoft.
     
    Creating spring application in Eclipse IDE
     
     
    Creating spring application in Eclipse IDE
     
    Enjoy
    Download example
    Reviews of dzosoft.com   Copyright © 2022  -  dzosoft.com  All rights reserved.  
    dzosoft- How to do it ?
    What they are cookies
    Like most websites, we use data file small
    dimensions that are saved on your computer, tablet, mobile phone or other device
    Mobile (collectively referred to as 'device') to record certain data whenever
    login or interact with our sites, services, apps, messaging systems
    and strumenti.I names and specific types of cookies used can change over time.
    To help you better understand the rules and \ 'use of such technology, the following are
    some terms and their definitions:
    Cookies: small text files (usually formed by letters and numbers) that are saved
    in memory of the browser or the device when you visit a website or display a message.
    Cookies enable a website to recognize a particular device or browser. There
    different types of cookies:
    Session cookies expire at the end of the browser session and allow us to
    connect your actions during that specific session.
    Persistent cookies are instead stored in your device even after the end
    the browser session and let you remember your preferences or actions at multiple sites.
    First-party cookies are set by the site you're visiting.
    The third-party cookies are set by a third party site than the site you are visiting.
    Cookies can be disabled or removed using the tools available in most
    browsers. The cookie preferences must be set separately
    for each browser used, because each offers features and specific options.
    dzosoft.com - How to do it ?
    Licenza Creative Commons
    How to do it ?    - download & install software     is licensed under license Creative Commons Attribution - Non-commercial - Share alike 4.0 International.