JavaSpring

Spring Hello World with Constructor Injection using XML

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

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

We will be using constructor injection for injecting dependencies. The constructor injection will be for String literal.

 

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>

</dependencies>

</project>

 

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-4.0.xsd”>

<bean id=“firstBean” class=“com.techkatak.app.Hello”>

<constructor-arg 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 Hello(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.

 

Here Spring is passing “Techkatak” as the constructor argument in Hello class’s constructor.

 

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

 

Hello Techkatak

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