《Spring Recipes》第一章筆記:Scanning Components from ...

問題

當須要注入的bean太多時,手工進行配置太費時費力,Spring容器提供了指定掃描功能。

解決方案


使用Spring的component scanning功能。能夠經過@Component,@Repository, @Service , 和@Controller註解,使Spring容器指定掃描bean配置,進行注入。

@Repository
public class SequenceDaoImpl implements SequenceDao {
... ...
}

能夠在使用component scan的同時,使用@Autowired進行注入
@Service 
public class SequenceService {
    @Autowired
    private SequenceDao sequenceDao;
... ...
}


配置文件:
須要配置<context:component-scan base-package="com.apress.springrecipes.sequence">,base-package指定了容器須要掃描的類路徑。
<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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.apress.springrecipes.sequence" />
</beans>

Filtering Components

Spring容器運行用戶對須要進行掃描的bean進行過濾。
經過context:include-filter和context:exclude-filter標籤進行配置。

Table 4.5. Filter Typesjava

Filter Type Example Expression Description
annotation org.example.SomeAnnotation An annotation to be present at the type level in target components.
assignable org.example.SomeClass A class (or interface) that the target components are assignable to (extend/implement).
aspectj org.example..*Service+ An AspectJ type expression to be matched by the target components.
regex org\.example\.Default.* A regex expression to be matched by the target components class names.
custom org.example.MyTypeFilter A custom implementation of the org.springframework.core.type .TypeFilter interface.

例:
<beans ...>
  <context:component-scan base-package="com.apress.springrecipes.sequence">
    <context:include-filter type="regex"
    expression="com\.apress\.springrecipes\.sequence\..*Dao.*" />
    <context:include-filter type="regex"
    expression="com\.apress\.springrecipes\.sequence\..*Service.*" />
    <context:exclude-filter type="annotation"
    expression="org.springframework.stereotype.Controller" />
  </context:component-scan>
</beans>


對掃描到的Componet進行命名

Spring容器默認狀況下,對掃描到的bean的命名方式爲:將bean的類名的首字母小寫,例如
@Repository
public class SequenceDaoImpl implements SequenceDao {
... ...
}

在容器中的名稱爲sequenceDaoImpl。

用戶也能夠自行進行指定:
@Service("sequenceService")
public class SequenceService {
...
}

@Repository("sequenceDao")
public class SequenceDaoImpl implements SequenceDao {
...
}


而且用戶能夠經過設置<context:component-scan>元素的name-generator屬性來指定component默認名稱生成規則。
<beans>
<context:component-scan base-package="org.example"
    name-enerator="org.example.MyNameGenerator"/>
</beans>

指定Component的Scope

使用@Scope註解設定。
@Scope("prototype")
@Repository
public class MovieFinderImpl implements MovieFinder {
  // ...
}


Table 4.3. Bean scopesweb

Scope Description

singletonspring

(Default) Scopes a single bean definition to a single object instance per Spring IoC container.express

prototypesession

Scopes a single bean definition to any number of object instances..net

requestprototype

Scopes a single bean definition to the lifecycle of a single HTTP request; that is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.code

sessioncomponent

Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.xml

global session

Scopes a single bean definition to the lifecycle of a global HTTP Session. Typically only valid when used in a portlet context. Only valid in the context of a web-aware Spring ApplicationContext.

相關文章
相關標籤/搜索