Today I’m going to share with you the injection form of @import involved in Pig4Cloud. Different forms of injection are used to maximize the simplicity of the architecture.

@import Imports a component

Look at the EnablePigxDynamicRoute annotation. When we need to start a dynamic data source, we just need to add this annotation to the main method.

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

Actually core is the introduction of DynamicRouteAutoConfiguration the configuration class, such has not been Spring scanning management

Let me write a 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"); }}Copy the code

Note that Dog does not add the declarative annotations above and injects a Bean with a full type name

ImportSelector interface

As the name implies, an imported selector is injected according to which matching is performed when the class introduced by @Import is an implementation of the ImportSelector interface

public class DogImportSelector implements ImportSelector { @Override public String[] selectImports(AnnotationMetadata importingClassMetadata) { ... Matching logic query out a pile of full class name return to inject new String [] {" com. Pig4cloud. Spring. Learn. Not. Dog "}; }}Copy the code

@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"); }}Copy the code

ImportBeanDefinitionRegistrar

Is introduced when @ Import ImportBeanDefinitionRegistrar interface implementation class, automatically introduce registerBeanDefinitions Bean definition

With pig resources server configuration Settings, automatic introduced a PigxResourceServerConfigurerAdapter classes, and bean name as 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); }}Copy the code

This means that EnablePigxResourceServer annotations can be used to enable the OAuth resource client operation class encapsulated by Pig4Cloud, which is also the source code entry

@Import({PigxSecurityBeanDefinitionRegistrar.class})
public @interface EnablePigxResourceServer {

}Copy the code