Skip to content
Spring-Smart-DI: Dynamic Provider Switching Without Restart Using @AutowiredProxySPI

Spring-Smart-DI: Dynamic Provider Switching Without Restart Using @AutowiredProxySPI

This tutorial demonstrates how spring-smart-di's @AutowiredProxySPI annotation enables dynamic switching between service provider implementations by injecting a proxy that resolves the active implementation from configuration at runtime, eliminating manual bean lookups and allowing zero-restart changes via @EnvironmentProxySPI or custom config sources like databases.

IoT Full-Stack Technology
IoT Full-Stack Technology
IoT Full-Stack Technology

1. Introduction to spring-smart-di

spring-smart-di is an innovative extension of Spring's @Autowired annotation that provides custom autowiring logic. It currently implements two key annotations: @SmartAutowired and @AutowiredProxySPI. This article focuses on using @AutowiredProxySPI to achieve dynamic switching of service providers.

2. Quick Start

2.1 Add Dependency

Add the following Maven dependency to your pom.xml:

<dependency>
    <groupId>io.github.burukeyou</groupId>
    <artifactId>spring-smart-di-all</artifactId>
    <version>0.2.0</version>
</dependency>

2.2 Enable Functionality

Annotate a Spring configuration class with @EnableSmartDI to activate spring-smart-di features.

2.3 Using @EnvironmentProxySPI

@EnvironmentProxySPI marks a configuration point that defines how to retrieve the concrete implementation. For example, with two SMS providers, annotate the interface to read the active provider from the environment property ${sms.impl}. Implementation classes are distinguished using @BeanAliasName.

@EnvironmentProxySPI("${sms.impl}")
public interface SmsService {
}

// Alias for first provider
@BeanAliasName("某腾短信服务")
@Component
public class ASmsService implements SmsService {
}

// Alias for second provider
@BeanAliasName("某移短信服务")
@Component
public class BSmsService implements SmsService {
}

2.4 Configure Active Provider

In your configuration file (YAML), set the active provider using the alias, component name, or fully qualified class name:

sms:
  impl: 某移短信服务

2.5 Inject with @AutowiredProxySPI

Use @AutowiredProxySPI just like @Autowired. The injected object is a proxy that resolves the current implementation on each invocation, so configuration changes take effect immediately without restart.

@AutowiredProxySPI
private SmsService smsService;

2.6 Define Custom Configuration Points

@EnvironmentProxySPI reads from environment variables. For custom sources (e.g., database), create a custom annotation annotated with @ProxySPI specifying an AnnotationProxyFactory implementation. The factory's getProxy method receives the annotation and returns the appropriate bean.

Example: a DBProxySPI annotation that fetches the implementation name from a database via SysConfigMapper.

@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 approach provides a clean, efficient way to handle dynamic provider switching with flexible configuration sources.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

proxy patternSpring Frameworkspring-smart-diconfiguration-driven@AutowiredProxySPIdynamic dependency injection@EnvironmentProxySPIservice-provider-switching
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.