标签:
@Component 相当于实例化类的对象,其他三个注解可以理解为@Component的子注解或细化。
在annotaion配置注解中用@Component来表示一个通用注释用于说明一个类是一个spring容器管理的类,此类将有spring扫描并加入容器参与ioc。即就是该类已经拉入到spring的管理中了。
通过在 classpath 中通过自动扫描方式把组建纳入 spring 容器管理。
要使用自动扫描机制我们需要打开一下配置信息:
注:前面讲要使用注解需要配置: <context:annotation-config />但如果使用了@Component就不需要加它了,因为:<context:component-scan base-package="com.zchen">里面默认了<context:annotation-config />。
而@Controller, @Service, @Repository是@Component的细化,这三个注解比@Component带有更多的语义,它们分别对应了控制层、服务层、持久层的类。
@Component泛指组件,当组件不好归类的时候我们可以使用这个注解进行标注,(现在可以都用此注解,可以只使用单一组件)
关于Spring注解 @Service @Component @Controller @Repository 用法
标签:
原文地址:http://www.cnblogs.com/luoluoshidafu/p/4604451.html