问题描述
我正在使用 spring-mvc 开发一个 Web 应用程序.
I am developing a web application using spring-mvc.
现在@Controller、@Service 和@Repository 构造型可用.
Now the @Controller, @Service and @Repository stereotypes are available.
我发现@Controller 特别有用,特别是因为我正在使用
I found @Controller particulary useful, specially because I am using
<context:component-scan base-package="my.cool.controller"/>
现在,关于@Service 和@Repository,到目前为止看起来像
Now, regarding @Service and @Repository, so far looks like
- 如果使用正确的构造型注释类,则可以更好地处理异常,好的,我承认这是一个优势
- 我可以对服务和 DAO/存储库使用组件扫描,但是我不喜欢使用组件扫描的想法,因为它会减慢应用程序的启动时间,这对我来说是一个关键功能(即使只需 1 秒,我每周重新部署一次)
那么,除了更好的例外,还有其他优势吗?注释类对性能有影响吗?
So, apart from the better exceptions, any other advantage at all? Does annotating classes have an impact on performance?
推荐答案
刻板印象解释:
@Service
- 使用 @Service 注释所有服务类.该层知道工作单元.您所有的业务逻辑都将在服务类中.通常,服务层的方法都包含在事务中.您可以从服务方法进行多次 DAO 调用,如果一个事务失败,所有事务都应该回滚.@Repository
- 使用 @Repository 注释所有 DAO 类.您所有的数据库访问逻辑都应该在 DAO 类中.@Component
- 使用组件原型注释您的其他组件(例如 REST 资源类).@Autowired
- 让 Spring 使用 @Autowired 注解将其他 bean 自动连接到您的类中.
@Service
- Annotate all your service classes with @Service. This layer knows the unit of work. All your business logic will be in Service classes. Generally methods of service layer are covered under transaction. You can make multiple DAO calls from service method, if one transaction fails all transactions should rollback.@Repository
- Annotate all your DAO classes with @Repository. All your database access logic should be in DAO classes.@Component
- Annotate your other components (for example REST resource classes) with component stereotype.@Autowired
- Let Spring auto-wire other beans into your classes using @Autowired annotation.
@Component
是任何 Spring 管理的组件的通用构造型.@Repository
、@Service
和 @Controller
是 @Component
针对更具体用例的特化,例如,分别在持久层、服务层和表示层中.
@Component
is a generic stereotype for any Spring-managed component. @Repository
, @Service
, and @Controller
are specializations of @Component
for more specific use cases, for example, in the persistence, service, and presentation layers, respectively.
使用它们的原因:
- 与@Component 相比,使用@Repository 或@Service 的主要优势在于,它很容易编写一个针对例如所有使用@Repository 注释的类的AOP 切入点.
- 您不必在上下文 xml 文件中编写
bean
定义.而是注释类并通过自动装配来使用它们. - 专门的注释有助于清楚地划分应用层(在标准的 3 层应用中).
- The main advantage of using @Repository or @Service over @Component is that it's easy to write an AOP pointcut that targets, for instance, all classes annotated with @Repository.
- You don't have to write
bean
definitions in context xml file. Instead annotate classes and use those by autowiring. - Specialized annotations help to clearly demarcate application layers (in a standard 3 tiers application).
现在,使用上下文 xml bean 对性能的实际影响注释是一样的.组件扫描有点贵(当您扫描@Service 时,@Component).注释通过反射解析",xml - 使用 xml 解析器.但是,正如您所说,这是启动时间 - 它只发生一次.在中等机器上,即使有注释,它也能很快启动.
Now, Practically performance impact of using context xml beans & annotations is the same. Component scanning is a bit more expensive (when you scan for @Service, @Component). The annotations are 'parsed' with reflection, the xml - with an xml parser. But, as you said, it is startup-time - it happens only once. And on a moderate machine it starts pretty quickly even with annotations.
这篇关于使用弹簧定型的优点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!