ZetCode

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.

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

This is the project structure of the Spring Boot application.

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.zetcode</groupId>
    <artifactId>SpringBootImportBeans</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>

    <properties>
        <java.version>13</java.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
    </parent>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

This is the Maven pom.xml file.

com/zetcode/service/TimeService.java
package com.zetcode.service;

import org.springframework.stereotype.Service;

import java.time.LocalTime;

@Service
public class TimeService {

    public LocalTime getTime() {

        var now = LocalTime.now();

        return now;
    }
}

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

com/zetcode/Application.java
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;

@EnableAutoConfiguration
@Configuration
@Import(value=TimeService.class)
public class Application implements CommandLineRunner {

    public static void main(String[] args) {

        new SpringApplicationBuilder(Application.class)
//                .sources(TimeService.class)
                .build()
                .run(args);
    }

    @Autowired
    private TimeService timeService;

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

        System.out.println(timeService.getTime());
    }
}

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.

@EnableAutoConfiguration
@Configuration
@Import(value=TimeService.class)
public class Application implements CommandLineRunner {

We import the TimeService bean into the application context.

	        new SpringApplicationBuilder(Application.class)
//                .sources(TimeService.class)
                .build()
                .run(args);

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.

@Autowired
private TimeService timeService;

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

System.out.println(timeService.getTime());

Finally, we call the getTime method of the bean.

$ mvn -q spring-boot:run
...
15:27:10.838441

We run the application.

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

List Spring Boot tutorials.