Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier、@scope
以下内容摘自部分网友的,并加上了自己的理解
@Service用于标注业务层组件(我们通常定义的service层就用这个)
@Controller用于标注控制层组件(如struts中的action、Spring MVC中的Controller)
@Repository用于标注数据访问组件,即DAO组件
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
这几个注解是当你需要定义某个类为一个bean,则在这个类的类名前一行使用@Service("XXX"),就相当于将这个类定义为一个bean,bean名称为XXX; 这几个是基于类的,我们可以定义名称,也可以不定义,不定义会默认以类名为bean的名称(类首字母小写)。
Spring 容器中匹配的候选 Bean 数目必须有且仅有一个。当找不到一个匹配的 Bean 时,Spring 容器将抛BeanCreationException 异常,并指出必须至少拥有一个匹配的 Bean。
@Resource、@Autowired、@Qualifier
当需要在某个类中定义一个属性,并且该属性是一个已存在的bean,要为该属性赋值我们就用着三个。例如:
<span style="font-size:18px;">@Resource
private IIocDao iocDao; @Autowired
private IocService iocService;</span>
我们先看@Resource,它是javax.annotation.Resource; 这个包中,也就是说是javaEE中的,并不是spring中的
而且@Resource("xxx") 是可以定义bean名称的,就是说我这个属性要用那个bean来赋值。
@Autowired,它是org.springframework.beans.factory.annotation.Autowired 是这个包中,它是spring的包。
而且它没有@Autowired("xxx"),那我要为这个bean定义名称怎么办这个时候可以用@Qualifier("xxx") 这个也是spring中的。
另外,@Autowired 可以对成员变量、方法以及构造函数进行注释,而 @Qualifier 的标注对象是成员变量、方法入参、构造函数入参。
一般情况下Spring是通过类名来生成相应的bean的,如果一个接口有多个实现,那具体应该调用哪个bean呢?或者我不想使用和类名相同的bean名字怎么办呢?有以下两种
第一种:我们在生成bean的时候就给bean定义个名称
这样就把这个实现定义为myQuestionSysService了,而不是默认的类名questionSysService(生成的bean通常和类名相同,只是首字母小写)
//这里@Repository和使用@Service等是一样的
@Repository("myQuestionSysService")
public class QuestionSysImpl implements QuestionSysService {
	@Resource(name="questionSysDao")
	private QuestionSysDao  questionSysDao;
}
在使用这个bean的时候,使用
@Resource
private QuestionSysService myQuestionSysService;
第二种:在注入bean的时候指定名称
//在@Resource中指定name名字,和实现类的bean的名字相同
注意这里是定义的实现类还是使用的默认bean
<span style="font-size:18px;">@Resource(name = "</span><span style="font-size:18px;">q</span><span style="font-size:18px;">uestionSysImpl")
</span><pre name="code" class="java"><span style="font-size:18px;">private QuestionSysService xxx;</span>
如果你要为这个类指定别名bean,@Repository("myQuestionSysService"),那么@Resource(name="myQuestionSysService") 就要这么写了。就是这里的name要跟实现类对应的bean名称保持一致。而这里,private QuestionSysService xx; 这个属性名就随便写了。
如果用Autowired就要这么写了
@Autowired
@Qualifier("<span style="font-size:18px;">q</span><span style="font-size:18px;">uestionSysImpl</span>")
private QuestionSysService xx;
当然@Resoucre也可以像@Autowired这样指定@Qualifier,只不过@Resoucre可以直接指定bean name,而@Autowired不可以
记住一点:@Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,如果发现找到多个bean,则,又按照byName方式比对,如果还有多个,则报出异常 而@Resource默认按 byName自动注入罢了。其实spring注解,最常用的还是根据名称,根据类型啊,构造方法啊,用的非常少。所以在多个实现的时候我们定义好bean的名称就行,就不会错乱。
因为Autowired 不能像Resource 那样带个参数指定一个name,就要用Qualifier来指定了。
在一个稍大的项目中,如果组件采用xml的bean定义来配置,显然会增加配置文件的体积,查找以及维护起来也不太方便。 Spring2.5为我们引入了组件自动扫描机制,他在类路径下寻找标注了上述注解的类,并把这些类纳入进spring容器中管理。它的作用和在xml文件中使用bean节点配置组件时一样的。要使用自动扫描机制,我们需要打开以下配置信息:
<?xml version="1.0" encoding="UTF-8"?>
<!--Spring总体配置-->
<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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> </beans>
最后是@scope用于指定bean在IOC容器中的生命周期
Spring最初只提供了两种scope,即singleton(默认的)和prototype,但在2.0之后,又发布了三种类型:request、session和global session,这三种只能在web应用中才能使用。
singleton: Spring 容器只会创建该bean定义的唯一实例,这个实例会被保存到缓存中,并且对该bean的所有后续请求和引用都将返回该缓存中的对象实例,一般情况下,无状态的bean使用该scope。
prototype:每次对该bean的请求都会创建一个新的实例,一般情况下,有状态的bean使用该scope。
request:每次http请求将会有各自的bean实例,类似于prototype。
session:在一个http session中,一个bean定义对应一个bean实例。
global session:在一个全局的http session中,一个bean定义对应一个bean实例。典型情况下,仅在使用portlet context的时候有效。
无状态会话bean :bean一旦实例化就被加进会话池中,各个用户都可以共用。即使用户已经消亡,bean 的生命期也不一定结束,它可能依然存在于会话池中,供其他用户调用。由于没有特定的用户,那么也就不能保持某一用户的状态,所以叫无状态bean。但无状态会话bean 并非没有状态,如果它有自己的属性(变量),那么这些变量就会受到所有调用它的用户的影响,这是在实际应用中必须注意的。
有状态会话bean :每个用户有自己特有的一个实例,在用户的生存期内,bean保持了用户的信息,即“有状态”;一旦用户灭亡(调用结束或实例结束),bean的生命期也告结束。即每个用户最初都会得到一个初始的bean。
详见http://docs.spring.io/spring/docs/3.0.0.M3/reference/html/ch04s04.html
Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier、@scope的更多相关文章
- SpringAnnotation注解之@Component,@Repository,@Service,@Controller
		
