Springboot中@Import的使用原理
点击上方蓝色字体,选择“标星公众号”
优质文章,第一时间送达
org.springframework.boot
spring-boot-starter-parent
1.5.6.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-maven-plugin
package com.entity;
public class User implements Serializable{
private String name;
private String age;
public User() {
this.name = "xiaochen";
this.age = "6";
}
}
package com.demo;
public class ImportUser implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[] {"com.entity.User"};
}
}
package com.demo;
@Import(ImportUser.class)
@Configuration
public class ImportConfiguration {}
package com.demo;
public class ImportDemo {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
//这里使用register()和scan()方法都可,用scan()方法就不用在配置类中使用@Configuration注解了。
// applicationContext.register(ImportConfiguration.class);
applicationContext.scan("com.demo");
applicationContext.refresh();
User user = applicationContext.getBean(User.class);
System.out.println(user);
}
}
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {}
@SpringBootApplication
public class ApplicationStart {
public static void main(String[] args) {
SpringApplication.run(ApplicationStart.class, args);
}
}
public ConfigurableApplicationContext run(String... args) {
refreshContext(context);
}
private void refreshContext(ConfigurableApplicationContext context) {
refresh(context);
}
protected void refresh(ApplicationContext applicationContext) {
((AbstractApplicationContext) applicationContext).refresh();
}
@Override
public void refresh() throws BeansException, IllegalStateException {
invokeBeanFactoryPostProcessors(beanFactory);
}
protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
}
public static void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory, List
beanFactoryPostProcessors) {
invokeBeanDefinitionRegistryPostProcessors(priorityOrderedPostProcessors, registry);
}
private static void invokeBeanDefinitionRegistryPostProcessors(
Collection extends BeanDefinitionRegistryPostProcessor> postProcessors, BeanDefinitionRegistry registry) {
for (BeanDefinitionRegistryPostProcessor postProcessor : postProcessors) {
postProcessor.postProcessBeanDefinitionRegistry(registry);
}
}
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
processConfigBeanDefinitions(registry);
}
public void processConfigBeanDefinitions(BeanDefinitionRegistry registry) {
// Parse each @Configuration class
ConfigurationClassParser parser = new ConfigurationClassParser(
this.metadataReaderFactory, this.problemReporter, this.environment,
this.resourceLoader, this.componentScanBeanNameGenerator, registry);
parser.parse(candidates);
}
public void parse(Set
configCandidates) {
processDeferredImportSelectors();
}
private void processDeferredImportSelectors() {
String[] imports = deferredImport.getImportSelector().selectImports(configClass.getMetadata());
processImports(configClass, asSourceClass(configClass), asSourceClasses(imports), false);
}
private void processDeferredImportSelectors() {
ListdeferredImports = this.deferredImportSelectors;
this.deferredImportSelectors = null;
Collections.sort(deferredImports, DEFERRED_IMPORT_COMPARATOR);
for (DeferredImportSelectorHolder deferredImport : deferredImports) {
ConfigurationClass configClass = deferredImport.getConfigurationClass();
}
}
private void processImports(ConfigurationClass configClass, SourceClass currentSourceClass,Collection
importCandidates,
boolean checkForCircularImports) throws IOException {
for (SourceClass candidate : importCandidates) {
if (candidate.isAssignable(ImportSelector.class)) {
// Candidate class is an ImportSelector -> delegate to it to determine imports
Class> candidateClass = candidate.loadClass();
//这里用于实例化ImportSelector的实现类
ImportSelector selector = BeanUtils.instantiateClass(candidateClass,ImportSelector.class);
ParserStrategyUtils.invokeAwareMethods(selector, this.environment,this.resourceLoader, this.registry);
if (this.deferredImportSelectors != null && selector instanceof DeferredImportSelector) {
this.deferredImportSelectors.add(new DeferredImportSelectorHolder(configClass, (DeferredImportSelector) selector));
}
}
}
}
protected final SourceClass doProcessConfigurationClass(ConfigurationClass configClass, SourceClass sourceClass) throws IOException {
// Process any @Import annotations
processImports(configClass, sourceClass, getImports(sourceClass), true);
}
private SetgetImports(SourceClass sourceClass) throws IOException {
Setimports = new LinkedHashSet ();
Setvisited = new LinkedHashSet ();
collectImports(sourceClass, imports, visited);
return imports;
}
private void collectImports(SourceClass sourceClass, Setimports,Set visited) throws IOException {
if (visited.add(sourceClass)) {
for (SourceClass annotation : sourceClass.getAnnotations()) {
String annName = annotation.getMetadata().getClassName();
if (!annName.startsWith("java") && !annName.equals(Import.class.getName())) {
collectImports(annotation, imports, visited);
}
}
imports.addAll(sourceClass.getAnnotationAttributes(Import.class.getName(), "value"));
}
}
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:
https://blog.csdn.net/qq_21299835/article/details/109695279
粉丝福利:实战springboot+CAS单点登录系统视频教程免费领取
???
?长按上方微信二维码 2 秒 即可获取资料
感谢点赞支持下哈
评论