原文地址:http://blog.csdn.net/zhang854429783/article/details/6785574

很长时间没做web项目都把以前学的那点框架知识忘光了,今天把以前做的一个项目翻出来看一下发现用·@Component标记一个组件,而网上有的用@Service标记组件,我晕就查了一下资料:

Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository、@Service 和 @Controller。
在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层、业务层和控制层(Web 层)相对应。
虽然目前这3 个注释和 @Component 相比没有什么新意,但 Spring 将在以后的版本中为它们添加特殊的功能。
所以,如果 Web 应用程序采用了经典的三层分层结构的话,最好在持久层、业务层和控制层分别采用上述注解对分层中的类进行注释。

@Service用于标注业务层组件

@Controller用于标注控制层组件(如struts中的action)

@Repository用于标注数据访问组件,即DAO组件

@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

@Service
public class VentorServiceImpl implements iVentorService {
}
@Repository
public class VentorDaoImpl implements iVentorDao {
}

在一个稍大的项目中,如果组件采用xml的bean定义来配置,显然会增加配置文件的体积,查找以及维护起来也不太方便。
Spring2.5为我们引入了组件自动扫描机制,他在类路径下寻找标注了上述注解的类,并把这些类纳入进spring容器中管理。
它的作用和在xml文件中使用bean节点配置组件时一样的。要使用自动扫描机制,我们需要打开以下配置信息:

代码

<?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:component-scan base-package=”com.eric.spring”>
</beans>

1.component-scan标签默认情况下自动扫描指定路径下的包(含所有子包),将带有@Component、@Repository、 @Service、@Controller标签的类自动注册到spring容器。对标记了 Spring's @Required、@Autowired、JSR250's @PostConstruct、@PreDestroy、@Resource、JAX-WS's @WebServiceRef、EJB3's @EJB、JPA's @PersistenceContext、@PersistenceUnit等注解的类进行对应的操作使注解生效(包含了annotation- config标签的作用)。

getBean的默认名称是类名(头字母小写),如果想自定义,可以@Service(“aaaaa”)这样来指定。
这种bean默认是“singleton”的,如果想改变,可以使用@Scope(“prototype”)来改变。

可以使用以下方式指定初始化方法和销毁方法:

@PostConstruct
public void init() {
}
@PreDestroy
public void destory() {
}

注入方式:

把DAO实现类注入到action的service接口(注意不要是service的实现类)中,注入时不要new 这个注入的类,因为spring会自动注入,如果手动再new的话会出现错误,
然后属性加上@Autowired后不需要getter()和setter()方法,Spring也会自动注入。

在接口前面标上@Autowired注释使得接口可以被容器注入,如:

@Autowired
@Qualifier("chinese")
private Man man;

当接口存在两个实现类的时候必须使用@Qualifier指定注入哪个实现类,否则可以省略,只写@Autowired。

http://crabboy.iteye.com/blog/339840

[转] Spring注解@Component、@Repository、@Service、@Controller区别的更多相关文章

  1. 【转载】@Component, @Repository, @Service的区别

    @Component, @Repository, @Service的区别 官网引用 引用spring的官方文档中的一段描述: 在Spring2.0之前的版本中,@Repository注解可以标记在任何 ...

  2. SpringAnnotation注解之@Component,@Repository,@Service,@Controller

    @Component:组件,表示此写上了此注解的bean,作为一个组件存在于容器中.这样的话别的地方就可以使用@Resource这个注解来把这个组件作为一个资源来使用了.初始化bean的名字为类名首字 ...

  3. @Component @Repository @Service @Controller

    Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository.@Service 和 @Controller.在目前的 Spring ...

  4. @Component, @Repository, @Service的区别

    注解 含义 @Component 最普通的组件,可以被注入到spring容器进行管理 @Repository 作用于持久层 @Service 作用于业务逻辑层 @Controller 作用于表现层(s ...

  5. 从头认识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; ...

  6. Spring 注解@Component,@Service,@Controller,@Repository

    Spring 注解@Component,@Service,@Controller,@RepositorySpring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释, ...

  7. [转]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 ...

  8. What's the difference between @Component, @Repository & @Service annotations in Spring?

    @Component is equivalent to <bean> @Service, @Controller , @Repository = {@Component + some mo ...

  9. Spring注解Component原理源码解析

    在实际开发中,我们经常使用Spring的@Component.@Service.@Repository以及 @Controller等注解来实现bean托管给Spring容器管理.Spring是怎么样实 ...

  10. Spring注解@Component、@Repository、@Service、@Controller,@Autowired、@Resource用法

    一.Spring定义bean,@Component.@Repository.@Service 和 @Controller Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥 ...

随机推荐

  1. C# 格式化小总结

    C#中几个常用的格式化标识符 C或c Currency 货币格式 D或d Decimal 十进制格式(十进制整数,不要和.Net的Decimal数据类型混淆了) E或e Exponent 指数格式 F ...

  2. MarkdownPad Win10 无法预览

    软件环境 系统:windows 10 x64 软件:MarkDownPad 2 遇到问题 Markdownpad的实时预览无法显示 解决办法 安装 Awesonmium sdk后,重新打开Markdo ...

  3. sql 盲注之正则表达式攻击

    -----------------------------------------MYSQL 5+----------------------------------------- 我们都已经知道,在 ...

  4. Python自动补全

    转自:http://blog.linuxeye.com/324.html Python自动补全有vim编辑下和python交互模式下,下面分别介绍如何在这2种情况下实现Tab键自动补全. 一.vim ...

  5. Hibernate总结1(入门)

    1,官网包简介 整体简介 Project包简介,这个包里有etc文件,这个etc主要包括了配置文件,最主要的是hibernate.cfg.xml文件 lib文件夹包含的是需要加载的依赖jar包,必须加 ...

  6. 微软架构师解读Windows Server 2008 R2新特性

    目前众多企业都开始为自己寻找一个更加适合自身发展的服务器操作平台.微软的Windows Server 2008 R2就是可以为大家解决服务器平台问题.微软最新的服务器平台Windows Server ...

  7. 安装 SQL SERVER 2008 必须使用 "角色管理工具" 错误 的 解决方案 (转)

    刚在服务器(Win2008)上安装SqlServer2008的时候出现了这么一个报错——必须使用“角色管理工具”安装或配置Microsoft .NET Framework 3.5 SP1.一开始以为是 ...

  8. Oracle 常用函数

    主要是对项目中用过的 oracle 函数进行总结,并做出目录,方便后续项目是快速查找,提高效率. 01.Round (数值的四舍五入) 描述:传回一个数值,该数值是按照指定的小数位元数进行四舍五入运算 ...

  9. [MCSM]伪随机数和伪随机数生成器

    1. 几个问题 为什么需要随机数? 伪随机数伪在哪里? 为何要采用伪随机数代替随机数?这种代替是否有不利影响? 如何产生(伪)随机数? 以下内容将围绕这几个问题依次说明. 2. 参考 http://e ...

  10. 网游中的网络编程系列1:UDP vs. TCP

    原文:UDP vs. TCP,作者是Glenn Fiedler,专注于游戏网络编程相关工作多年. 目录 网游中的网络编程系列1:UDP vs. TCP 网游中的网络编程2:发送和接收数据包 网游中的网 ...