Monday, April 1, 2013

Setting Up Environment For Spring In Eclipse

1.   Create a new java project:
·         Go to File menu 
·         Click New
·         Click project
·         Select Java Project.
·         Write the project name e.g. springExample
·         Click Finish à Now the java project is created.

 





2.   Add spring jar files: Add the required jar files in the projectà
·         antlr-runtime-3.0.1
·         org.springframework.aop-3.1.0.M2
·         org.springframework.asm-3.1.0.M2
·         org.springframework.aspects-3.1.0.M2
·         org.springframework.beans-3.1.0.M2
·         org.springframework.context.support-3.1.0.M2
·         org.springframework.context-3.1.0.M2
·         org.springframework.core-3.1.0.M2
·         org.springframework.expression-3.1.0.M2
·         commons-logging-1.1.1

For adding the jar files:
·         Right click the Project, click properties,
·         Click Java Build Path



·         Click Libraries tab
·         And then click add External Jar’s to add the above jar files in the libraries.

3.   Create Java class :
·         Right click the src
·         Then click New
·         And then click Class; a new window will pop up. You can create your new Java class.





·         Click Finish, your class gets created.


·         Add the following content in the java file:
package com.spring.examples;

public class FirstSpringClass {
                        private String name;

                        public String getName() {
                                    return name;
                        }

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

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


4.   Create the configuration file:
·         Right click src
·         Click New
·         Then Click Other
·         Then select xml from XML wizard
 



 



·         Click Next
·         Mention the file name as spring.xml

 

·         Click Finish.

·         Add the following code:



<?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="firstSpringExample" class="com.spring.examples.FirstSpringClass">
<property name="name" value="Sam This is my first Spring Example.." />
</bean>

</beans>



5.   Create the Util/Main class:
·         Right click package,
·         Click New
·         And then click Class


·         Provide the class name.
·         Click on “public static void main”
·          Click Finish.

·         Add the following the code:

package com.spring.examples;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.sun.org.apache.bcel.internal.util.ClassPath;

public class SpringExample {

      /**
       * @param args
       */
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
            FirstSpringClass firstExample = (FirstSpringClass) ctx.getBean("firstSpringExample");
            firstExample.displayInfo();
      }

}




·         We are done now… Right click the above file (i.e. SpringExample) and Click Run as Java Application
 





Next Page > Spring Bean Basics