Spring @Component,@Service,@Controller,@Repository使用说明

2015-11-26by coin, 次阅读
Spring的这些注解功能是一样的。在一个分多层的应用中,如presentation, service, business, data access不同层应使用不同注解:
@Component – generic and can be used across application.(分不清时可用这个)
@Service – annotate classes at service layer level.
@Controller – annotate classes at presentation layers level, mainly used in Spring MVC.
@Repository – annotate classes at persistence layer, which will act as database repository.

@Service, @Controller , @Repository = {@Component + some more special functionality}
You will noticed that all @Repository,@Service or @Controller are annotated with @Component.
So, can we use just @Component for all the components for auto scanning?
Yes, you can, and Spring will auto scan all your components with @Component annotated.
It’s working fine, but not a good practice, for readability, you should always declare @Repository,@Service or @Controller for a specified layer to make your code more easier to read