Explain various flower activities injected by @ import in spring annotation in detail
Today, let's share the injection form of @ import involved in pig4cloud. Through different forms of injection, the architecture is concise to the greatest extent.
@Import imports a component
Look at the annotation enablepigxdynamicroute. When we need to start the 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 { }
The actual core is to introduce the configuration class dynamicrouteautoconfiguration, which is not managed by spring scanning
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"); } }
Note that dog does not add the above declarative annotation, but injects a bean with full type name
Importselector interface
As the name implies, when the class introduced by @ import is the implementation of the importselector interface, matching injection will be carried out according to this selector
public class DogImportSelector implements ImportSelector { @Override public String[] selectImports(AnnotationMetadata importingClassMetadata) { ... 匹配逻辑查询出来一堆要注入的全类名 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,"error dog bean"); } }
ImportBeanDeFinitionRegistrar
When @ import introduces the importbeandefinitionregister interface implementation class, it will automatically introduce the bean defined by registerbeandeffinitions
According to the resource server configuration settings of pig, a class of pigxresourceserverconfigueradapter is automatically introduced, and the bean name is 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); } }
This means that if you use the enablepigxresourceserver annotation, you can open the OAuth resource client operation class encapsulated in pig4cloud, which is also the entry of the source code
@Import({PigxSecurityBeanDeFinitionRegistrar.class}) public @interface EnablePigxResourceServer { }
The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.