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;

@Configuration("dbConfig")
@PropertySource(value="classpath:properties/db.properties")
public class DBConfiguration {
  @Autowired
  private Environment env;

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

    ds.setDriverClassName(env.getProperty("db.dev.driver_class_name"));
    ds.setUrl(env.getProperty("db.dev.url"));
    ds.setUsername(env.getProperty("db.dev.user"));
    ds.setPassword(env.getProperty("db.dev.password"));
    return ds;
  }
}
properties/db.properties
#DB configuration for dev
db.dev.url=jdbc:oracle:thin:@localhost:1521/XEPDB1
db.dev.user=test
db.dev.password=test
db.dev.driver_class_name=oracle.jdbc.driver.OracleDriver
UserConfig class

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

@Configuration
@Import(DBConfiguration.class)
public class UserConfig {
  @Bean
  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 {
  @Autowired
  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);
    userDAO.getUsers();
    context.close();
  }
}

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.

@Configuration
@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