How to create spring application by using eclipse IDE

To create spring application by using eclipse IDE, we have to follow below steps. There are majorly 5 steps we mentioned for how to create spring application by using eclipse IDE. That are,
  1. Create Java Project
  2. Add spring jar files
  3. Create the bean class
  4. Create XML file to provide values
  5. Create the Test class
Create Java Project:
        Follow these suggestions to create a new Java Project in Eclipse IDE. File --> New --> Java Project

        Click 'Next' and then click 'Finish'.

Add Spring libraries:
        Once creating the project you can download the Spring core module libraries from the Internet and add it into your creating project.

Spring core library files:
        The below which I mentioned library files are related to Spring core module. In that which are mandatory and which are optional library files also I defined in below. Download those all library files from Internet and add into newly creating Spring project.
  • com.springsource.org.apache.commons.logging-1.1.1.jar (Mandatory) 
  • com.springsource.org.apache.log4j-1.2.15.jar (Optional) 
  • jmxtools-1.2.1.jar (Optional) 
  • org.springframework.asm-3.0.1.RELEASE-A.jar (Mandatory) 
  • org.springframework.beans-3.0.1.RELEASE-A.jar (Mandatory) 
  • org.springframework.context-3.0.1.RELEASE-A.jar (Mandatory) 
  • org.springframework.core-3.0.1.RELEASE-A.jar (Mandatory) 
  • org.springframework.expression-3.0.1.RELEASE-A.jar (Optional)
Steps to add library file into project.
        Here is the steps how to add library files into project.
Right click on project top folder --> select 'Properties' --> go to 'Java Build Path' --> Choose 'Libraries' --> Click on 'Add External Jars' --> browse downloading path --> select all the Jars

       then click 'Open' --> then say 'OK'.

       This is the way to add the library files into a project.

Create Bean Class:
This is the simple bean class which is containing the property of name.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
 package com.javatbrains;

 public class SpringBean {
    private String name;

    public String getName() {
       return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void showInfo() {
        System.out.println("Hello : " + name);
    }
 }

The above SpringBean class contains one extra method name called showInfo() which will helps to print name with the message of (Hello : +name).

Create XML file:
This xml file should be present under the project src folder not in the package.

<?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="springBean" class="com.javatbrains.SpringBean">
            <property name="name" value="JavaTBrains">   </property>
       </bean>
 </beans>

In the above xml file declaring the <bean> tag under the <beans> tag. Inside the bean declared as a property which represents the value of name in bean class. Here, only one property declared in java bean if we have more properties in bean class that many <property> tags we need to use in bean tag for passing the values.

Create the Test class:
Here creating the test class for running the application name called SetterTest.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
package com.javatbrains;

 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 SetterTest {

      public static void main(String[] args) {
           Resource resource = new ClassPathResource("applicationContext.xml");
           BeanFactory factory = new XmlBeanFactory(resource);
           SpringBean bean = (SpringBean) factory.getBean("springBean");
           bean.showInfo();
     }
 }

Steps to remember:
  • Create the Resource instance by using the class name called ClassPathResource by passing the applicationContext.cml file.
  • Create BeanFactory instance by using the XmlBeanFactory class by passing the resource instance.
  • Getting the bean object by using the factory.getBean() method by passing the bean id which we declared in applicationContext.xml file.
Output: 
log4j:WARN No appenders could be found for logger      (org.springframework.beans.factory.xml.XmlBeanDefinitionReader).
log4j:WARN Please initialize the log4j system properly.
Hello : JavaTBrains

Comments

Popular posts from this blog

how to count the page views by using JSP

Exception in thread "main" java.lang.NoClassDefFoundError: javax/transaction/SystemException

Multithreading in java with example