Close

Spring Framework - DeferredImportSelector Examples

Spring Framework 

DeferredImportSelector applies configuration after all @Configuration beans have been processed. Following example shows difference between using ImportSelector and DeferredImportSelector.

package com.logicbig.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;
import org.springframework.core.type.AnnotationMetadata;

public class ImportSelectorExample {

public static void main (String[] args) {
System.setProperty("myProp", "someValue");

ApplicationContext context =
new AnnotationConfigApplicationContext(
MainConfig.class);
ClientBean bean = context.getBean(ClientBean.class);
bean.doSomething();
}

@Configuration
@Import(MyImportSelector.class)
public static class MainConfig {

@Bean
ClientBean clientBean () {
return new ClientBean();
}

@Bean
AppBean appBean () {
return new AppBean("from main config ");
}

}

public static class ClientBean {
@Autowired
private AppBean appBean;

public void doSomething () {
System.out.println(appBean.getMessage());
}
}

public static class MyImportSelector implements ImportSelector {

@Override
public String[] selectImports (AnnotationMetadata importingClassMetadata) {
String prop = System.getProperty("myProp");
if ("someValue".equals(prop)) {
return new String[]{MyConfig1.class.getName()};
} else {
return new String[]{MyConfig2.class.getName()};
}
}
}

public static class AppBean {
private String message;

public AppBean (String message) {
this.message = message;
}

public String getMessage () {
return message;
}
}

@Configuration
public static class MyConfig1 {
@Bean
AppBean appBean () {
return new AppBean("from config 1");
}
}

@Configuration
public static class MyConfig2 {
@Bean
AppBean appBean () {
return new AppBean("from config 2");
}
}
}

Output

from main config 
INFO: Overriding bean definition for bean 'appBean' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=com.logicbig.example.ImportSelectorExample$MyConfig1; factoryMethodName=appBean; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/logicbig/example/ImportSelectorExample$MyConfig1.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=importSelectorExample.MainConfig; factoryMethodName=appBean; initMethodName=null; destroyMethodName=(inferred); defined in com.logicbig.example.ImportSelectorExample$MainConfig]



package com.logicbig.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;
import org.springframework.core.type.AnnotationMetadata;

public class DeferredImportSelectorExample {

public static void main (String[] args) {
System.setProperty("myProp", "someValue");

ApplicationContext context =
new AnnotationConfigApplicationContext(
MainConfig.class);
ClientBean bean = context.getBean(ClientBean.class);
bean.doSomething();
}

@Configuration
@Import(MyImportSelector.class)
public static class MainConfig {

@Bean
ClientBean clientBean () {
return new ClientBean();
}

@Bean
AppBean appBean () {
return new AppBean("from main config");
}

}

public static class ClientBean {
@Autowired
private AppBean appBean;

public void doSomething () {
System.out.println(appBean.getMessage());
}

}


public static class MyImportSelector implements DeferredImportSelector {

@Override
public String[] selectImports (AnnotationMetadata importingClassMetadata) {
String prop = System.getProperty("myProp");
if ("someValue".equals(prop)) {
return new String[]{MyConfig1.class.getName()};
} else {
return new String[]{MyConfig2.class.getName()};
}
}
}

public static class AppBean {
private String message;

public AppBean (String message) {
this.message = message;
}

public String getMessage () {
return message;
}
}

@Configuration
public static class MyConfig1 {
@Bean
AppBean appBean () {
return new AppBean("from config 1");
}
}

@Configuration
public static class MyConfig2 {
@Bean
AppBean appBean () {
return new AppBean("from config 2");
}
}
}

Output

from config 1
INFO: Overriding bean definition for bean 'appBean' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=deferredImportSelectorExample.MainConfig; factoryMethodName=appBean; initMethodName=null; destroyMethodName=(inferred); defined in com.logicbig.example.DeferredImportSelectorExample$MainConfig] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=com.logicbig.example.DeferredImportSelectorExample$MyConfig1; factoryMethodName=appBean; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/logicbig/example/DeferredImportSelectorExample$MyConfig1.class]]
Original Post




See Also