Spring-Smart-DI: Dynamic @Autowired Implementation Switching at Runtime
This article introduces spring-smart-di, a library that extends Spring's @Autowired with @AutowiredProxySPI to dynamically switch service implementations (e.g., SMS providers) via configuration without restart, using proxies that resolve the active bean at each invocation.
Problem: Dynamic Service Provider Switching
In real-world systems, a single feature often integrates multiple service providers (e.g., SMS vendors) for high availability and cost optimization. The traditional approach requires manually fetching the active provider key from a config source (database, Nacos, etc.), looking up the corresponding Spring bean, and invoking it on every call — leading to repetitive boilerplate.
Solution: spring-smart-di
spring-smart-di(version 0.2.0, io.github.burukeyou:spring-smart-di-all) extends Spring's @Autowired with custom injection logic. It provides two key annotations: @SmartAutowired and @AutowiredProxySPI. This article focuses on @AutowiredProxySPI.
2.1 Add Dependency
<dependency>
<groupId>io.github.burukeyou</groupId>
<artifactId>spring-smart-di-all</artifactId>
<version>0.2.0</version>
</dependency>2.2 Enable Functionality
Annotate a configuration class with @EnableSmartDI to activate the extension.
2.3 Define Configuration Point with @EnvironmentProxySPI
Place @EnvironmentProxySPI on the service interface, specifying a Spring Environment property placeholder (e.g., ${sms.impl}). Implementation classes are marked with @BeanAliasName (alias) and @Component.
@EnvironmentProxySPI("${sms.impl}")
public interface SmsService {}
@BeanAliasName("某腾短信服务")
@Component
public class ASmsService implements SmsService {}
@BeanAliasName("某移短信服务")
@Component
public class BSmsService implements SmsService {}2.4 Configure Active Provider
In application.yml, set the property to one of the aliases (or the @Component value or fully qualified class name):
sms:
impl: 某移短信服务2.5 Inject with @AutowiredProxySPI
Use @AutowiredProxySPI exactly like @Autowired:
@AutowiredProxySPI
private SmsService smsService;The injected object is a proxy. On each method call, it reads the current ${sms.impl} value, resolves the matching bean, and delegates — enabling real-time switching without restart.
2.6 Custom Configuration Sources (e.g., Database)
For non-Environment sources, create a custom annotation meta-annotated with @ProxySPI pointing to an AnnotationProxyFactory implementation.
@Inherited
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@ProxySPI(DbProxyFactory.class)
public @interface DBProxySPI {
String value();
}
@Component
public class DbProxyFactory implements AnnotationProxyFactory<DBProxySPI> {
@Autowired
private SysConfigMapper sysConfigDao;
@Override
public Object getProxy(Class<?> targetClass, DBProxySPI spi) {
String configName = sysConfigDao.getConfig(spi.value());
return springContext.getBean(configName);
}
}
@DBProxySPI("${sms.impl}")
public interface SmsService {}This mechanism lets developers plug in arbitrary configuration backends while keeping the injection site unchanged.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
