@Component 至关于实例化类的对象,其余三个注解能够理解为@Component的子注解或细化。java
在annotaion配置注解中用@Component来表示一个通用注释用于说明一个类是一个spring容器管理的类,此类将有spring扫描并加入容器参与ioc。即就是该类已经拉入到spring的管理中了。spring
经过在 classpath 中经过自动扫描方式把组建归入 spring 容器管理。spa
要使用自动扫描机制咱们须要打开一下配置信息:component
Bean.xml代码 xml
- <?xml version= "1.0" encoding= "UTF-8" ?>
- <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-2.5 .xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5 .xsd">
- <!--<context:annotation-config />-->
- <context:component-scan base-package="com.zchen" />
- <!-- 包含annotation-config。spring能够自动去扫描base-pack下面或者子包下面的java文件,若是扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean-->
- </beans>
注:前面讲要使用注解须要配置: <context:annotation-config />但若是使用了@Component就不须要加它了,由于:<context:component-scan base-package="com.zchen">里面默认了<context:annotation-config />。对象
而@Controller, @Service, @Repository是@Component的细化,这三个注解比@Component带有更多的语义,它们分别对应了控制层、服务层、持久层的类。string
@Component泛指组件,当组件很差归类的时候咱们能够使用这个注解进行标注,(如今能够都用此注解,能够只使用单一组件)it