- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章聊聊注解@controller@service@component@repository的区别由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
查了一些网上的其他博客,发现几个注解本质上没有什么区别,至少在spring2.5版本里,这几个注解本质是一样的(当然,新的版本有什么变化目前还没细查) 。
@Repository
:持久层,用于标注数据访问组件,即DAO组件。@Service
:业务层,用于标注业务逻辑层主键。@Controller
:控制层,用于标注控制层组件。@Component
:当你不确定是属于哪一层的时候使用。之所以区分开几种类型,一是spring想在以后的版本中为它们添加特殊技能,二是这种分层的做法使web架构更清晰,易读性与维护性更好.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
/**
* Indicates that an annotated class is a "Service", originally defined by Domain-Driven
* Design (Evans, 2003) as "an operation offered as an interface that stands alone in the
* model, with no encapsulated state."
*
* <p>May also indicate that a class is a "Business Service Facade" (in the Core J2EE
* patterns sense), or something similar. This annotation is a general-purpose stereotype
* and individual teams may narrow their semantics and use as appropriate.
*
* <p>This annotation serves as a specialization of {@link Component @Component},
* allowing for implementation classes to be autodetected through classpath scanning.
*
* @author Juergen Hoeller
* @since 2.5
* @see Component
* @see Repository
*/
@Target
({ElementType.TYPE})
@Retention
(RetentionPolicy.RUNTIME)
@Documented
@Component
public
@interface
Service {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any
*/
String value()
default
""
;
}
|
从@service的源码看,service仍然是使用了@Component注解(@Controller与@Repository与service一样,这里就不贴源码了).
component即组件,相当于xml配置文件中的bean申明,通过spring的自动扫描机制,在规定的类路径中寻找标注了@Component,@Service,@Repository,@Controller注解的类,并把这些类纳入进容器中进行管理.
getBean的默认名称是类名(头字母小写),并且是单例的,如果想自定义,可以使用@Service(“beanName”)@Scope(“prototype”)来改变.
1
2
|
/**自动扫描base-package目录下的所有文件,包括子目录**/
<context:component-scan base-
package
=
"com.user.*"
/>
|
Spring的一个核心功能是IOC,就是将Bean初始化加载到容器中, Bean加载到容器中可以使用 Spring注解方式或者 Spring XML配置方式。(通过注解将java中的一些类,加载到容器中) 。
@Repository、@Service、@Controller的功能和@Component相同,但为了使标注类的用途更加清晰(层次化),在实际开发中推荐使用:
@Repository标注数据访问层(DAO层)、使用@Service标注业务逻辑层(Service层)以及使用@Controller标注控制器层(控制层).
该注解是一个泛化的概念,仅仅表示一个组件对象(Bean),可以作用在任何层次上.
该注解用于将数据访问层(DAO)的类标识为Bean,即注解数据访问层Bean,其功能与@Component()相同.
该注解用于标注一个业务逻辑组件类(Service层),其功能与@Component()相同.
该注解用于标注一个控制器组件类(Spring MVC的Controller),其功能与@Component()相同.
该注解可以对类成员变量、方法及构造方法进行标注,完成自动装配的工作(按照Bean的类型进行装配)。 通过 @Autowired的使用来消除setter 和getter方法。默认按照Bean的类型进行装配。(属于Spring 的org.springframework.beans.factory.annotation包) 。
通过@Autowired注解,如果在容器中有对应的bean,就可以通过@Autowired自动装载,也就是赋值。装载之后自动的按照类型在spring容器中查找相同类型,然后为该字段其注入那个类型的bean实例.
其实就是一种依赖注入的方式,此方式必须确保加注解的类在spring中有对应的bean(怎样加进去不管),并且字段的类型需要在spring容器中有相同类型的bean,才能创建bean实例,为其注入.
@Autowired注解就相当于从Spring容器中通过类型,实例化了当前对象,可以直接调用它的方法.
1
2
3
|
@Autowired
private
UserService userService;
// 相当于执行了实例化 private UserService userService = new UserServiceImpl();
|
该注解与@Autowired功能一样。区别在于,该注解默认是按照名称来装配注入的,只有当找不到与名称匹配的Bean才会按照类型来装配注入;而@Autowired默认按照Bean的类型进行装配,如果想按照名称来装配注入,则需要结合@Qualifier注解一起使用.
@Resource注解有两个属性:name和type。name属性指定Bean实例名称,即按照名称来装配注入;type属性指定Bean类型,即按照Bean的类型进行装配.
该注解与@Autowired注解配合使用。当@Autowired注解需要按照名称来装配注入,则需要结合该注解一起使用,Bean的实例名称由@Qualifier注解的参数指定.
@Autowired注解默认按照类型装配,如果容器中包含多个同一类型的Bean,那么启动容器时会报找不到指定类型bean的异常,解决办法是结合 @Qualifier 注解进行限定,指定注入的bean名称.
比如接口BaseInterface,它有两个实现类AClass和BClass,如果用@Autowired自动装载BaseInterface,会无法识别到底是哪一个实现类(AClass/BClass),所以就需要用@Qualifier来配合 @Autowired区分.
1
2
3
4
5
|
public
class
Demo{
@Autowired
@Qualifier
(
"id值"
)
//默认类名首字母小写 @Qualifier("aClass")
private
BaseInterface base;
}
|
以上为个人经验,希望能给大家一个参考,也希望大家多多支持我.
原文链接:https://blog.csdn.net/u011305680/article/details/51701371 。
最后此篇关于聊聊注解@controller@service@component@repository的区别的文章就讲到这里了,如果你想了解更多关于聊聊注解@controller@service@component@repository的区别的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
平时很少在jquery中用到this。查看代码时发现用到了,就调试出this的值,心想原来如此。还是挺有用的。这里总结一下this与$(this)的区别和使用。 $(this)生成的是什么?
使用单例类和应用程序范围的托管 bean 来保存应用程序数据有区别吗? 我需要查找某些 JNDI 资源,例如远程 bean 接口(interface),因此我为自己编写了一个单例来缓存我的引用并且只允
如果您仔细查看包含的图片,您会注意到您可以使用 Eclipse IDE 重构 Groovy 代码并将方法转换为闭包,反之亦然。那么,闭包到底是什么,它与方法有什么不同呢?有人可以举一个使用闭包的好例子
vagrant box repackage有什么区别( docs ) 和 vagrant package ( docs )? 我意识到 vagrant package仅适用于 VirtualBox 提
我想看看是否有人可以解释为什么以下代码适用于 valueOf 但不适用于其他代码。 import java.math.BigDecimal; public class Change { publ
这个问题已经有答案了: 已关闭12 年前。 Possible Duplicates: What is Closures/Lambda in PHP or Javascript in layman te
This question already has answers here: Vagrant, Docker, Puppet, Chef (3个答案) 2年前关闭。 docker和chef有什么共同
以下代码在95%的机器上产生相同的输出,但是在几台机器上却有所不同。在 Debug模式下,输出: Changing from New to Fin OK 但在 Release模式下: Changing
////Creating Object var Obj; // init Object Obj= {}; 它们之间有什么区别两个? 有没有可能把它变成一个单行? 这样使用有什么好处吗?
我想找出定时器服务之间的区别。我应该使用哪个以及何时使用。我正在使用 Jboss 应用服务器。 1) java.ejb.Schedule。 @Schedule注解或配置自xml。 2) javax.e
我发现在 C++ 中可以通过三种不同的方式将对象传递给函数。假设我的类(class)是这样的: class Test { int i; public: Test(int x);
有什么区别。 public class Test { public static void main(String args[]) { String toBeCast = "c
如果我有一列,设置为主索引,设置为INT。 如果我不将其设置为自动递增,而只是将唯一的随机整数插入其中,与自动递增相比,这是否会减慢 future 的查询速度? 如果我在主索引和唯一索引为 INT 的
这两种日期格式有什么区别。第一个给出实际时间,第二个给出时间购买添加时区偏移值。 NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]
如果有一个函数,请说foo: function foo() { console.log('bar'); } 那么在 JavaScript 中,从另一个函数调用一个函数有什么区别,如下所示: f
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 4 年前。 Improv
代码是什么: class Time { private: int hours; int minutes; int seconds; pu
我知道这是非常基本的,但有人介意解释一下这两个数组声明之间的区别吗: #include array myints; ...和: int myints[5]; ...以及为什么 myints.size
我学会了如何根据 http://reference.sitepoint.com/css/specificity 计算 css 特异性但是,基于this reference,我不明白伪类(来自c)和伪元
为什么在运行 2) 时会出现额外的空行?对我来说 1 就像 2。那么为什么 2) 中的额外行? 1) export p1=$(cd $(dirname $0) && pwd) #
我是一名优秀的程序员,十分优秀!