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