アプリケーションでは、我々はIOCへのクラスの多くを注入します、必要がBeanに対応するクラスを取得する際には、IOCがクラスの容器に注入したが、使用しないで、時々ではありません、あなたは@importのコメントを使用する必要があります。
A. @import使用
はじめに1.1 @import
- 直接クラスに
- コンフィギュレーション・クラス、この時間またはImportSelector ImportBeanDefinitionRegistrarの達成するために必要でコンフィギュレーション導入することにより、マナー
@import輸入部品、全体のクラス名コンポーネントのid:注
1.2 @importを使用
1.2.1には、いくつかの豆を定義します
このとき、容器に注入されていませんIOC
@Data
public class SuperMan {
private String name;
private Integer age;
}
---------
@Data
public class Man {
private String userName;
private String email;
}
--------
@Data
public class Bule {
private Integer code;
}
1.2.2 @import注入
@Configuration
@Import({SuperMan.class, SuperSelector.class,MyDefinitionRegistrar.class})
public class MainConfig {
}
MyDefinitionRegistrar、SuperSelectorは、設定ファイルによるものです
public class MyDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata annotationMetadata,
BeanDefinitionRegistry beanDefinitionRegistry) {
boolean beanDefinition = beanDefinitionRegistry.containsBeanDefinition("com.javayh.api.bean.SuperMan");
if(beanDefinition){
RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(Bule.class);
beanDefinitionRegistry.registerBeanDefinition("bule",rootBeanDefinition);
// beanDefinitionRegistry.removeBeanDefinition("com.javayh.api.bean.Man");
}
}
}
public class SuperSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
return new String[]{
"com.javayh.api.bean.Man"
};
}
}
1.3検証
public class TestMain {
AnnotationConfigApplicationContext applicationContext =
new AnnotationConfigApplicationContext(MainConfig.class);
@Test
public void testImport(){
printBeans(applicationContext);
SuperMan bean = applicationContext.getBean(SuperMan.class);
Man man = applicationContext.getBean(Man.class);
Bule bule = applicationContext.getBean(Bule.class);
System.out.println(bean);
System.out.println(man);
System.out.println(bule);
}
private void printBeans(AnnotationConfigApplicationContext applicationContext){
String[] definitionNames = applicationContext.getBeanDefinitionNames();
for (String name : definitionNames) {
System.out.println(name);
}
}
}
図は次のとおりです。私たちはIOCに注入使用していたとき
、我々はコメントし@importことができ、またはオープンコードはコメント化されて、実行されています
あなたがコメントアウトコードを開くと、以下のエラーが発生するでしょう!
II。拡張
多くのSpringBoot基礎となるアプリケーションで@import、以下のように
、我々はソースコードを見て次
public class AutoConfigurationImportSelector
implements DeferredImportSelector,
BeanClassLoaderAware, ResourceLoaderAware,
BeanFactoryAware, EnvironmentAware, Ordered {
}
見つかった道具の多くを含むインターフェース、DeferredImportSelector
インポートセレクタはなく、DeferredImportSelector
また、継承されましたImportSelector
public interface ImportSelector {
String[] selectImports(AnnotationMetadata var1);
}
私はあなたの助けのために願って、ここに一時的にこの記事を共有
より多くの情報を受信するためにJavaの株式に焦点を当て
接触小さなシリーズを。マイクロ手紙:372 787 553は、お互いから学ぶことグループにあなたを取る
自由な情報を入手する権利を、マイクロ文字の左側の小さなシリーズに
- SpringCloudカスタムスキーマパッケージhttps://github.com/Dylan-haiji/javayh-platform
- コード学習のJavaデザインパターンhttps://github.com/Dylan-haiji/design-patternを
- SpringCloud学習コード:https://github.com/Dylan-haiji/javayh-cloud
- AlibabaCloud学習コード:https://github.com/Dylan-haiji/javayh-cloud-nacos
- SpringBoot + MyBatisの複数のデータソースを切り替える:https://github.com/Dylan-haiji/javayh-boot-data-soure
- Redisの、モンゴ、RabbitMQの、カフカの学習コード:https://github.com/Dylan-haiji/javayh-middleware
- SpringBoot + SpringSecurityカスタム学習ログインコードを実装:https://github.com/Dylan-haiji/javayh-distribution