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 注释外,还定义了几个拥 ...
随机推荐
- hihocoder 1496 寻找最大值(高维前缀最大次大值)
[题目链接] https://hihocoder.com/problemset/problem/1496 [题目大意] 给定N个数A1, A2, A3, ... AN, 从中找到两个数Ai和Aj(i≠ ...
- LOJ.116.[模板]有源汇有上下界最大流(Dinic)
题目链接 http://blog.csdn.net/just_sort/article/details/75448403 有源汇有上下界网络流 通过添加一条(T->S,[0,INF])的边变成无 ...
- Java并发程序设计(八)设计模式与并发之单例模式
设计模式与并发之单例模式 简单的单例实现: public class Singleton { private Singleton(){ System.out.println("Creatin ...
- python计算文件夹大小——os.walk
下面提供一种利用os.walk()快捷的计算指定文件夹大小的方法 话不多说,直接上代码 简略版: import os def get_file_size(file_path, size=0): for ...
- 创建PCB原理图的模板
Q:创建PCB原理图的模板,主要就是设置模板的大小,右下角的信息框, 效果图如下: 创建PCB工程 创建原理图 设置模板,方便以后画原理图 打开模板的方式有:1按DO 2双击边框 标题块是附带有的,取 ...
- spring源码分析系列 (5) spring BeanFactoryPostProcessor拓展类PropertyPlaceholderConfigurer、PropertySourcesPlaceholderConfigurer解析
更多文章点击--spring源码分析系列 主要分析内容: 1.拓展类简述: 拓展类使用demo和自定义替换符号 2.继承图UML解析和源码分析 (源码基于spring 5.1.3.RELEASE分析) ...
- awk 入门教程
作者: 阮一峰 日期: 2018年11月 7日 awk是处理文本文件的一个应用程序,几乎所有 Linux 系统都自带这个程序. 它依次处理文件的每一行,并读取里面的每一个字段.对于日志.CSV 那 ...
- Java 数组元素倒序的三种方式
将数组元素反转有多种实现方式,这里介绍常见的三种. 直接数组元素对换 @Test public void testReverseSelf() throws Exception { System.out ...
- Ubuntu下升级Git以及获取ssh keys的代码
今天开始用的git,记下获取ssh keys 的代码 ? 1 2 3 ssh-keygen -t rsa -C "your_email@example.com" # Enter f ...
- IntelliJ_2017_安装Grep Console插件(console输出内容加颜色)
官网下载:https://plugins.jetbrains.com/plugin/7125-grep-console 一.安装Grep Console插件 二.设置不同日志类型的不同 ...