@Component:组件,表示此写上了此注解的bean,作为一个组件存在于容器中.这样的话别的地方就可以使用@Resource这个注解来把这个组件作为一个资源来使用了.初始化bean的名字为类名首字 ...
 - @Component @Repository @Service @Controller
		
Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository.@Service 和 @Controller.在目前的 Spring ...
 - 从头认识Spring-2.7 自己主动检測Bean(1)-@Component @Repository @Service @Controller
		
这一章节我们来讨论一下自己主动检測Bean. 1.domain 厨师类: package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_19; ...
 - Spring 注解@Component,@Service,@Controller,@Repository
		
Spring 注解@Component,@Service,@Controller,@RepositorySpring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释, ...
 - [转]what’s the difference between @Component ,@Repository & @Service annotations in Spring
		
原文地址:https://www.cnblogs.com/softidea/p/6070314.html @Component is equivalent to <bean> @Servi ...
 - What's the difference between @Component, @Repository & @Service annotations in Spring?
		
@Component is equivalent to <bean> @Service, @Controller , @Repository = {@Component + some mo ...
 - Spring注解Component原理源码解析
		
在实际开发中,我们经常使用Spring的@Component.@Service.@Repository以及 @Controller等注解来实现bean托管给Spring容器管理.Spring是怎么样实 ...
 - 【转载】@Component, @Repository, @Service的区别
		
@Component, @Repository, @Service的区别 官网引用 引用spring的官方文档中的一段描述: 在Spring2.0之前的版本中,@Repository注解可以标记在任何 ...
 - Spring注解@Component、@Repository、@Service、@Controller,@Autowired、@Resource用法
		
一.Spring定义bean,@Component.@Repository.@Service 和 @Controller Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥 ...
 
随机推荐
- 图的 储存 深度优先(DFS)广度优先(BFS)遍历
			
图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...
 - Java多线程:CAS与java.util.concurrent.atomic
			
锁的几种概念 悲观锁 总是假设最坏的情况,每次获取数据都认为别人会修改,所以拿数据时会上锁,一直到释放锁不允许其他线程修改数据.Java中如synchronized和reentrantLock就是这种 ...
 - GO开发环境搭建
			
GO开发环境搭建 1)下载GO SDK,并安装 https://golang.google.cn/dl/ 2)下载GO IDE:GOLAND,并安装 3)设置GOROOT和GOPATH 4)新建一个工 ...
 - block 相关清单
			
对Objective-C中Block的追探 李博士
 - 1.2 Stream API
			
引例: List<String> strList = Arrays.asList("zhaojigang","nana","tianya& ...
 - (转)Fur Shader
			
转自:http://qiankanglai.me/misc/2014/11/15/fur-shader/ 花时间看了下毛发效果,苦于囊中羞涩没能买QuickFur.furFX等插件,最后找到了Fur ...
 - openHEVC 编译 for VS2017+Win10 x64
			
编译暂未成功,有空再次更新 前期准备: yasm下载:http://yasm.tortall.net/Download.html http://www.tortall.net/projects/yas ...
 - C++开源项目等收集
			
VLC 是一款自由.开源的跨平台多媒体播放器及框架,可播放大多数多媒体文件,以及 DVD.音频 CD.VCD 及各类流媒体协议. Downloading vlc-2.2.4.tar.xz Thanks ...
 - 【Yaml】Yaml学习笔记
			
转载:https://blog.csdn.net/moshenglv/article/details/52084899 YAML何许物也?在XML泛滥的情况下,YAML的出现的确让人眼前一亮,在初步学 ...
 - 使用VirtualBox在Ubuntu下虚拟Windows XP共享文件夹设置方法
			
1.首先保证虚拟的Windows XP有虚拟光驱(正常安装的都是有的,因为在ubuntu下一般都是用硬盘虚拟安装的),然后在已经运行起来的Windows XP菜单栏上选择“设备-安装增强功能“,如果不 ...