@Import annotation will be useful if we have multiple spring bean configurations for different modules like Audit ,Transaction etc in a project . In such scenario it is better to import all this configurations into a single configuration class so that there wont be any confusion on creating the beans by referring multiple configuration classes. @Import annotation in spring allows for loading bean definitions from another configuration class
It is very easy to import multiple configuration classes into a single application configuration. Lets understand the usage of @Import with the below example code.
Lets create two bean classes Audit and Transaction
1 2 3 4 5 6 |
public class Audit { Audit(){ System.out.println("Inside Audit constructor"); } } |
1 2 3 4 5 |
public class Transaction { public Transaction() { System.out.println("Inside Transaction constructor"); } } |
Below class is to configure Audit related beans, There could be multiple beans . In our example lets consider only one bean called audit.
1 2 3 4 5 6 7 8 9 10 |
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AuditConfig { @Bean(name="audit") public Audit audit() { return new Audit(); } } |
The same way lets create another configuration class for configuring Transaction related beans.
1 2 3 4 5 6 7 8 9 10 11 12 |
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class TransactionConfig { @Bean(name="transaction") public Transaction transaction() { return new Transaction(); } } |
As of now we have two separate configuration files for Audit and Transaction . It will be difficult to manage if we are not consolidating all the different configuration classes into one. Here comes the use of @Import annotation .
In the below class ApplicationConfig we will import both Audit and Transaction configurations by using @Import annotation.
1 2 3 4 5 6 7 8 |
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @Configuration @Import({AuditConfig.class,TransactionConfig.class}) public class ApplicationConfig { } |
Below program is to test the import configuration.
1 2 3 4 5 6 7 8 9 10 |
import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class ImportAnnotationDemo { public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(ApplicationConfig.class); Audit auditBean =(Audit) ctx.getBean("audit"); Transaction transactionBean =(Transaction) ctx.getBean("transaction"); } } |
Output :
Jun 06, 2017 4:19:56 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2c634b: startup date [Tue Jun 06 16:19:56 IST 2017]; root of context hierarchy
Inside Audit constructor
Inside Transaction constructor
Here you can notice that
- We are creating single application context by providing the ApplicationConfig.class .
- All the beans defined in Audit and transaction configurations classes are available with ApplicationConfig class.
Leave a Reply