February 16, 2022

Spring @Import Annotation

This post shows how to use @Import annotation in Spring. In an application you may have several bean definitions making your @Configuration class quite huge. Rather than putting all the bean definitions in a single configuration class you can break your configuration into several small configurations module wise. Then using Spring @Import annotation you can specify the configuration classes that are to be imported into a specific configuration class.

@Import annotation is similar to <import/> element in Spring XML which is used to import multiple XML resources. That way you can split a large Spring XML configuration file into smaller XMLs and then import those resources.

Same way @Import annotation in Spring allows for loading @Bean definitions from other configuration classes so you can split and group your configurations by modules or functionality which makes your code easily maintainable.

Spring @Import annotation example

As an example you can take a scenario where you need to do the following.

  1. Register a datasource in one configuration class.
  2. Register DAO classes in a separate configuration, import DBConfiguration class to load bean definition from that configuration class.
DBConfiguration class

This Java configuration class is used to provide DB related configuration. A datasource (Apache DBCP) is defined here by providing DB properties. Note that properties are stored in a properties file.

import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

public class DBConfiguration {
  private Environment env;

  public BasicDataSource devDataSource() {
    BasicDataSource ds = new BasicDataSource();

    return ds;
#DB configuration for dev
UserConfig class

There is another Java configuration class that has definitions for all the DAOs.

public class UserConfig {
  public UserDAO userDAO() {
    return new UserDAOImpl();

As you can see in UserConfig class @Import annotation is used to load the bean definitions from DBConfiguration class.

UserDAO interface
public interface UserDAO {
  public void getUsers();
UserDAOImpl class
public class UserDAOImpl implements UserDAO {
  BasicDataSource ds;
  public void getUsers() {
    System.out.println("In getUsers method, use DS to connect to DB");
    System.out.println("Driver class name- " + ds.getDriverClassName());
    System.out.println("DB User- " + ds.getUsername());
    System.out.println("DB URL- " + ds.getUrl());

Class with main method to run the example.

public class App {
  public static void main(String[] args) {
    AbstractApplicationContext context = new AnnotationConfigApplicationContext(UserConfig.class);
    UserDAO userDAO = context.getBean("userDAO", UserDAOImpl.class);

While instantiating the context only UserConfig class is specified rather than specifying both UserConfig and DBConfiguration classes. Use of @Import annotation with the UserConfig class ensures that the bean definitions from DBConfiguration are also loaded.

Loading multiple configuration classes using @Import

You can also specify more than one configuration classes with the @Import annotation. In that case classes have to passed as an array.

@Import({ConfigA.class, ConfigB.class, ConfigC.class})
public class ConfigD {

That's all for the topic Spring @Import Annotation. If something is missing or you have something to share about the topic please write a comment.

You may also like

No comments:

Post a Comment