JavaSpring

Spring Hello World

In this post, we will create Hello World program in Spring using Maven.

We will use an xml based configuration file for managing dependencies.

 

First of all if you don’t have Maven, download maven and make sure to setup JAVA_HOME and PATH variables.

Please refer below article for this.

Maven Basics

 

Now, run the following command :

mvn archetype:generate -DgroupId=com.techkatak.app -DartifactId=app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

 

This will create a basic java project.

Now lets Import this project in eclipse :

proj1

 

 

The project is a normal java project.. We will need to add dependencies for Spring.

Lets add the Spring dependency under Dependencies section in  pom.xml.

 

Here is how the final pom.xml will look like :

<project xmlns=“http://maven.apache.org/POM/4.0.0” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd”>

<modelVersion>4.0.0</modelVersion>

<groupId>com.techkatak.app</groupId>

<artifactId>app</artifactId>

<packaging>jar</packaging>

<version>1.0-SNAPSHOT</version>

<name>app</name>

<url>http://maven.apache.org</url>

<dependencies>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>4.1.6.RELEASE</version>

</dependency>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>3.8.1</version>

<scope>test</scope>

</dependency>

</dependencies>

</project>

Here run the following from command prompt from the root of your project(where pom.xml is present).

validate

 

We will be using XML based configuration in this post. So, lets add an xml file Spring.xml in src/main/java

In this xml, we are creating a Bean “firstBean”.

 

<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=“firstBean” class=“com.techkatak.app.Hello”>

<property name=“name” value=“Techkatak” />

</bean>

</beans>

 

Here is how the project structure should look like now :

projstructure

 

 

Add a Hello.java class under com.techkatak.app package:

package com.techkatak.app;

public class Hello {

private String name;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public void print(){

System.out.println(“Hello “ + name);

}

}

 

Also, add an App.java under com.techkatak.app :

 

package com.techkatak.app;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App

{

private static ApplicationContext context;

public static void main( String[] args )

{

context = new ClassPathXmlApplicationContext(“Spring.xml”);

Hello hello = (Hello) context.getBean(“firstBean”);

hello.print();

}

}

 

In this class, we are Spring provided ClassPathXmlApplicationContext class to create a Hello instance.

If you run this class , you will get the following output :

 

Hello Techkatak

 

 

 

 

 

 

 

© 2015, www.techkatak.com. All rights reserved.