spring component-scan filter
8907 ワード
このファイルにはbeansルートノードの下には一つのcontextしかない:component-scanノードがあります.このノードには2つの属性base-package属性があるので、springにスキャンするパケットを教えています.use-default-filters=「false」はデフォルトのフィルタを使わないことを示しています.ここでのデフォルトフィルタは、Service、Component、Component、Component、Component、Component、Component、Component、Compここでは例として、わざとuse-default-filters属性をfalseに設定します.
context:component-scanノードは、2つのサブノード<context:include-filter>と<context:exclude-filter>があることを許可する.filterタグのtypeと表現は以下の通りです.
Filter Type
Examples Expression
Description
include-filterを例にとって
アンノテーション
org.example.SomeAnnotation
SomeAnnotionに該当するtaget class
<context:include-filter type=「annotation」expression=「org.aspectj.lang.annotations.Aspect」/>スキャンbase-packageのクラスにAspringの注釈を加えたクラスを表し、springのbean容器に登録します.
assignable
org.example.SomeClass
クラスまたはインターフェースのフルネームを指定します.
<context:include-filter type=「assignable」expression=「comp.test.scan.StuService」/>スキャンStuService類をbeanとして指定します.
aspectj
org.example.*Service+
AsppectJ文法
レゲックス
org\.example\.Default.*
Regelar Expression
custom
org.example.MyType Filter
Spring 3新規予約Type、実作org.springframe ewark.type.Type Filter
私たちの例では、filterのtypeを正規表現、regexに設定します.正則の中では、すべての文字を表します.\.は本当の文字を表します.私たちの正則はDaoまたはServiceで終わるクラスを表しています.
私達はannotationを使って限定することもできます.以下の通りです.
またcontext:conponent-scanノードには、排除するクラスを指定するために、「context:exclude-filter」があります.その使い方はinclude-filterと一致します.
<回転:http://outofmemory.cn/java/spring/spring-DI-with-annotation-context-component-scan>
use-default-filtersのデフォルト値はtrueで、つまりspingはデフォルトでService、Component、Repository、Controllerが注釈したbeanをスキャンします.
その他に加えたinclude-filterとexclude-filterは以上の基礎の上で増加するスキャン規則です.
context:component-scanノードは、2つのサブノード<context:include-filter>と<context:exclude-filter>があることを許可する.filterタグのtypeと表現は以下の通りです.
Filter Type
Examples Expression
Description
include-filterを例にとって
アンノテーション
org.example.SomeAnnotation
SomeAnnotionに該当するtaget class
<context:include-filter type=「annotation」expression=「org.aspectj.lang.annotations.Aspect」/>スキャンbase-packageのクラスにAspringの注釈を加えたクラスを表し、springのbean容器に登録します.
assignable
org.example.SomeClass
クラスまたはインターフェースのフルネームを指定します.
<context:include-filter type=「assignable」expression=「comp.test.scan.StuService」/>スキャンStuService類をbeanとして指定します.
aspectj
org.example.*Service+
AsppectJ文法
レゲックス
org\.example\.Default.*
Regelar Expression
custom
org.example.MyType Filter
Spring 3新規予約Type、実作org.springframe ewark.type.Type Filter
私たちの例では、filterのtypeを正規表現、regexに設定します.正則の中では、すべての文字を表します.\.は本当の文字を表します.私たちの正則はDaoまたはServiceで終わるクラスを表しています.
私達はannotationを使って限定することもできます.以下の通りです.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="cn.outofmemory.spring" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/> <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/> </context:component-scan> </beans>
ここで指定したinclude-filterのtypeはannotationで、expressionは注釈類のフルネームです.またcontext:conponent-scanノードには、排除するクラスを指定するために、「context:exclude-filter」があります.その使い方はinclude-filterと一致します.
<回転:http://outofmemory.cn/java/spring/spring-DI-with-annotation-context-component-scan>
use-default-filtersのデフォルト値はtrueで、つまりspingはデフォルトでService、Component、Repository、Controllerが注釈したbeanをスキャンします.
その他に加えたinclude-filterとexclude-filterは以上の基礎の上で増加するスキャン規則です.
public ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters)
{
resourcePatternResolver = new PathMatchingResourcePatternResolver();
metadataReaderFactory = new CachingMetadataReaderFactory(resourcePatternResolver);
resourcePattern = "**/*.class";
if(useDefaultFilters)
registerDefaultFilters();
}
protected void registerDefaultFilters()
{
includeFilters.add(new AnnotationTypeFilter(org/springframework/stereotype/Component));
ClassLoader cl = org/springframework/context/annotation/ClassPathScanningCandidateComponentProvider.getClassLoader();
try
{
includeFilters.add(new AnnotationTypeFilter(cl.loadClass("javax.annotation.ManagedBean"), false));
logger.info("JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning");
}
catch(ClassNotFoundException _ex) { }
try
{
includeFilters.add(new AnnotationTypeFilter(cl.loadClass("javax.inject.Named"), false));
logger.info("JSR-330 'javax.inject.Named' annotation found and supported for component scanning");
}
catch(ClassNotFoundException _ex) { }
}
excludeFilters includeFilters
protected boolean isCandidateComponent(MetadataReader metadataReader)
throws IOException
{
for(Iterator iterator = excludeFilters.iterator(); iterator.hasNext();)
{
TypeFilter tf = (TypeFilter)iterator.next();
if(tf.match(metadataReader, metadataReaderFactory))
return false;
}
for(Iterator iterator1 = includeFilters.iterator(); iterator1.hasNext();)
{
TypeFilter tf = (TypeFilter)iterator1.next();
if(tf.match(metadataReader, metadataReaderFactory))
return true;
}
return false;
}