关于Spring注解 @Service @Component @Controller @Repository 用法
@Component 相当于实例化类的对象,其他三个注解可以理解为@Component的子注解或细化。
在annotaion配置注解中用@Component来表示一个通用注释用于说明一个类是一个spring容器管理的类,此类将有spring扫描并加入容器参与ioc。即就是该类已经拉入到spring的管理中了。
通过在 classpath 中通过自动扫描方式把组建纳入 spring 容器管理。
要使用自动扫描机制我们需要打开一下配置信息:
Bean.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带有更多的语义,它们分别对应了控制层、服务层、持久层的类。
@Component泛指组件,当组件不好归类的时候我们可以使用这个注解进行标注,(现在可以都用此注解,可以只使用单一组件)
关于Spring注解 @Service @Component @Controller @Repository 用法的更多相关文章
- Spring中的注解@Service @Component @Controller @Repository区别
@Service用于标注业务层组件, @Controller用于标注控制层组件(如struts中的action), @Repository用于标注数据访问组件,即DAO组件, @Component泛指 ...
- Spring学习使用标签来标记资源(@Component、@Repository、 @Service和@Controller)和用法(包括如何jsp正在使用)
首先,在xml其中新增部分标有下划线的文件,容器初始化的时候需要扫描包 注意: a. 包款扫描(下划线部分)一定要加,默认是不扫描整个包.与每一包之间','开.如过具有同样的父包,那么我们能够 ...
- Spring注解之@Component、@Controller、@Service、@Repository
目录 1.使用这四个注解的前提 2.详解@Component 3. @Service("XXX")或者@Service(value = "XXX")情况 4.总 ...
- Spring注解之@Component详细解析
@controller 控制器(注入服务) 2.@service 服务(注入dao) 3.@repository dao(实现dao访问) 4.@component (把普通pojo实例化到sprin ...
- spring注解中@component是什么意思
@Component("userManager") public class UserManagerImpl implements UserManager { private Us ...
- springMvc基本注解:@Component、@Repository(持久层) 、@Service(业务逻辑) 、@Controller(控制层)
1.@Controller(控制层) :就是action层 2.@Service(业务逻辑) :业务逻辑层,负责处理各种控制层的操作 3.@Repository(持久层) :称为“持久化”层,负责对数 ...
- @Component, @Controller, @Repository, @Service 有何区别?
@Component :这将 java 类标记为 bean.它是任何 Spring 管理组件的通 用构造型.spring 的组件扫描机制现在可以将其拾取并将其拉入应用程序环境 中. @Controll ...
- spring注解(Component、依赖注入、生命周期、作用域)
1.注解 注解就是一个类,使用@加上注解名称,开发中可以使用注解取代配置文件 2.@Component 取代<bean class="">,@Component 取代 ...
- spring注解@service("service")括号中的service有什么用
相当于 xml配置中得 bean id = service 也可以不指定 不指定相当于 bean id = com. service.service 就是这个类的全限定名 好处是:同一个接口可以有多个 ...
随机推荐
- mfoc安装编译
系统环境 UBUNTU16.04,要安装好各种基础的编译环境,这就不说了,如果不知道需要什么,那就编译时出错时看到什么安装什么吧 下载源码包libnfc, mfoc, mfcuk,都是github上, ...
- 如何在SQLite中创建自增字段
SQLite 简单的回答:一个声明为 INTEGER PRIMARY KEY 的字段将自动增加. 这里是详细的答案: 从 SQLite 的 2.3.4 版本开始,如果你将一个表中的一个字段声明为 ...
- GestureDetector 完全解析
个人原创 OnDown(MotionEvent e):用户触发DonenEvent就会执行onShowPress(MotionEvent e):用户触发DonenEvent后,在很短大概0.5秒内,没 ...
- BF3 里面的z cull reverse reload
Bf3 siggraph2011的 分享 http://advances.realtimerendering.com/s2011/White,%20BarreBrisebois-%20Renderin ...
- 字符串匹配算法系列一:KMP算法原理
本文主要参考了https://mp.weixin.qq.com/s/rbaPmBejID8-rYui35Snrg的表述,加上部分自己的理解 学习任何算法都要了解该算法解决什么问题?我们看看KMP算法主 ...
- maven打包时出现no compiler is provided in this environment
我是使用maven打jar包时出现的问题,与我问题不同的可以查看下面链接是否有棒状 http://blog.csdn.net/lslk9898/article/details/73836745 可以确 ...
- PHP中读取文件的几个方法
整理了一下PHP中读取文件的几个方法,方便以后查阅. 1.fread string fread ( int $handle , int $length ) fread() 从 handle 指向的文件 ...
- HDU5294——Tricks Device(最短路 + 最大流)
第一次做最大流的题目- 这题就是堆模板 #include <iostream> #include <algorithm> #include <cmath> #inc ...
- 栈(C++)
简介: 限定仅在表尾进行插入或删除操作的线性表 表尾端称为栈顶(top),表头端称为栈底(bottom) 特点: 在栈中,后入栈的元素先出栈 C语言版本 用于测试的文件,以及测试结果可以去作者GitH ...
- OpenCV 中的三大数据类型:CvMat 类型
前言 本文将介绍 OpenCV 中的矩阵结构 CvMat 并提供几个很常用的矩阵使用方法. 更多的矩阵处理函数还请参阅相关资料. CvMat 的类型定义 typedef struct CvMat { ...