Spring 5 Hello World Example + Java Configuration


How to create a Spring project using Maven are demonstrated in this guide. In this Spring Hello World Example – Java Configuration are used.

Technologies and Tools Used for Spring Hello World Example – Java Configuration Project

  • Java 8
  • Maven 3.6.0
  • Spring 5.2.0.RELEASE
  • Eclipse

1. Create a Maven Project and Add Dependency

In Eclipse, create a maven project and add the following dependency.

File : pom.xml

<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.learnfromexamples.springcore</groupId>
	<artifactId>JavaConfiguration</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

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

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<spring.version>5.2.0.RELEASE</spring.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>

2. Spring Bean (Java Modal Class)

Create a plain Java Class which is POJO class.

File : Employee.java


package com.learnfromexamples.springcore.modal;

public class Employee {
	private String empName;

	public String getEmpName() {
		return empName;
	}

	public void setEmpName(String empName) {
		this.empName = empName;
	}

	@Override
	public String toString() {
		return "Employee [empName=" + empName + "]";
	}
	
}

 

3. Spring Bean Configuration File

Create a Java configuration by specifying the bean which needs to be created and dependency which needs to be injected.

File : AppConfig.java

 

package com.learnfromexamples.springcore.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.learnfromexamples.springcore.modal.Employee;

@Configuration
public class AppConfig {

	@Bean(name="sridhar")
	public Employee getEmployee() {
		Employee emp = new Employee();
		emp.setEmpName("Sridhar - Spring Core - Java Config");
		return emp;
	}
}


 

4. Main Program

Create a Java File with main method and configuration file should be loaded using AnnotationConfigApplicationContext.  Spring Container  (i.e ApplicationContext)will take care of creating an instance and injecting the dependency. Bean reference can be retrieved either by providing Class name or by providing the bean name which is specified in AppConfig.java.

File : App.java


package com.learnfromexamples.springcore.JavaConfiguration;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.learnfromexamples.springcore.configuration.AppConfig;
import com.learnfromexamples.springcore.modal.Employee;

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

		@SuppressWarnings("resource")
		ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

		//Employee emp = context.getBean(Employee.class);
		
		Employee emp = (Employee) context.getBean("sridhar");
		
		// but 
		//Employee emp = (Employee) context.getBean("empName"); 
		// Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'empName' available

		System.out.println(emp);  
	}
}

4. Output

Employee [empName=Sridhar - Spring Core - Java Config]

 

5. Source Code

Get the source code from the below git url.

Spring Hello World Example + XML Configuration

Spring Hello World Example

6. Reference

Spring Hello World

Spring 5 Hello World Example + XML Configuration


How to create a Spring project using Maven are demonstrated in this guide. In this Spring Hello World Example – XML Configuration are used.

Technologies and Tools Used for Spring Hello World Example – XML Configuration Project

  • Java 8
  • Maven 3.6.0
  • Spring 5.2.0.RELEASE
  • Eclipse

1. Create a Maven Project and Add Dependency

In Eclipse, create a maven project and add the following dependency.

File : pom.xml

<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.learnfromexamples.springcore</groupId>
	<artifactId>springcore-XMLConfiguration</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

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

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<spring.version>5.2.0.RELEASE</spring.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${spring.version}</version>
		</dependency>
		
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>

2. Spring Bean (Java Modal Class)

Create a plain Java Class which is POJO class.

File : Employee.java


package com.learnfromexamples.springcore.modal;

public class Employee {
	private String empName;

	public String getEmpName() {
		return empName;
	}

	public void setEmpName(String empName) {
		this.empName = empName;
	}

	@Override
	public String toString() {
		return "Employee [empName=" + empName + "]";
	}
	
}

 

3. Spring Bean Configuration File

Create a XML configuration file by specifying the bean which needs to be created and dependency which needs to be injected. This file should be created in src/main/resources folder

File : beans1.xml

 

<?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.xsd">

	<bean id="employee" class="com.learnfromexamples.springcore.modal.Employee">
		<property name="empName" value="Sridharan N"/>
	</bean>

</beans> 

 

4. Main Program

Create a Java File with main method and configuration file should be loaded using ClassPathXMLApplicationContent  .  Spring Container  (i.e ApplicationContext)will take care of creating an instance and injecting the dependency. Bean reference can be retrieved either by providing Class name or by providing the name which is specified in configuration file.

File : XMLConfiguration.java


package com.learnfromexamples.springcore.XMLConfiguration;

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

import com.learnfromexamples.springcore.modal.Employee;

public class XMLConfiguration {

	public static void main(String[] args) {

		@SuppressWarnings("resource")
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans1.xml");

		Employee employee = ctx.getBean(Employee.class);

		System.out.println(employee);
		
		// Getting the bean using name specified in the beans1.xml
		
		Employee emp = (Employee) ctx.getBean("employee");
		
		System.out.println(emp);
		
	}
}


4. Output

Employee [empName=Sridharan N]
Employee [empName=Sridharan N]

5. Source Code

Get the source code from the below git url.

Spring Hello World Example + XML Configuration

Spring Hello World Example

6. Reference

Spring Hello World

Introduction to Data Structure and Algorithms

Brief overview of Data Structures and Algorithms are explained with examples.

Data Structures and Algorithms

Data Structure – Efficient way of arranging the data which enables to solve a problem efficiently.

Example of Data Structure : Unsorted fruit basket.

Data Structures And Algorithms

Data Structure

Algorithms – Steps to be followed to solve a problem.

Example of Algorithm : Preparing a fruit salad.

Data Structure and Algorithms

Data Structure and Algorithms

 

Common Examples

Queue – To avoid rush in ticket counter or ATM counter.

Queue Data Structure

Queue Data Structure

Tree –  To identify the relationship between parent and child

Tree Data Structure

Tree Data Structure

Graph –  To find shortest distance between cities 

Graph Data Structure

Graph Data Structure

 

References

Data Structures

DS and Algorithms