《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 TypeExample ExpressionDescription
annotationorg.example.SomeAnnotationAn annotation to be present at the type level in target components.
assignableorg.example.SomeClassA class (or interface) that the target components are assignable to (extend/implement).
aspectjorg.example..*Service+An AspectJ type expression to be matched by the target components.
regexorg\.example\.Default.*A regex expression to be matched by the target components class names.
customorg.example.MyTypeFilterA 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

ScopeDescription

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.


转载于:https://my.oschina.net/pkpk1234/blog/57515

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值