@Component、@Service、@Constroller,@Repository,它们分别用于软件系统的不同层次:

  • @Component 是一个泛化的概念,仅仅表示一个组件 (Bean) ,可以作用在任何层次。
  • @Service 通常作用在业务层,但是目前该功能与 @Component 相同。
  • @Constroller 通常作用在控制层,但是目前该功能与 @Component 相同
  • @Repository通常用于将数据访问层 (DAO 层 ) 的类标识为 Spring Bean

 

为了让 Spring 能够扫描类路径中的类并识别出 @Repository 注解,需要在 XML 配置文件中启用Bean 的自动扫描功能,这可以通过<context:component-scan/>实现

<beans … > 
    ……
 <context:component-scan base-package=”bookstore.dao” /> 
  ……
</beans> 

相关文章: