This article will talk about the rudimentary steps required to set up a simple Hello World application in Java using Spring Framework.
Download Links
Steps
1. Create a new Java Project in Eclipse
2. In Project Explorer,
right click on project name,
Build Path,
Configure,
Libraries Tab,
Add External Jars
Browse & add
All the executable Jar files from spring-framework-3.1.0.M2/dist directory.
antlr-runtime-4.2.1
commons-logging-1.1.3
3. In the src folder create package org.techmight
4. Add the following two classes in org.techmight package.
HelloWorld.java
MainApp.java (this class will have main method)
5. Create spring-config.xml in src folder with the following bean configuration.
6. Finally right click on MainApp.java,
Run as
Java application
Output
Your Message : Hello World from TechMight Solutions!
Advantage
The default values of the member variables can be changed by making the changes in spring-config.xml file without changing any Java code.
Implementation
E.g. Consider a class which uses some configuration variables for database communication. If there is any change in any of these configurations, there is no need to make modifications in java code and then recompile it. Only setting the proper configuration in spring-config.xml file will do the work.
Download Links
Steps
1. Create a new Java Project in Eclipse
2. In Project Explorer,
right click on project name,
Build Path,
Configure,
Libraries Tab,
Add External Jars
Browse & add
All the executable Jar files from spring-framework-3.1.0.M2/dist directory.
antlr-runtime-4.2.1
commons-logging-1.1.3
3. In the src folder create package org.techmight
4. Add the following two classes in org.techmight package.
HelloWorld.java
package org.techmight; /** * @author TechMight Solutions */ public class HelloWorld { private String message; public String getMessage() { return ("Your Message : " + message); } public void setMessage(String message) { this.message = message; } }
MainApp.java (this class will have main method)
package org.techmight; /** * @author TechMight Solutions */ import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "spring-config.xml"); HelloWorld obj = (HelloWorld) context.getBean("helloWorld"); String response = obj.getMessage(); System.out.println(response); } }
5. Create spring-config.xml in src folder with the following bean configuration.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="helloWorld" class="org.techmight.HelloWorld"> <property name="message" value="Hello World from TechMight Solutions!"/> </bean> </beans>
6. Finally right click on MainApp.java,
Run as
Java application
Output
Your Message : Hello World from TechMight Solutions!
Advantage
The default values of the member variables can be changed by making the changes in spring-config.xml file without changing any Java code.
Implementation
E.g. Consider a class which uses some configuration variables for database communication. If there is any change in any of these configurations, there is no need to make modifications in java code and then recompile it. Only setting the proper configuration in spring-config.xml file will do the work.
No comments:
Post a Comment
Your comments are very much valuable for us. Thanks for giving your precious time.