`
endual
  • 浏览: 3544468 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

@Component,@Service,@Controller,@Repository注解

    博客分类:
  • JPA
 
阅读更多

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

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

1. <?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" 
2. > 
3.  
4. <context:component-scan base-package=”com.eric.spring”>  
5. </beans>  
6.
其中base-package为需要扫描的包(含所有子包) @Service用于标注业务层组件,@Controller用于标注控制层组件(如struts中的action,@Repository用于标注数据访问组件,即DAO组件,而@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。  
7. @Service public class VentorServiceImpl implements iVentorService {  
8. } @Repository public class VentorDaoImpl implements iVentorDao { 
9. } getBean
的默认名称是类名(头字母小写),如果想自定义,可以@Service(“aaaaa”)这样来指定,这种bean默认是单例的,如果想改变,可以使用@Service(“beanName”) @Scope(“prototype”)来改变。可以使用以下方式指定初始化方法和销毁方法(方法名任意): @PostConstruct public void init() { 
10. } 
11. @PreDestroy public void destory() { 
12. }

<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->

注入方式:

DAO实现类注入到service实现类中,把service的接口(注意不要是service的实现类)注入到action中,注

入时不要new 这个注入的类,因为spring会自动注入,如果手动再new的话会出现错误,然后属性加上

@Autowired
后不需要getter()setter()方法,Spring也会自动注入。至于更具体的内容,等对注入的方式更

加熟练后会做个完整的例子上来。

注解:

spring的配置文件里面只需要加上<context:annotation-config/> <context:component-scan base-package="需要实现注入的类所在包"/>,可以使用base-package="*"表示全部的类。  

<context:component-scan base-package=”com.eric.spring”>

其中base-package为需要扫描的包(含所有子包)

在接口前面标上@Autowired@Qualifier注释使得接口可以被容器注入,当接口存在两个实现类的时候必须指定其中一个来注入,使用实现类首字母小写的字符串来注入,如:

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

否则可以省略,只写@Autowired  

@Service
服务层组件,用于标注业务层组件,表示定义一个bean,自动根据bean的类名实例化一个首写字母为小写的bean,例如Chinese实例化为chinese,如果需要自己改名字则:@Service("你自己改的bean")  

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

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

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


@Service
public class VentorServiceImpl implements iVentorService {
}

@Repository
public class VentorDaoImpl implements iVentorDao {
}

getBean
的默认名称是类名(头字母小写),如果想自定义,可以@Service(“aaaaa”) 这样来指定,这种

bean
默认是单例的,如果想改变,可以使用@Service(“beanName”) @Scope(“prototype”)来改变。

可以使用以下方式指定初始化方法和销毁方法(方法名任意):

@PostConstruct

public void init() {

}

@PreDestroy

public void destory() {

}

分享到:
评论

相关推荐

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

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

    解释@Component @Controller @Service @Repository

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

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

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

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

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

    spring @Component注解原理解析

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

    spring注解笔记

    通过base-package属性指定需要扫描的包路径,Spring将会扫描该包及其子包下带有@Component、@Controller、@Service或@Repository注解的类,并自动注册为Bean。 #### 8. 多包扫描配置 在"context:component-scan...

    spring @component的作用详细介绍

    默认情况下,使用@Component、@Service、@Controller或@Repository注解的类实例化的对象都是单例的,也就是在整个Spring容器中只创建一次,所有对这个对象的引用都指向同一个实例。如果需要改变这个默认行为,可以...

    Spring @Bean vs @Service注解区别

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

    Spring @compenent注解详解

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

    @Commponent注解HelloWorld示例

    在本示例中,我们可能没有XML配置,而是使用了Java配置类,并使用`@ComponentScan`注解来指示Spring去寻找那些带有`@Component`(以及其子注解如`@Service`、`@Repository`和`@Controller`)的类。 3. **@Component...

    spring3.x注解

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

    spring使用注解暴露remoting服务

    4. **@Component**: 如果@Service、@Repository或@Controller不适用,可以使用@Component作为通用注解标记任何组件。Spring会同样对其进行扫描和管理。 **远程调用注解** 1. **@WebService**: 对于基于SOAP的Web...

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

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

    Spring @讲义.txt

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

    Spring的自动扫描注入.docx

    在 Spring 2.5 中,引入了组件自动扫描机制,该机制可以在类路径下寻找标注了 @Component、@Service、@Controller、@Repository 注解的类,并将这些类纳入 Spring 容器中管理。 @Component、@Repository、@Service...

    spring中Resource和Component注解

    `@Component`的派生注解如`@Service`、`@Repository`和`@Controller`,则是为不同类型的业务层对象提供了更明确的语义。 接着,`@Resource`注解则主要用于注入bean的依赖。它使用JavaBeans属性名称来查找对应的bean...

    spring注解属性的用法

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

    浅谈spring 常用注解

    例如,使用@Repository注解来标识数据访问层,使用@Service注解来标识业务层,使用@Controller注解来标识控制层。 需要注意的是,在使用这些注解时,需要遵循Spring的命名约定和编程规范,以便提高代码的可读性和...

    spring的配置注解

    Spring提供了一些特定领域的`@Component`扩展,如`@Service`(用于业务层),`@Repository`(用于数据访问层)和`@Controller`(用于表现层)。这些注解可以进一步强化代码的语义,但功能上与`@Component`并无本质...

    spring3.0依赖注入详解

    本文将深入探讨Spring 3.0中依赖注入的新特性,特别是如何使用`@Repository`、`@Service`、`@Controller`和`@Component`注解来标记类为Bean,以及如何利用`&lt;context:component-scan/&gt;`元素自动扫描和注册这些Bean。...

Global site tag (gtag.js) - Google Analytics