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

@Service用于标注业务层组件

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

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

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

  1. @Service
  2. public class VentorServiceImpl implements iVentorService {
  3. }
  4. @Repository
  5. public class VentorDaoImpl implements iVentorDao {
  6. }
@Service
public class VentorServiceImpl implements iVentorService {
}
@Repository
public class VentorDaoImpl implements iVentorDao {
}

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

代码

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context-2.5.xsd">
  9. <context:annotation-config />
  10. <context:component-scan base-package=”com.eric.spring”>
  11. </beans>
<?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.eric.spring”>
</beans>

1.annotation-config是对标记了 Spring's @Required、@Autowired、JSR250's @PostConstruct、@PreDestroy、@Resource、JAX-WS's @WebServiceRef、EJB3's @EJB、JPA's @PersistenceContext、@PersistenceUnit等注解的类进行对应的操作使注解生效。
2.base-package为需要扫描的包(含所有子包),负责扫描那些类有注解。

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

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

  1. public void init() {
  2. }
  3. @PreDestroy
  4. public void destory() {
  5. }
@PostConstruct
public void init() {
}
@PreDestroy
public void destory() {
}

注入方式:

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

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

  1. @Qualifier("chinese")
  2. private Man man;
@Autowired
@Qualifier("chinese")
private Man man;

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

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. 如果客户端禁用cookie,session还能使用吗?

    记得在以前找工作的时候,可多次被问到如果客户端被禁用cookie,session还能使用吗? 今天终于找到了相关的答案:我们来看一下: session是在服务器段保持会话数据的一种方法,对应的cook ...

  2. KBMMW 4.93.10 win64 一个BUG 修正

    经常有人提到kbmmw 4.93.10 的64 位版本没有32位版本稳定. 经过官方确认,是delphi 编译器生成64 位代码内存偏移地址的错误. 在kbmMWGlobal.pas 中 有一个函数k ...

  3. Python:利用内建函数将字符串转化为整数

    使用内建函数raw_input()内建函数,它读取标准输入,并将读取到的数据赋值给指定的变量.我们可以使用int()内建函数将用户输入的字符串转换为整数: >>> user = ra ...

  4. 使用Eclipse上传/下载Git项目

    使用Eclipse上传/下载Git项目 前提: Eclipse已安装EGit插件 已拥有GitLab / GitHub / 其它Git托管服务账号 SSH方式 配置 配置Git信息 配置用户信息 Ec ...

  5. 编写一个make

    一.简介 How to make a "make"?在进行实现前,应该先对make有一个最基本的了解.这里稍作简介:当一个程序的源文件较少时,对其进行修改并重新生成可执行文件并不复 ...

  6. pip 安装 MySQL-python 失败

    今天在安装 MySQL-python 提示 EnvironmentError: mysql_config not found 得知 mysql_config 是属于MySQL开发用的文件,而使用apt ...

  7. 数据库一些常用的SQL语句

    1.多表连接查询: 假设现在有三个表,One,Two,Three: One表字段:Code(主键),Name Two表字段:Birthday,T_code(One表Code的外键) Three表字段: ...

  8. C++设计模式-策略模式(2)

    策略模式:策略模式针对一组算法,将每一个算法封装到具有共同接口的独立的类中从而使得它们可以相互替换. 策略模式使得算法可以在不影响到客户端的情况下发生变化.策略模把行为和环境分开.环境类负责维持和查询 ...

  9. Button四角有弧度及按下显示不同的颜色

    一般的button都是矩形或者正方形,但为了显示不同的效果,让界面更美化,可以对其进行处理!!! 1.四角有弧度的button 2.按下button显示不同的颜色 实现步骤: 首先在drawable文 ...

  10. 2013 acm 长沙网络赛 G题 素数+枚举 Goldbach

    题目 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3856 先预处理求出两个素数的和与积,然后枚举n-prime和n/pr ...