[スプリング]スプリング5プログラミング入門-5章素子スキャン
12595 ワード
エレメントスキャンは、スプリングがクラスを直接検索して空に登録する機能です.必要なクラスを空に登録でき、設定クラスに空に登録する必要がないため、構成部品スキャン機能を使用すると、設定コードを大幅に削減できます.
@Component Anotationスキャンターゲットの指定
@Component @Controller @Service @Repository @Aspect @Configuration @AsspectAnovationを除いて、残りの部分は実際に@Component Anovationの特殊な分析です.
スキャン構成部品による競合の処理
@Component Anotationスキャンターゲットの指定
MemberDao@Component
public class MemberDao {
...
}
コメント値が指定されていない場合は、クラス名の最初のルート文字を小文字に変更し、空の名前を使用します.
MemberInfoPrinter@Component("infoPrinter")
public class MemberListPrinter {
private MemberDao memberDao;
private MemberPrinter printer;
...
}
@Componentアニメーションのプロパティ値が与えられました.
スキャンを@ComponentScanプレゼンテーションに設定
@Component Anotationを持つクラスをスキャンしてSpringBinとして登録する場合は、設定クラスに@ComponentScan Anotationを適用する必要があります.
AppCtx@Configuration
@ComponentScan(basePackages = {"spring"})
public class AppCtx {
@Bean
@Qualifier("printer")
public MemberPrinter memberPrinter1() {
return new MemberPrinter();
}
@Bean
@Qualifier("summaryPrinter")
public MemberSummaryPrinter memberPrinter2() {
return new MemberSummaryPrinter();
}
@Bean
public VersionPrinter versionPrinter() {
VersionPrinter versionPrinter = new VersionPrinter();
versionPrinter.setMajorVersion(5);
versionPrinter.setMinorVersion(0);
return versionPrinter;
}
}
Springコンテナは@Componentコメント付きクラスを検索して空に登録するため、設定コードが減少します.
@ComponentScanで説明したbasePackagesプロパティの値は{"spring"}です.このプロパティは、スキャンするパッケージのリストを指定します.ここにはspringパケットとそのサブパケットのクラスをスキャンターゲットに設定するspring値が1つしか存在しません.スキャンターゲットに対応するクラスで、@Component Annotationを持つクラスのオブジェクトを作成し、空に登録します.
スキャンターゲットから除外または含む
正規式
@Configuration
@ComponentScan(basePackages = {"spring"},
excludeFilters = @Filter(type = FilterType.REGEX, pattern = "spring\\..*Dao"))
public class AppCtx {
...
}
このコードは@Filterアニメーションのtype属性値としてFilterTypeです.REGEXをあげましたこれは、正規表現を使用して除外オブジェクトを指定することを意味します.上記の設定では、「spring」を選択します.Daoで始まる正規表現が指定されているのでspring.MemberDaoクラスを構成部品スキャンターゲットから除外します.
Aspectj
@Configuration
@ComponentScan(basePackages = {"spring"},
excludeFilters = @Filter(type = FilterType.ASPECTJ, pattern = "spring.*Dao"))
public class AppCtx {
...
}
まず現在「spring.*Dao」の予測JモードはspringpackageのDaoで終わるタイプを指定しますAspectjweaverモジュールは、AspectJモードを実行するために依存オブジェクトに追加する必要があります.
よくせい
特定のノイズを伴うタイプは、構成部品ターゲットから除外することもできます.たとえば、@NoProductまたは@Manualbeanコメントを追加するクラスは、構成部品スキャンターゲットから除外したい場合があります.@Configuration
@ComponentScan(basePackages = {"spring"},
excludeFilters = @Filter(type = FilterType.ANNOTATION, classes = {NoProduct.class, ManualBean.class}))
public class AppCtx {
...
}
を選択します。
構成部品スキャンから特定のタイプまたはそのサブタイプを除外するには、ASSIGNABLE TYPEをFilterTypeとして使用します.@Configuration
@ComponentScan(basePackages = {"spring"},
excludeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = MemberDao.class))
public class AppCtx {
...
}
既定のスキャンターゲット
@Componentコメント付きクラスのみがコンポーネントスキャンターゲットに含まれるわけではありません.構成部品スイープオブジェクトには、次の注釈付きクラスが含まれます.
@Component
public class MemberDao {
...
}
@Component("infoPrinter")
public class MemberListPrinter {
private MemberDao memberDao;
private MemberPrinter printer;
...
}
@Component Anotationを持つクラスをスキャンしてSpringBinとして登録する場合は、設定クラスに@ComponentScan Anotationを適用する必要があります.
AppCtx
@Configuration
@ComponentScan(basePackages = {"spring"})
public class AppCtx {
@Bean
@Qualifier("printer")
public MemberPrinter memberPrinter1() {
return new MemberPrinter();
}
@Bean
@Qualifier("summaryPrinter")
public MemberSummaryPrinter memberPrinter2() {
return new MemberSummaryPrinter();
}
@Bean
public VersionPrinter versionPrinter() {
VersionPrinter versionPrinter = new VersionPrinter();
versionPrinter.setMajorVersion(5);
versionPrinter.setMinorVersion(0);
return versionPrinter;
}
}
Springコンテナは@Componentコメント付きクラスを検索して空に登録するため、設定コードが減少します.@ComponentScanで説明したbasePackagesプロパティの値は{"spring"}です.このプロパティは、スキャンするパッケージのリストを指定します.ここにはspringパケットとそのサブパケットのクラスをスキャンターゲットに設定するspring値が1つしか存在しません.スキャンターゲットに対応するクラスで、@Component Annotationを持つクラスのオブジェクトを作成し、空に登録します.
スキャンターゲットから除外または含む
正規式
@Configuration
@ComponentScan(basePackages = {"spring"},
excludeFilters = @Filter(type = FilterType.REGEX, pattern = "spring\\..*Dao"))
public class AppCtx {
...
}
このコードは@Filterアニメーションのtype属性値としてFilterTypeです.REGEXをあげましたこれは、正規表現を使用して除外オブジェクトを指定することを意味します.上記の設定では、「spring」を選択します.Daoで始まる正規表現が指定されているのでspring.MemberDaoクラスを構成部品スキャンターゲットから除外します.
Aspectj
@Configuration
@ComponentScan(basePackages = {"spring"},
excludeFilters = @Filter(type = FilterType.ASPECTJ, pattern = "spring.*Dao"))
public class AppCtx {
...
}
まず現在「spring.*Dao」の予測JモードはspringpackageのDaoで終わるタイプを指定しますAspectjweaverモジュールは、AspectJモードを実行するために依存オブジェクトに追加する必要があります.
よくせい
特定のノイズを伴うタイプは、構成部品ターゲットから除外することもできます.たとえば、@NoProductまたは@Manualbeanコメントを追加するクラスは、構成部品スキャンターゲットから除外したい場合があります.@Configuration
@ComponentScan(basePackages = {"spring"},
excludeFilters = @Filter(type = FilterType.ANNOTATION, classes = {NoProduct.class, ManualBean.class}))
public class AppCtx {
...
}
を選択します。
構成部品スキャンから特定のタイプまたはそのサブタイプを除外するには、ASSIGNABLE TYPEをFilterTypeとして使用します.@Configuration
@ComponentScan(basePackages = {"spring"},
excludeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = MemberDao.class))
public class AppCtx {
...
}
既定のスキャンターゲット
@Componentコメント付きクラスのみがコンポーネントスキャンターゲットに含まれるわけではありません.構成部品スイープオブジェクトには、次の注釈付きクラスが含まれます.
@Configuration
@ComponentScan(basePackages = {"spring"},
excludeFilters = @Filter(type = FilterType.REGEX, pattern = "spring\\..*Dao"))
public class AppCtx {
...
}
@Configuration
@ComponentScan(basePackages = {"spring"},
excludeFilters = @Filter(type = FilterType.ASPECTJ, pattern = "spring.*Dao"))
public class AppCtx {
...
}
@Configuration
@ComponentScan(basePackages = {"spring"},
excludeFilters = @Filter(type = FilterType.ANNOTATION, classes = {NoProduct.class, ManualBean.class}))
public class AppCtx {
...
}
@Configuration
@ComponentScan(basePackages = {"spring"},
excludeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = MemberDao.class))
public class AppCtx {
...
}
@Componentコメント付きクラスのみがコンポーネントスキャンターゲットに含まれるわけではありません.構成部品スイープオブジェクトには、次の注釈付きクラスが含まれます.
スキャン構成部品による競合の処理
構成部品スキャン機能を使用して空を自動的に登録する場合は、競合に注意してください.空の名前の競合と手動登録の競合が大きい場合があります.
空の名前の競合
Springパッケージとspring 2パッケージにMemberRegisterServiceクラスが存在し、両方のクラスに@Componentコメントが添付されているとします.この状態でspringとspring 2の両方が素子走査を行うと,利点概念が生じる.
このような構成部品スキャン中に、異なるタイプの構成部品で同じ空の名前を使用する場合は、名前の競合を回避するために、いずれかの構成部品に空の名前を明確に指定する必要があります.
手動登録との空の競合
スキャン時に使用する空の名前が手動で登録した空の名前と同じである場合、手動で登録した空の名前が優先されます.
Reference
この問題について([スプリング]スプリング5プログラミング入門-5章素子スキャン), 我々は、より多くの情報をここで見つけました
https://velog.io/@injoon2019/스프링-스프링5-프로그래밍-입문-5장-컴포넌트-스캔
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
Reference
この問題について([スプリング]スプリング5プログラミング入門-5章素子スキャン), 我々は、より多くの情報をここで見つけました https://velog.io/@injoon2019/스프링-스프링5-프로그래밍-입문-5장-컴포넌트-스캔テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol