Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
112 views
in Technique[技术] by (71.8m points)

java - Advantages of using spring stereotypes?

I am developing a web application using spring-mvc.

Now the @Controller, @Service and @Repository stereotypes are available.

I found @Controller particulary useful, specially because I am using

<context:component-scan base-package="my.cool.controller"/>

Now, regarding @Service and @Repository, so far looks like

  1. The exceptions are better handled if the class is annotated with the correct stereotype, ok, that is an advantage I acknowlegde
  2. I could use component-scan for services and DAOs/repositories, however I do not like the idea of using component-scan, since it slows the startup time of the application, and that is a key feature for me (even if it is only 1 sec and I redeploy once per week)

So, apart from the better exceptions, any other advantage at all? Does annotating classes have an impact on performance?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Explanation of stereotypes :

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

Reasons to use them :

  • 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).

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...