Java 什么';是@Component、@Repository和&@春天的服务注释?

Java 什么';是@Component、@Repository和&@春天的服务注释?,java,spring,spring-mvc,annotations,Java,Spring,Spring Mvc,Annotations,和注释是否可以在Spring中互换使用,或者它们除了作为注释设备外还提供了任何特定的功能 换句话说,如果我有一个服务类,并且我将注释从@Service更改为@Component,它的行为是否仍然相同 或者注释也会影响类的行为和功能吗?它们几乎是一样的——所有这些都意味着类是一个Springbean@服务、@存储库和@控制器是专门的@组件s。您可以选择使用它们执行特定操作。例如: @Controllerbean由spring mvc使用 @Repositorybean有资格进行持久性异常转换

和注释是否可以在Spring中互换使用,或者它们除了作为注释设备外还提供了任何特定的功能

换句话说,如果我有一个服务类,并且我将注释从
@Service
更改为
@Component
,它的行为是否仍然相同


或者注释也会影响类的行为和功能吗?

它们几乎是一样的——所有这些都意味着类是一个Springbean<代码>@服务、
@存储库
@控制器
是专门的
@组件
s。您可以选择使用它们执行特定操作。例如:

  • @Controller
    bean由spring mvc使用
  • @Repository
    bean有资格进行持久性异常转换
另一件事是,您在语义上将组件指定给不同的层

@Component
提供的一件事是,您可以使用它对其他注释进行注释,然后以与
@Service
相同的方式使用它们

例如,最近我做了:

@Component
@Scope("prototype")
public @interface ScheduledJob {..}
因此,用
@ScheduledJob
注释的所有类都是Springbean,除此之外,还注册为quartz作业。您只需提供处理特定注释的代码。

来自:

@Repository
注释是满足 存储库(也称为数据访问对象)的角色或原型 或道)。该标记的用途之一是自动翻译 例外情况,如中所述

Spring提供了进一步的原型注释:
@组件
@服务
, 和
@控制器
@Component
是任何 Spring托管组件<代码>@Repository、
@Service
@Controller
@组件的专门化
用于更具体的用例(在 分别是持久性层、服务层和表示层)。 因此,您可以使用
@component
注释组件类, 但是,通过使用
@Repository
@Service
@Controller
相反,您的类更适合通过工具进行处理 或者与方面相关

例如,这些原型注释 为切入点设定理想的目标<代码>@存储库,
@服务
,以及
@Controller
还可以在未来的 Spring框架。因此,如果您选择使用
@组件
@服务
对于您的服务层,
@服务
显然是 更好的选择。类似地,如前所述,
@Repository
已经存在 在您的应用程序中支持作为自动异常转换的标记 持久层

@Component – Indicates a auto scan component.
@Repository – Indicates DAO component in the persistence layer.
@Service – Indicates a Service component in the business layer.
@Controller – Indicates a controller component in the presentation layer.
注释 意思
@组件
任何Spring托管组件的通用原型
@Repository
持久层的原型
@服务
服务层原型
@控制器
表示层的原型(SpringMVC)
从数据库连接的角度来看,使用
@Service
@Repository
注释非常重要

  • 对所有web服务类型的DB连接使用
    @Service
  • 对所有存储的proc DB连接使用
    @Repository
  • 如果未使用正确的注释,则可能会遇到由回滚事务覆盖的提交异常。在压力负载测试期间,您将看到与回滚JDBC事务相关的异常

    Spring2.5引入了更多的原型注释:@Component、@Service和@Controller@组件作为任何Spring管理组件的通用原型;然而,@Repository、@Service和@Controller作为@Component的专门化,用于更具体的用例(例如,分别在持久性层、服务层和表示层)。这意味着您可以使用@component注释组件类,但通过使用@Repository、@Service或@Controller注释它们,您的类更适合通过工具处理或与方面关联。例如,这些原型注释是切入点的理想目标。当然,@Repository、@Service和@Controller也可能在Spring框架的未来版本中包含额外的语义。因此,如果您决定在服务层使用@Component还是@Service,@Service显然是更好的选择。类似地,如上所述,@Repository已经被支持作为持久层中自动异常转换的标记

    @Component – Indicates a auto scan component.
    @Repository – Indicates DAO component in the persistence layer.
    @Service – Indicates a Service component in the business layer.
    @Controller – Indicates a controller component in the presentation layer.
    

    参考:-

    即使我们交换@Component或@Repository或@service


    它的行为将是相同的,但有一个方面是,如果我们在Spring中使用组件或@service

    @service
    @Controller
    ,以及
    @Repository
    是原型注释,它们将无法捕获与DAO相关的特定异常,而不是与存储库相关的异常,这些注释用于:

    @Controller:
    当您的请求从表示页面映射到表示层时,即表示层不会转到任何其他文件,它会直接转到
    @Controller
    类,并在
    @RequestMapping
    注释中检查请求的路径,该注释在方法调用之前编写(如有必要)

    @Service
    :所有业务逻辑都在这里,即与数据相关的计算和所有。此业务层注释中,我们的用户不直接调用持久性方法,因此将使用此注释调用此方法它将根据用户请求请求@Repository

    @Repository
    :这是Persi
    1. **@Repository**   - Automatic exception translation in your persistence layer.
    2. **@Service**      - It indicates that the annotated class is providing a business service to other layers within the application.
    
    package com.spring.anno;
    @Service
    public class TestBean
    {
        public void m1()
        {
           //business code
        }
    }
    
    package com.spring.anno;
    @Repository
    public class TestBean
    {
        public void update()
        {
           //persistence code
        }
    }
    
    @Component
    public @interface Service {
        ….
    }
    
    @Component
    public @interface Repository {
        ….
    }
    
    @Component
    public @interface Controller {
        …
    }
    
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
    
    @Service("AAA")
    public class CustomerService{
    
    CustomerService cust = (CustomerService)context.getBean("AAA");
    
    @Component
    public class AdressComp{
        .......
        ...//some code here    
    }
    
    @RestController- Declare at controller level.
    @Controller – Declare at controller level.
    @Component – Declare at Bean/entity level.
    @Repository – Declare at DAO level.
    @Service – Declare at BO level.
    
    @Component
    @Controller
    @Repository
    @Service
    @RestController