@Import注解源码分析

master
xuchengsheng 2023-09-28 17:12:13 +08:00
parent c8b3fc4986
commit 4851a3d3dc
12 changed files with 409 additions and 1254 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
package com.xcs.spring;
import com.xcs.spring.config.MyConfig;
import com.xcs.spring.config.MyConfiguration;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
@ -10,7 +10,7 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
public class ImportApplication {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration.class);
for (String beanDefinitionName : context.getBeanDefinitionNames()) {
System.out.println("beanName = " + beanDefinitionName);
}

View File

@ -6,16 +6,4 @@ package com.xcs.spring.bean;
**/
public class MyBean {
private String describe;
public MyBean(String describe) {
this.describe = describe;
}
@Override
public String toString() {
return "MyBean{" +
"describe='" + describe + '\'' +
'}';
}
}

View File

@ -1,8 +0,0 @@
package com.xcs.spring.bean;
/**
* @author xcs
* @date 20230828 1113
**/
public class MyBeanD {
}

View File

@ -1,14 +0,0 @@
package com.xcs.spring.bean;
/**
* @author xcs
* @date 20230828 1048
**/
public class User {
private String name;
public User(String name) {
this.name = name;
}
}

View File

@ -1,27 +0,0 @@
package com.xcs.spring.config;
import com.xcs.spring.service.MyService;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* @author xcs
* @date 20230807 1625
**/
@Configuration
// 导入常规的 @Configuration 类
// @Import(UserConfig.class)
// 导入普通的组件类
// @Import(MyService.class)
// 使用ImportSelector
// @Import(CustomImportSelector.class)
// 使用 ImportBeanDefinitionRegistrar
// @Import(CustomRegistrar.class)
@Import({UserConfig.class, MyService.class, CustomImportSelector.class, CustomDeferredImportSelector.class, CustomRegistrar.class})
public class MyConfig {
}

View File

@ -0,0 +1,15 @@
package com.xcs.spring.config;
import com.xcs.spring.bean.MyBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* @author xcs
* @date 20230807 1625
**/
@Configuration
@Import({MyBean.class, MyImportSelector.class, MyDeferredImportSelector.class, MyImportBeanDefinitionRegistrar.class})
public class MyConfiguration {
}

View File

@ -1,7 +1,6 @@
package com.xcs.spring.config;
import com.xcs.spring.bean.MyBeanA;
import com.xcs.spring.bean.MyBeanC;
import com.xcs.spring.bean.MyBeanB;
import org.springframework.context.annotation.DeferredImportSelector;
import org.springframework.core.type.AnnotationMetadata;
@ -9,10 +8,10 @@ import org.springframework.core.type.AnnotationMetadata;
* @author xcs
* @date 20230829 1108
**/
public class CustomDeferredImportSelector implements DeferredImportSelector {
public class MyDeferredImportSelector implements DeferredImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{MyBeanC.class.getName()};
return new String[]{MyBeanB.class.getName()};
}
}

View File

@ -1,6 +1,6 @@
package com.xcs.spring.config;
import com.xcs.spring.bean.MyBeanB;
import com.xcs.spring.bean.MyBeanC;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
@ -10,10 +10,10 @@ import org.springframework.core.type.AnnotationMetadata;
* @author xcs
* @date 20230828 1117
**/
public class CustomRegistrar implements ImportBeanDefinitionRegistrar {
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
RootBeanDefinition beanDefinition = new RootBeanDefinition(MyBeanB.class);
registry.registerBeanDefinition("myBeanB", beanDefinition);
RootBeanDefinition beanDefinition = new RootBeanDefinition(MyBeanC.class);
registry.registerBeanDefinition(MyBeanC.class.getName(), beanDefinition);
}
}

View File

@ -8,7 +8,7 @@ import org.springframework.core.type.AnnotationMetadata;
* @author xcs
* @date 20230828 1112
**/
public class CustomImportSelector implements ImportSelector {
public class MyImportSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{MyBeanA.class.getName()};

View File

@ -1,18 +0,0 @@
package com.xcs.spring.config;
import com.xcs.spring.bean.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author xcs
* @date 20230828 1048
**/
@Configuration
public class UserConfig {
@Bean
public User user(){
return new User("xcs");
}
}

View File

@ -1,20 +0,0 @@
package com.xcs.spring.service;
import com.xcs.spring.bean.MyBeanD;
import org.springframework.context.annotation.Bean;
/**
* @author xcs
* @date 20230828 1100
**/
public class MyService {
@Bean
public MyBeanD myBeanD(){
return new MyBeanD();
}
public String getInfo(){
return "MyService info";
}
}