Spring Annotation Based Configuration

In previous post we have seen how to configure spring using XML configuration. Now in this example , we will see spring configuration using annotations –

Tools used to create below project – 

  1. Spring 5.0.7.RELEASE
  2. Maven 3.3
  3. Java 8
  4. Eclipse Neon.2

Github Link :   Download

Step 1: Create Simple Maven Project with Spring Configuration

Step 2: Configure maven dependencies

Below pom.xml file contains the dependencies required for spring project –

<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">





<!-- Spring 5 dependencies -->



Below project structure will be created for you by Eclipse .










Step 3:  Create City.java under com.myjavablog.beans package –

package com.myjavablog.beans;

* @author anupb
public class City {

String cityName;

public City(String cityName) {
this.cityName = cityName;

public String getcityName() {
return cityName;

public void setcityName(String cityName) {
this.cityName = cityName;


This is the bean file where we have getters and setters defined.

Step 4: Create AnnotationConfig .java file under com.myjavablog.config package-


package com.myjavablog.config;

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

import com.myjavablog.beans.City;

* @author anupb
public class AnnotationConfig {

public City getCountry()
return new City("Pune");

This is Annotation based configuration to configure City bean. Its similar to XML based configuration which we did in previous post. The same Configuration can be done in XML as below –

<div id="crayon-5b537011533c5704156605-2" class="crayon-line crayon-striped-line"><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
<bean id="countryObj" class="org.arpit.java2blog.Country" >
<property name="countryName" value="India"/>

Step 5: Create a SpringJavaBasedConfigMain.java under com.myjavablog package –

package com.myjavablog;

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

import com.myjavablog.beans.City;
import com.myjavablog.config.AnnotationConfig;

* Hello world!
public class SpringJavaBasedConfigMain {
public static void main(String[] args) {

ApplicationContext appContext = new AnnotationConfigApplicationContext(AnnotationConfig.class);
City cityObj = (City) appContext.getBean("cityObj");
String cityName = cityObj.getcityName();

System.out.println("City name: " + cityName);

We have created ApplicationContext which actually loads bean definitions from annotation configuration i.e. AnnotationConfig.java

Step 6: Clean and install project

Now run the maven clean install phase as below –

Command:  mvn clean install


Right click on project -> Debug As -> Maven build . Below popup will come up –










Step 7: Run the spring application as Java Application










City name: Pune

2 thoughts on “Spring Annotation Based Configuration”

  1. ApplicationContext appContext = new AnnotationConfigApplicationContext(AppConfig.class);

    I think, above line you need to change to below:

    ApplicationContext appContext = new AnnotationConfigApplicationContext(AnnotationConfig.class);

    • Yes Sanjeev , Your observation is correct!!!
      I was playing around with different configurations so that it could be the reason i might have included wrong configuration.

      Thanks for pointing it out.


Leave a Comment