Home  >  Spring

Spring @Import Example

By Arvind Rai, November 08, 2021
Spring @Import annotation imports one or more @Configuration classes. The @Import annotation is equivalent to <import> element in Spring XML.
If our application is using @ComponentScan, then all @Configuration classes will automatically be scanned and no need to use @Import annotation. But in some cases, we want to scan only selected @Configuration classes and in this case @Import is useful.
Find the code snippet to use @Import annotation.
@Configuration
@Import(AppConfTwo.class)
public class AppConfOne {
   ------
} 
We can import more than one configuration classes as following.
@Configuration
@Import({EmpConf.class, AddConf.class})
public class AppConf {
   ------
} 





Using @Import

Here we will provide a simple example of @Import annotation. We have two configuration files. Each of them is creating a bean. Find the example.
A.java
package com.concretepage;
public class A {
	public A(){
		System.out.println("Class A initialized.");
	}
        public void doWork(){
    	        System.out.println("A is doing work.");
        }
} 
B.java
package com.concretepage;
public class B {
	 public B(){
		System.out.println("Class B initialized.");
	 }
	 public void doWork(){
	        System.out.println("B is doing work.");
	 }
} 
AppConfOne.java
package com.concretepage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import(AppConfTwo.class)
public class AppConfOne {
	@Bean(name="b")	
	public B b(){
		return new B();
	}
} 
AppConfTwo.java
package com.concretepage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfTwo {
	@Bean(name="a")
	public A a(){
		return new A();
	}
} 
SpringDemo.java
package com.concretepage;
import java.sql.SQLException;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class SpringDemo {
	public static void main(String[] args) throws SQLException {
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
		ctx.register(AppConfOne.class);
		ctx.refresh();
		A a=(A)ctx.getBean("a");
		B b=(B)ctx.getBean("b");
		a.doWork();
		b.doWork();
		ctx.registerShutdownHook();
	}
} 
Output
Class A initialized.
Class B initialized.
A is doing work.
B is doing work. 




Using @Import with Dependency Injection

In real time scenario there can be dependency of a bean to another bean and these beans can be declared in different configuration files. Find the example.
Address.java
package com.concretepage;
public class Address {
	private String city;
	public Address(String city) {
		this.city = city;
	}
	public String getCity() {
		return city;
	}
} 
Employee.java
package com.concretepage;
public class Employee {
	private Address address;
	public Employee(Address address) {
		this.address = address;
	}
	public Address getAddress() {
		return address;
	}
} 
Company.java
package com.concretepage;
public class Company {
	private Employee ceo;
	public Company(Employee ceo) {
		this.ceo = ceo;
	}
	public Employee getCeo() {
		return ceo;
	}
} 
AddConf.java
package com.concretepage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AddConf {
	@Bean
	public Address getAddress(){
		return new Address("Varanasi");
	}
} 
EmpConf.java
package com.concretepage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class EmpConf {
	@Bean
	public Employee getEmployee(Address address){
		return new Employee(address);
	}
} 
CompConf.java
package com.concretepage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import({EmpConf.class, AddConf.class})
public class CompConf {
	@Bean
	public Company getCompany(Employee employee){
		return new Company(employee);
	}
} 
SpringDemo.java
package com.concretepage;
import java.sql.SQLException;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class SpringDemo {
	public static void main(String[] args) throws SQLException {
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
		ctx.register(CompConf.class);
		ctx.refresh();
		Company comp = ctx.getBean(Company.class);
		System.out.println(comp.getCeo().getAddress().getCity());
		ctx.registerShutdownHook();
	}
} 
Output
 Varanasi 

Using @Import with @Autowired

Find the example to use @Import annotation with @Autowired annotation.
EmpConf.java
package com.concretepage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class EmpConf {
	@Autowired
	private Address address;
	@Bean
	public Employee getEmployee(){
		return new Employee(address);
	}
} 
CompConf.java
package com.concretepage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import({EmpConf.class, AddConf.class})
public class CompConf {
	@Autowired
	private Employee employee;
	@Bean
	public Company getCompany(){
		return new Company(employee);
	}
} 
Output
Run SpringDemo class, we will get the output as following.
 Varanasi 

References

Using the @Import Annotation
Annotation Type Import

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI











©2022 concretepage.com | Privacy Policy | Contact Us