DEV Community

Willian Ferreira Moya
Willian Ferreira Moya

Posted on • Originally published at springmasteryhub.com

1

How to Use the @Import Annotation in Spring Framework

The @Import annotation allows you to specify which configurations your Spring application should load.

You can think of this annotation as your Java class's import statements. In this case, it’s going to import only classes with the @Configuration annotation. It’s not the exact concept, but you get the idea.

By using the @Import, you explicitly import configurations. This annotation is useful when you need specific configurations from a package that is not scanned by default for your application, and you don’t want to load all configurations in that package.

You can also use it to group some configurations and load that specific group.

Let’s see an example:

Let's create some configuration classes that we can import using @import:

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

@Configuration
public class DataSourceConfig {

    @Bean
    public DataSource dataSource() {
        return new DataSource("url", "username", "password");
    }
}

@Configuration
public class ServiceConfig {

    @Bean
    public MyService myService() {
        return new MyService();
    }
}

Enter fullscreen mode Exit fullscreen mode

However, importing all configurations can be exhausting. We can create groups (like categories), to group these configurations and use @import to achieve this.

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

@Configuration
@Import({DataSourceConfig.class, ServiceConfig.class})
public class AppConfig {
}

Enter fullscreen mode Exit fullscreen mode

Then when we are configuring our application, we can import the configuration groups using the @import annotation.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;

@SpringBootApplication
@Import(AppConfig.class)
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

Enter fullscreen mode Exit fullscreen mode

Now you know how to configure your Spring applications using the @import annotation.

If you like this topic, make sure to follow me. I’ll be explaining more about Spring annotations! Stay tuned!

Willian Moya (@WillianFMoya) / X (twitter.com)

Willian Ferreira Moya | LinkedIn

Top comments (0)

👋 Kindness is contagious

Explore this insightful write-up embraced by the inclusive DEV Community. Tech enthusiasts of all skill levels can contribute insights and expand our shared knowledge.

Spreading a simple "thank you" uplifts creators—let them know your thoughts in the discussion below!

At DEV, collaborative learning fuels growth and forges stronger connections. If this piece resonated with you, a brief note of thanks goes a long way.

Okay