Mastering @Import in Spring: Dynamic Routes and Custom Bean Registration

This article explains how the @Import annotation in the pig4cloud framework can be used to import components, implement dynamic routing, and register beans via ImportSelector and ImportBeanDefinitionRegistrar, providing concise architecture and easy activation of OAuth resource servers.

Java Architecture Diary
Java Architecture Diary
Java Architecture Diary
Mastering @Import in Spring: Dynamic Routes and Custom Bean Registration

Today I share the injection forms of @Import used in pig4cloud. Different injection methods help keep the architecture concise.

Importing a Component with @Import

The EnablePigxDynamicRoute annotation enables dynamic data sources; simply add it to the main method.

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(DynamicRouteAutoConfiguration.class)
public @interface EnablePigxDynamicRoute {
}

The core is importing the DynamicRouteAutoConfiguration class, which is not managed by Spring's component scan.

Simple example:

public class Dog {
}

@Import({Dog.class})
@SpringBootApplication
public class SpringLearnApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SpringLearnApplication.class, args);
        Assert.isTrue(context.containsBean("com.pig4cloud.spring.learn.demo1.Dog"), "error dog bean");
    }
}

Note that Dog does not have the declarative annotation; it is injected as a bean by its full class name.

ImportSelector Interface

When @Import references a class that implements ImportSelector, the selector determines which classes to import.

public class DogImportSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        // matching logic returns class names to import
        return new String[]{"com.pig4cloud.spring.learn.demo1.Dog"};
    }
}
@Import({DogImportSelector.class})
@SpringBootApplication
public class SpringLearnApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SpringLearnApplication.class, args);
        Assert.isTrue(context.containsBean("com.pig4cloud.spring.learn.demo1.Dog"), "error dog bean");
    }
}

ImportBeanDefinitionRegistrar

When @Import references a class that implements ImportBeanDefinitionRegistrar, the beans defined in registerBeanDefinitions are automatically registered.

Using pig's resource server configuration, it automatically registers a PigxResourceServerConfigurerAdapter bean named resourceServerConfigurerAdapter.

public class PigxSecurityBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
        GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
        beanDefinition.setBeanClass(PigxResourceServerConfigurerAdapter.class);
        registry.registerBeanDefinition(SecurityConstants.RESOURCE_SERVER_CONFIGURER, beanDefinition);
    }
}

Thus, adding the EnablePigxResourceServer annotation enables pig4cloud's packaged OAuth resource client operations, serving as the entry point of the source code.

@Import({PigxSecurityBeanDefinitionRegistrar.class})
public @interface EnablePigxResourceServer {
}
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.

Spring BootImportImportBeanDefinitionRegistrarImportSelectorDynamic Route
Java Architecture Diary
Written by

Java Architecture Diary

Committed to sharing original, high‑quality technical articles; no fluff or promotional content.

0 followers
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.