《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 Types

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 scopes

Scope Description

singleton

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

prototype

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

request

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.

session

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

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.


你可能感兴趣的:(《Spring Recipes》第一章笔记:Scanning Components from ...)