`

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

 
阅读更多

很长时间没做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泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

[java] view plain copy
  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节点配置组件时一样的。要使用自动扫描机制,我们需要打开以下配置信息:

代码

[html] view plain copy
  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”)来改变。

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

[java] view plain copy
  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注释使得接口可以被容器注入,如:

[java] view plain copy
  1. @Autowired  
  2. @Qualifier("chinese")  
  3. private Man man;   

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

 

附加:

@RequestMapping(value = "/produces", produces = "application/json"):表示将功能处理方法将生产json格式的数据,此时根据请求头中的Accept进行匹配,如请求头“Accept:application/json”时即可匹配; @RequestMapping(value = "/produces", produces = "application/xml"):表示将功能处理方法将生产xml格式的数据,此时根据请求头中的Accept进行匹配,如请求头“Accept:application/xml”时即可匹配。 此种方式相对使用@RequestMapping的“headers = "Accept=application/json"”更能表明你的目的。 服务器控制器代码详解cn.javass.chapter6.web.controller.consumesproduces.ProducesController;

分享到:
评论

相关推荐

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

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

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

    Spring注解 @Component、@Repository、@Service、@Controller区别,有兴趣的可能看一下。

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

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

    Spring @讲义.txt

    Spring @讲义.txt Spring注解@Component、@Repository、@Service、@Controller区别

    Spring注解开发

    spring注解开发@PreDestroy除了@Component外,Spring提供了3个功能基本和@Component等效的注解 @Repository 用于对DAO实现类进行标注 @Service 用于对Service实现类进行标注 @Controller 用于对Controller实现类进行...

    Spring核心注解深入解析:提升开发效率

    在这份文档中,我们深入探讨了Spring的核心注解,包括但不限于@Component、@Repository、@Service、@Controller和@Autowired。这些注解简化了配置过程,减少了样板代码,并使得组件之间的耦合度降低,更有利于单元...

    Spring注释 注入方式源码示例,Annotation

    凡带有@Component,@Controller,@Service,@Repository 标志的等于告诉Spring这类将自动产生对象,而@Resource则等于XML配置中的ref,告诉spring此处需要注入对象,所以用@Resource就有了ref的功效。 要用注解注入方式...

    Java面试可能问的问题.docx

    面试遇到的问题 1.spring的AOP/IOC怎么用 Ioc: ...Aop: ... 2.设计模式 单例模式 ...3.Spring的优越性 ...4.SpringMVC注解 @Controller ...@Component 在类定义之前添加@...@Service 用于对业务逻辑层进行注解, (特殊的@Compone

    SpringBoot常用注解详解含使用示例(值得珍藏)

    本文将详细介绍Spring Boot中最常用的注解,包括@SpringBootApplication、@Component、@Service、@Repository、@Controller、@RequestMapping、@GetMapping、@PostMapping、@PutMapping、@DeleteMapping、@Autowired...

    基于框架的Web开发-装配Bean自动装配.doc

    项目分层之后(引入dao,service,web层之后), @Component注解还有三个分身---@repository ,@Service,@Controller。这三个注解怎么用,以后再说,目前都使用@Component。 1.1 为Car类加@Component注解 注解也是要用...

    spring mvc 注解

    2. Controller 注解 3. Service 注解 4. Component 注解 5. Repository 注解 6. CookieValue 注解 7. PathVariable 注解 8. RequestBody 注解 9. RequestHeader 注解 10. RequestMethod 类 11. RequestParam 注解 12...

    springboot学习思维笔记.xmind

    @Service在业务逻辑层(service层) @Repository在数据访问层(dao层) @Controller在展现层(MVC→SpringMVC) 注入Bean的注解 @Autowired:Spring提供的注解 @Inject:JSR-330提供的注解 ...

    总结Spring注解第一篇

    包扫描@ComponentScan+组件标注注解(@Controller/@Service/@Repository/@Component)一般作用与自己写的类。 includeFilters表示包扫描的条件,起到过滤的作用。默认@ComponentScan会扫描标注这四个注解,将标注这...

    IOC 基于 注解方式 实现- 半自动化配置

    @Service: 一般用来修饰 业务service层 @Repository: 一般用来修饰 数据访问dao层 @Component: 当一个类 , 分不清是 哪个层 可以用这个注解来修饰 @Controller: 一般用来修饰 控制层 @Autowired @Qualifier ...

    Spring Boot知识点复习

     @Service  @Component  @Repository  @Controller    @Bean : 用在方法上面,将方法的返回值装配成Bean 2、AOP的具体应用 3、MVC 4、RESTfull 5、DAO层  MySQL、MyBatis  NoSQL  事务  6、拦截器  7、...

    Spring组件自动扫描详解及实例代码

    Spring组件自动扫描详解及实例代码 ...其他特定的注解有@Repository、@Service和@Controller,它们分别标识了持久层、服务处和表现层的组件。 实现方法 User.Java package com.zzj.bean; impor

    spring02-5

    类的注解步骤:开启类扫描、添加注解(Component、Repository 、Service 、Controller )

    Angular 理解module和injector,即依赖注入

    依赖注入(DI)的好处不再赘言,使用过spring框架的都知道。...比如spring中,服务的注册是通过xml配置文件的标签或是注解@Repository、@Service、@Controller、@Component实现的;对象的获取可以Applicati

    spring-boot-annotation-list:Spring Boot应用程序中常用注解的精选列表

    Spring Boot应用程序中常用注释的列表 本文档包含Spring Boot应用程序中常用注释的不完整...@Service-将带注释的类标记为Bean(通常包含业务逻辑的约定) @Repository-将带注释的类标记为Bean(通常提供数据访问的

    Spring.html

    Component:不分层的注解 Controller:web层 Service:service层 Repository:dao层 管理对象 注入 AutoWired Qualifier Resource Value 声明周期 Scope PostConstruct PreDestroy 新注解 ...

Global site tag (gtag.js) - Google Analytics