芝麻驿站/JuvenWes: Spring组件扫描

<context:component-scan />

使用目的:减少xml中配置的与Action等相关的bean。

Java:

  类名上加  @Controller / @Service / @Respository

  方法名上加  @RequestMapping(value="相对url")

 

xml:

1 <context:component-scan base-package="扫描的包">
2   <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
3   <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
4   <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" />
5 </context:component-scan>

@component是所有受Spring管理组件的通用形式。

细化:

  • @Repository 持久化层
  • @Service 服务层
  • @Controller 表现层

type:

filter type Example Expression Description
annotation org.example.SomeAnnocation 符合SomeAnnocation的target class
assignable org.example.SomeClass 指定class或interface全名
aspectj org.example.Service+ Aspectj语法
regex org.example.Default* Regelar Expression
custom org.example.MyTypeFilter Spring3新增的自定type
posted @ 2013-01-25 15:10  芝麻驿站/JuvenWes  阅读(167)  评论(1)    收藏  举报