`
sunbin
  • 浏览: 349540 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Spring注解@Component、@Repository、@Service、@Controller区别

 
阅读更多

今天把以前做的一个项目翻出来看一下发现用·@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泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。


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

 

在一个稍大的项目中,如果组件采用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.      
  10.     <context:component-scan base-package=”com.eric.spring”>     
  11. </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”)来改变。

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


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


 

注入方式:

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

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


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

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

分享到:
评论

相关推荐

    Spring注解@Component、@Repository、@Service、@Controller区别.doc

    Spring 注解@Component、@Repository、@Service、@Controller 区别 在 Spring 框架中,@Component、@Repository、@Service、@Controller 是四个常用的注解,它们都是继承自 @Component 注解,用于标注不同的组件或 ...

    Spring注解 @Component、@Repository、@Service、@Controller区别

    ### Spring注解 @Component、@Repository、@Service、@Controller 的区别 #### 一、引言 在现代软件开发中,尤其是Java领域的企业级应用开发中,Spring框架因其灵活、强大的依赖注入(DI)和面向切面编程(AOP)...

    解释@Component @Controller @Service @Repository

    本篇文章将深入探讨四个关键的注解:`@Component`、`@Controller`、`@Service`和`@Repository`,它们是Spring框架中的核心注解,用于定义不同层次的组件。 首先,`@Component`注解是最基础的,它代表了一个通用的...

    Spring Boot技术知识点:如何深入理解@Component注解

    在Spring Boot中,通过`@SpringBootApplication`注解,Spring会默认进行组件扫描,寻找类路径下标记了`@Component`、`@Service`、`@Repository`和`@Controller`的类。这些注解都是`@Component`的特殊形式,分别对应...

    spring注解笔记

    在本节中,我们主要介绍几个Spring中常用的注解,它们分别是@Component、@Controller、@Service和@Repository,这些注解用于将Java类声明为Spring管理的Bean。 #### 2. @Component注解 @Component是一个通用的构...

    Spring注解 - 52注解 - 原稿笔记

    在火狐中显示可能会有问题,大家都是... @RequestMapping , @RequestParam , @Resource , @ResponseBody , @RestController , @Scope , @Service , @Validated , @Value , @WebFilter , @WebInitParam , @WebListener

    spring @Component注解原理解析

    在 Spring 框架中,@Component 注解有多种衍生注解,如 @Controller、@Service、@Repository 等,这些注解都是 @Component 注解的子集,它们分别用于标注控制器、业务层和数据访问层的 Bean。 在使用 @Component ...

    Spring @compenent注解详解

    这个注解及其派生注解(如`@Service`、`@Repository`和`@Controller`)是Spring依赖注入(Dependency Injection, DI)机制的基础。在这篇文章中,我们将深入探讨`@Component`注解的各个方面,以及如何在实际应用中...

    Spring @讲义.txt

    ### Spring 注解详解:@Component、@Repository、@Service、@Controller 区别 #### 一、Spring 注解概述 Spring 框架是 Java 开发领域中非常流行的一个轻量级框架,它通过依赖注入(DI)和面向切面编程(AOP)等...

    spring使用注解暴露remoting服务

    在Spring配置中,我们通常使用@ComponentScan注解来指定需要扫描的包,这样Spring会自动找到带有@Service、@Repository和@Component注解的类。 ```java @Configuration @ComponentScan(basePackages = {...

    spring3.x注解

    在 Spring 3.x 中,提供了多种用于类注册的注解,如 @Component、@Repository、@Service、@Controller 等。 1. @Component @Component 注解是所有受 Spring 管理组件的通用形式,默认的名称(id)是小写开头的非...

    Spring @Bean vs @Service注解区别

    最后,需要强调的是,@Bean和@Service只是Spring框架中的两个组件扫描注解,还有其他许多注解,例如@Controller、@Repository、@Component等。每个注解都有其特定的用途和使用场景,需要根据实际情况选择合适的注解...

    spring @component的作用详细介绍

    在Spring框架中,@Component及其衍生的注解@Controller、@Service、@Repository是用于实现自动化的依赖注入与组件扫描,这是Spring框架的核心功能之一,让开发者能够以声明式的方式管理Java对象的生命周期,并且将...

    spring注解实例

    常见的注解包括@Component、@Service、@Repository和@Controller,它们用于声明组件,分别对应通用、服务、数据访问和Web层。此外,@Autowired注解用于自动装配bean,@Qualifier注解用于指定特定的bean。 **2. @...

    spring注解属性的用法

    Spring提供了几个`@Component`的派生注解,如`@Service`、`@Repository`和`@Controller`,它们在功能上与`@Component`相同,但提供了语义上的区别,方便代码管理和阅读。 - `@Service`:通常用于业务逻辑层...

    浅谈spring 常用注解

    在Spring中,@Component、@Repository、@Service、@Controller等注解都是用来定义Bean的,它们之间的区别在于标识组件的类型。 * @Component:是一个基本的注解,用于标识普通的Java类。 * @Repository:用于标识...

    对Spring中注解怎么实现的一些基本原理

    在Spring中,常见的注解如`@Autowired`、`@Component`、`@Service`、`@Repository`和`@Controller`等,用于标记类、方法或字段,以便Spring容器进行自动配置和管理。 **注解的处理过程:** 1. **扫描和发现**:...

    Struts2+spring注解配置简介

    Spring提供了如@Component、@Service、@Repository和@Controller等注解,用于标记Java类作为Spring容器中的bean。例如,@Component可以用来定义一个基础组件,@Service通常用于业务逻辑层,@Repository用于数据访问...

    Spring 注解 小例子

    1. `@Component`、`@Service`、`@Repository` 和 `@Controller` 这些注解是Spring组件扫描的基础,它们定义了一个bean。`@Component`是最通用的,适用于任何类型的服务。`@Service`通常用于业务逻辑层,`@...

    最简单的一个spring注解实例

    1. **@Component** 注解:这是所有Spring组件注解的基础,如 @Service、@Repository 和 @Controller。在最简单的情况下,我们可以在一个类上使用 @Component 注解来告诉Spring容器,这个类是一个需要管理的bean。...

Global site tag (gtag.js) - Google Analytics