@Controller、@Service、@Repository的区别

        在annotaion配置注解中用@Component来表示一个通用注释用于说明一个类是一个spring容器管理的类。即就是该类已经拉入到spring的管理中了。而@Controller,@Service, @Repository是@Component的细化,这三个注解比@Component带有更多的语义,它们分别对应了控制层、服务层、持久层的类。
@Component大致可以分为以下几个注解:
        a.@controller控制器(注入服务),用于标注控制层组件(如struts中的action)
        b.@service服务(注入dao),用于标注业务层组件
        c.@repository dao(实现dao访问),用于标注数据访问组件,即DAO组件
       
        一般版本低的JDK在用到@Resource注解时,可能需要引入j2ee/common-annotations.jar。JDK1.6或以上,不需要引入。

下面这个XML代码段是引入component的扫描组件(想要实现组件方式扫描,必须在XML中配置下面代码,其中base-package为需要扫描的包(含所有子包)):
    <!--扫描Web类包 启动时先于applicationContext加载-->
    <context:component-scan base-package="com.hide168">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
    </context:component-scan>

猜你喜欢

转载自kingwarluo.iteye.com/blog/2333185