Home  >  Spring Core

Example of @Import in Spring

By Arvind Rai, April 05, 2013 | Modified on June 17, 2016
On this page we will provide example of @Import in spring. @Import annotation imports a configuration class to another configuration class. It works with @Configuration annotation. It works same as <import/> in XML configuration. The advantage of importing the configuration class is that we need to handle with only one configuration class. The syntax of importing the configuration class using @Import is as given below.
@Import(AppConfTwo.class)
 
When more than one configuration class needs to be imported, we do as follows.
@Import({EmpConf.class, AddConf.class}) 
 
Here order of configuration classes does not matter. Now find the examples.


@Import Simple Example

Here we will provide a simple example of @Import annotation. We have two configuration files. Each of them is creating a bean. There is no dependency injection. 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. 

@Import with Dependency Injection

In real time scenario there can be dependency injection of bean to another bean and these beans can be declared in different configuration files. Spring @Import handles these situations by passing arguments to the method annotated by @Bean. This scenario can also be handled by @Autowired annotation. Here in the example we are creating three beans which are dependent on each other. We need to resolve it by dependency injection. 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
 

@Import with @Autowired

Now find the example using @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 follows.
 Varanasi 
 

References

Composing Java-based configurations

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us