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
So, apart from the better exceptions, any other advantage at all? Does annotating classes have an impact on performance?
The Spring Framework provides you with some special annotations. These annotations are used to create Spring beans automatically in the application context. The main stereotype annotation is @Component .
A stereotype allows a framework developer to identify such a role and declare some common metadata for beans with that role in a central place. A bean may declare zero, one or multiple stereotypes, by applying the stereotype annotation to the bean class or producer method or field.
Spring @Component, @Repository, @Service and @Controller are Stereotype Annotations. @Component is generic stereotype annotation for any Spring-managed component. In the previous version Spring 2.0 introduce the first Stereotype Annotations name as @Repository.
Spring @Bean Annotation is applied on a method to specify that it returns a bean to be managed by Spring context. Spring Bean annotation is usually declared in Configuration classes methods. In this case, bean methods may reference other @Bean methods in the same class by calling them directly.
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 :
bean
definitions in context xml file. Instead annotate classes and use those by autowiring.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.
Component scan saves you from defining each bean manually via xml or java configuration.
Multiple stereo types are there to define layers like service layer, data layer, etc. Also based on different stereo types if you want to do something specific then you can do so.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With