Master java skills

First Spring Application

Let’s create our first spring application. Please follow the below steps.

1. Create a maven project and add following dependency

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.sks</groupId>
  <artifactId>SpringCoreTutorial</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>Spring Core Tutorial</name>
  <description>Spring Core Tutorial</description>
  
  <properties>
	  <java.version>1.8</java.version>
  </properties>
  
  <dependencies>
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-context</artifactId>
  		<version>5.3.0</version>
  	</dependency>
  </dependencies>
  
</project>

2. Create Coffee class

package com.sks;

public class Coffee {

	private String name;
	private String type;
	private int price;

	public String getName() {
		return name;
	}

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

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	public Coffee(String name, String type, int price) {
		super();
		this.name = name;
		this.type = type;
		this.price = price;
	}

	@Override
	public String toString() {
		return "Coffee [name=" + name + ", type=" + type + ", price=" + price + "]";
	}

	public Coffee() {
		super();
		// TODO Auto-generated constructor stub
	}

}

3. Create applicationContext.xml file

<?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="coffee1" class="com.sks.Coffee">
		<property name="name" value="Cappuccino"></property>
		<property name="type" value="hot"></property>
		<property name="price" value="300"></property>
	</bean>
	
	<bean id="coffee2" class="com.sks.Coffee">
		<property name="name" value="Turkish Coffee"></property>
		<property name="type" value="hot"></property>
		<property name="price" value="250"></property>
	</bean>
</beans>

4. Create TestingMain class

package com.sks;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestingMain {
	
	public static void main(String[] args) {

		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		Coffee c1 = (Coffee) context.getBean("coffee1");
		Coffee c2 = (Coffee) context.getBean("coffee2");
		System.out.println("Drink 1 : " + c1);
		System.out.println("Drink 2 : " + c2);

		((ClassPathXmlApplicationContext) context).close();

	}
}

Output: