Spring AnnotationConfigApplicationContext

last modified October 18, 2023

Spring AnnotationConfigApplicationContext tutorial shows how to use AnnotationConfigApplicationContext in a Spring application.

Spring is a popular Java application framework.


AnnotationConfigApplicationContext is a standalone application context which accepts annotated classes as input. For instance, @Configuration or @Component. Beans can be looked up with scan or registered with register.

Spring AnnotationConfigApplicationContext example

The following example uses AnnotationConfigApplicationContext to build a standalone Spring application. It has one Spring bean--DateTimeService--, which is located with scan.

│   ├───java
│   │   └───com
│   │       └───zetcode
│   │           │   Application.java
│   │           └───bean
│   │                   DateTimeService.java
│   └───resources
│           logback.xml

This is the project structure.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"










This is the Maven build file for our Spring application.

<?xml version="1.0" encoding="UTF-8"?>
    <logger name="org.springframework" level="ERROR"/>
    <logger name="com.zetcode" level="INFO"/>

    <appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
            <Pattern>%d{HH:mm:ss.SSS} %blue(%-5level) %magenta(%logger{36}) - %msg %n

        <level value="INFO" />
        <appender-ref ref="consoleAppender" />

This is the Logback configuration file.

package com.zetcode.bean;

import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class DateTimeService {

    public LocalDate getDate() {

        return LocalDate.now();

    public LocalTime getTime() {

        return LocalTime.now();

    public LocalDateTime getDateTime() {

        return LocalDateTime.now();

The DateTimeService is a service class that provides data and time services. It is decorated with @Service stereotype, which causes it to be detected by the scanning process.

package com.zetcode;

import com.zetcode.bean.DateTimeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;

public class Application {

    private static final Logger logger = LoggerFactory.getLogger(Application.class);

    private DateTimeService dateTimeService;

    public static void main(String[] args) {

        var ctx = new AnnotationConfigApplicationContext();

        var bean = ctx.getBean(Application.class);


    public void run() {

        logger.info("Current time: {}", dateTimeService.getTime());
        logger.info("Current date: {}", dateTimeService.getDate());
        logger.info("Current datetime: {}", dateTimeService.getDateTime());

We set up the application and inject the DateTimeService. We call all three service methods.

public class Application {

The Application is also decorated with a stereotype, this time @Component. It will also be detected by Spring. We need to call its run method to go outside the static context.

private DateTimeService dateTimeService;    

The service class is injected with @Autowired.

var ctx = new AnnotationConfigApplicationContext();

A new AnnotationConfigApplicationContext is created. The scan method scans the com.zetcode package and its subpackages for annotated classes to generate beans. We need to call the refresh method to finish the process.

public void run() {

    logger.info("Current time: {}", dateTimeService.getTime());
    logger.info("Current date: {}", dateTimeService.getDate());
    logger.info("Current datetime: {}", dateTimeService.getDateTime());

We get the current date, time, and datetime.

$ mvn package
$ mvn -q exec:java
19:25:12.842 INFO  com.zetcode.Application - Current time: 19:25:12.842639200
19:25:12.842 INFO  com.zetcode.Application - Current date: 2019-01-05
19:25:12.842 INFO  com.zetcode.Application - Current datetime: 2019-01-05T19:25:12.842639200

We run the application.

In this article we have used AnnotationConfigApplicationContext to create a new standalone Spring application.


My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all Spring tutorials.