@Named与相同
@Component。但是,注释
@Controller,
@Service和
@Repository更具体。
从Spring 文档中:
@Component是任何Spring托管组件的通用构造型。@Repository,@Service和@Controller分别是@Component针对特定用例的专门化
,例如分别在持久性,服务和表示层。例如,这些构造型注释成为切入点的理想目标。这也有可能是
@Repository,@Service和@Controller可以携带Spring
framework的未来版本中为更多的语义。因此,如果您在使用@Component
还是@Service在服务层之间进行选择,@Service显然是更好的选择。同样,如上所述,@Repository在持久层中已经支持将其作为自动异常转换的标记。
本节说明与的区别
@Named。
许多组件,如Spring的
DispatcherServlet(在MVC配置
WebApplicationContext)不找
Component,他们正在寻找
@Controller。因此,当它扫描您的课程时,不会在中找到它
@Named。以类似的方式,带有的事务管理会
@Transactional寻找
@Service和
@Repository,而不是寻找更通用的
@Component。



