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

Add a Comment

Your email address will not be published. Required fields are marked *