使用弹簧定型的优势是什么? [英] Advantages of using spring stereotypes?

查看:110
本文介绍了使用弹簧定型的优势是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用弹簧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


  1. 例外情况更好,如果该类与正确的刻板印象注释处理,OK,这是一个优势,我acknowlegde

  2. 我可以使用组件扫描services和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 - 标注您所有的DAO类与@Repository。所有的数据库访问逻辑应该在DAO类。

  • @Component - 与组件原型注释其他组件(例如REST资源类)

  • @Autowired - 让Spring自动线等豆成使用@Autowired注解你的类。

  • @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 ,例如,在持久,服务和presentation层,分别为。

@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.

原因使用它们:


  • 使用@Repository或@Service在@Component的主要优点是,它很容易编写的目标,例如,所有类注释与@Repository一个AOP切入点。

  • 您不必写定义在上下文中的XML文件。相反注解类和使用这些被自动装配。

  • 注释专门帮助清楚地划分应用层(在一个标准的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豆类及放大器的性能实际上影响;注解是相同的。组件扫描更多的是有点贵(当你扫描@服务,@Component)。注解是分析与反思,将XML - 一个XML解析器。但是,正如你所说,这是启动时间 - 它发生一次。而温和的机器上很快开始pretty即使有注解。

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.

这篇关于使用弹簧定型的优势是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