@ComponentScan: Scans packages for components annotated with @Component, @Service, @Repository, etc. see our tutorials on ComponentScan here.
ImportSelector: Programmatically selects which configuration classes to import based on conditions. see our previous tutorials on ImportSelector.
An ImportSelector can be used to conditionally import @Configuration classes that itself contains a @ComponentScan annotation. This allows you to select component scanning for specific packages based on runtime conditions.
Example
Implementing ImportSelector
package com.logicbig.example;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;
public class MyImportSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
String prop = System.getProperty("myProp");
if ("someValue".equals(prop)) {
return new String[]{MyConfig2.class.getName()};
} else {
return new String[]{MyConfig1.class.getName()};
}
}
}
Configurations to select
package com.logicbig.example;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.logicbig.example.service1")
public class MyConfig1 {
}
package com.logicbig.example;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.logicbig.example.service2")
public class MyConfig2 {
}
MainConfig
package com.logicbig.example;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@ComponentScan("com.logicbig.example.main")
@Import(MyImportSelector.class)
public class MainConfig {
}
AppService
package com.logicbig.example;
public interface AppService {
void doSomething();
}
AppService1
package com.logicbig.example.service1;
import com.logicbig.example.AppService;
import org.springframework.stereotype.Service;
@Service
public class AppService1 implements AppService {
@Override
public void doSomething() {
System.out.println("In AppService1");
}
}
AppService2
package com.logicbig.example.service2;
import com.logicbig.example.AppService;
import org.springframework.stereotype.Service;
@Service
public class AppService2 implements AppService {
public void doSomething() {
System.out.println("in bean 2");
}
}
ClientBean
package com.logicbig.example.main;
import com.logicbig.example.AppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class ClientBean {
@Autowired
private AppService appBean;
public void doSomething() {
appBean.doSomething();
}
}
Main class
package com.logicbig.example;
import com.logicbig.example.main.ClientBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class ImportSelectorWithComponentScanExample {
public static void main(String[] args) {
System.setProperty("myProp", "someValue");
ApplicationContext context =
new AnnotationConfigApplicationContext(
MainConfig.class);
ClientBean bean = context.getBean(ClientBean.class);
bean.doSomething();
}
}
Outputin bean 2
Example ProjectDependencies and Technologies Used: - spring-context 6.2.12 (Spring Context)
Version Compatibility: 3.2.3.RELEASE - 6.2.12 Version compatibilities of spring-context with this example: Versions in green have been tested.
- JDK 25
- Maven 3.9.11
|