ItGo.me - 专注IT技术分享

首页 > Spring > Spring > Spring @Component,@Service,@Controller,@Repository使用说明

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

时间:2015-11-26来源:网友分享 点击:
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


标题标题标题

内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容...

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

Spring的这些注解功能是一样的。在一个分多层的应用中,如presentation, service, business, data access 不同层使用不同注解:...

@Resource和@Autowired的区别

  @Resource和@Autowire的区别 在java代码中可以使用@Autowire或者@Resource注解方式进行装配,这两个注解的区别是: @Autowire默认按照类型装配,默认情况下它要求依赖对象必须存在如果允许为null,可...

Spring的这些注解功能是一样的。在一个分多层的应用中,如presentation, service, business, data access 不同层使用不同注解:
------分隔线----------------------------