Spring Boot import beans

last modified July 6, 2020

Spring Boot import beans tutorial shows how to import additional beans into application context of a Spring Boot application.

Spring Boot is a popular application framework for creating enterprise application in Java, Kotlin, or Groovy.

The @SpringBootApplication is a convenience annotation which allows to define autoconfiguration, component scanning, and define extra beans on the application class.

Spring beans are typically registered in the application context via component scanning in modern applications. But it is also possible to import beans with the @Import annotation or the SpringApplicationBuilder's sources method.

Spring Boot import beans example

In the following application, we turn off the component scanning and register the service bean with @Import and sources.

├── main
│   ├── java
│   │   └── com
│   │       └── zetcode
│   │           ├── Application.java
│   │           └── service
│   │               └── TimeService.java
│   └── resources
└── test
    └── java

This is the project structure of the Spring Boot application.

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










This is the Maven pom.xml file.

package com.zetcode.service;

import org.springframework.stereotype.Service;

import java.time.LocalTime;

public class TimeService {

    public LocalTime getTime() {

        var now = LocalTime.now();

        return now;

We have a simple service bean that returns the current local time.

package com.zetcode;

import com.zetcode.service.TimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

public class Application implements CommandLineRunner {

    public static void main(String[] args) {

        new SpringApplicationBuilder(Application.class)
//                .sources(TimeService.class)

    private TimeService timeService;

    public void run(String... args) throws Exception {


We intentionally did not use the @SpringBootApplication annotation and did not specify the @ComponentScan annotation either. In order to register and use the TimeService bean, we have to import it.

public class Application implements CommandLineRunner {

We import the TimeService bean into the application context.

	        new SpringApplicationBuilder(Application.class)
//                .sources(TimeService.class)

An alternative way to import the bean is to use the SpringApplicationBuilder's sources method. Since we have already imported the bean, the line is commented.

private TimeService timeService;

Now that the bean is registered, we can inject it into the field.


Finally, we call the getTime method of the bean.

$ mvn -q spring-boot:run

We run the application.

In this tutorial, we have shown how to import additional beans into the application context.

List Spring Boot tutorials.