@Import注解源码分析
parent
c8b3fc4986
commit
4851a3d3dc
File diff suppressed because it is too large
Load Diff
|
@ -1,6 +1,6 @@
|
||||||
package com.xcs.spring;
|
package com.xcs.spring;
|
||||||
|
|
||||||
import com.xcs.spring.config.MyConfig;
|
import com.xcs.spring.config.MyConfiguration;
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -10,7 +10,7 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
|
||||||
public class ImportApplication {
|
public class ImportApplication {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
|
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration.class);
|
||||||
for (String beanDefinitionName : context.getBeanDefinitionNames()) {
|
for (String beanDefinitionName : context.getBeanDefinitionNames()) {
|
||||||
System.out.println("beanName = " + beanDefinitionName);
|
System.out.println("beanName = " + beanDefinitionName);
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,16 +6,4 @@ package com.xcs.spring.bean;
|
||||||
**/
|
**/
|
||||||
public class MyBean {
|
public class MyBean {
|
||||||
|
|
||||||
private String describe;
|
|
||||||
|
|
||||||
public MyBean(String describe) {
|
|
||||||
this.describe = describe;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "MyBean{" +
|
|
||||||
"describe='" + describe + '\'' +
|
|
||||||
'}';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +0,0 @@
|
||||||
package com.xcs.spring.bean;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author xcs
|
|
||||||
* @date 2023年08月28日 11时13分
|
|
||||||
**/
|
|
||||||
public class MyBeanD {
|
|
||||||
}
|
|
|
@ -1,14 +0,0 @@
|
||||||
package com.xcs.spring.bean;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author xcs
|
|
||||||
* @date 2023年08月28日 10时48分
|
|
||||||
**/
|
|
||||||
public class User {
|
|
||||||
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
public User(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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 2023年08月07日 16时25分
|
|
||||||
**/
|
|
||||||
@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 {
|
|
||||||
|
|
||||||
}
|
|
|
@ -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 2023年08月07日 16时25分
|
||||||
|
**/
|
||||||
|
@Configuration
|
||||||
|
@Import({MyBean.class, MyImportSelector.class, MyDeferredImportSelector.class, MyImportBeanDefinitionRegistrar.class})
|
||||||
|
public class MyConfiguration {
|
||||||
|
|
||||||
|
}
|
|
@ -1,7 +1,6 @@
|
||||||
package com.xcs.spring.config;
|
package com.xcs.spring.config;
|
||||||
|
|
||||||
import com.xcs.spring.bean.MyBeanA;
|
import com.xcs.spring.bean.MyBeanB;
|
||||||
import com.xcs.spring.bean.MyBeanC;
|
|
||||||
import org.springframework.context.annotation.DeferredImportSelector;
|
import org.springframework.context.annotation.DeferredImportSelector;
|
||||||
import org.springframework.core.type.AnnotationMetadata;
|
import org.springframework.core.type.AnnotationMetadata;
|
||||||
|
|
||||||
|
@ -9,10 +8,10 @@ import org.springframework.core.type.AnnotationMetadata;
|
||||||
* @author xcs
|
* @author xcs
|
||||||
* @date 2023年08月29日 11时08分
|
* @date 2023年08月29日 11时08分
|
||||||
**/
|
**/
|
||||||
public class CustomDeferredImportSelector implements DeferredImportSelector {
|
public class MyDeferredImportSelector implements DeferredImportSelector {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
|
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
|
||||||
return new String[]{MyBeanC.class.getName()};
|
return new String[]{MyBeanB.class.getName()};
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
package com.xcs.spring.config;
|
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.BeanDefinitionRegistry;
|
||||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
||||||
|
@ -10,10 +10,10 @@ import org.springframework.core.type.AnnotationMetadata;
|
||||||
* @author xcs
|
* @author xcs
|
||||||
* @date 2023年08月28日 11时17分
|
* @date 2023年08月28日 11时17分
|
||||||
**/
|
**/
|
||||||
public class CustomRegistrar implements ImportBeanDefinitionRegistrar {
|
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
|
||||||
@Override
|
@Override
|
||||||
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
|
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
|
||||||
RootBeanDefinition beanDefinition = new RootBeanDefinition(MyBeanB.class);
|
RootBeanDefinition beanDefinition = new RootBeanDefinition(MyBeanC.class);
|
||||||
registry.registerBeanDefinition("myBeanB", beanDefinition);
|
registry.registerBeanDefinition(MyBeanC.class.getName(), beanDefinition);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -8,7 +8,7 @@ import org.springframework.core.type.AnnotationMetadata;
|
||||||
* @author xcs
|
* @author xcs
|
||||||
* @date 2023年08月28日 11时12分
|
* @date 2023年08月28日 11时12分
|
||||||
**/
|
**/
|
||||||
public class CustomImportSelector implements ImportSelector {
|
public class MyImportSelector implements ImportSelector {
|
||||||
@Override
|
@Override
|
||||||
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
|
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
|
||||||
return new String[]{MyBeanA.class.getName()};
|
return new String[]{MyBeanA.class.getName()};
|
|
@ -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 2023年08月28日 10时48分
|
|
||||||
**/
|
|
||||||
@Configuration
|
|
||||||
public class UserConfig {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public User user(){
|
|
||||||
return new User("xcs");
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,20 +0,0 @@
|
||||||
package com.xcs.spring.service;
|
|
||||||
|
|
||||||
import com.xcs.spring.bean.MyBeanD;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author xcs
|
|
||||||
* @date 2023年08月28日 11时00分
|
|
||||||
**/
|
|
||||||
public class MyService {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public MyBeanD myBeanD(){
|
|
||||||
return new MyBeanD();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getInfo(){
|
|
||||||
return "MyService info";
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue