The interface DeferredImportSelector extends ImportSelector.
The configuration class directly registered with the application context given preference over imported one. That means a bean of type T, configured in the main configuration will be used instead of a bean of the same type T from imported configuration. That applies to ImportSelector as well (See our last example). On the other hand, DeferredImportSelector's configurations are applied after all other configuration beans have been processed.
Let's rewrite out last example. We will only replace the implementing interface of MyImportSelector to DeferredImportSelector.
public class MyImportSelector implements DeferredImportSelector {
..
}
There's no other difference from the last example. This time we will have the following output:
from config 1
Complete Example
Implementing DeferredImportSelector
package com.logicbig.example;
import org.springframework.context.annotation.DeferredImportSelector;
import org.springframework.core.type.AnnotationMetadata;
public 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()};
}
}
}
MyConfig1
desc
package com.logicbig.example;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyConfig1 {
@Bean
AppBean appBean() {
return new AppBean("from config 1");
}
}
MyConfig2
desc
package com.logicbig.example;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyConfig2 {
@Bean
AppBean appBean() {
return new AppBean("from config 2");
}
}
MainConfig
desc
package com.logicbig.example;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import(MyImportSelector.class)
public class MainConfig {
@Bean
ClientBean clientBean() {
return new ClientBean();
}
@Bean
AppBean appBean() {
return new AppBean("from main config");
}
}
AppBean
desc
package com.logicbig.example;
public class AppBean {
private String message;
public AppBean(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
}
ClientBean
desc
package com.logicbig.example;
import org.springframework.beans.factory.annotation.Autowired;
public class ClientBean {
@Autowired
private AppBean appBean;
public void doSomething() {
System.out.println(appBean.getMessage());
}
}
Main class
package com.logicbig.example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
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();
}
}
Outputfrom config 1
Example ProjectDependencies and Technologies Used: - spring-context 6.2.12 (Spring Context)
Version Compatibility: 3.2.9.RELEASE - 6.2.12 Version compatibilities of spring-context with this example: Versions in green have been tested.
- JDK 25
- Maven 3.9.11
|