@Repository:用于将数据访问层 (DAO 层 ) 的类标识为 Spring Bean。具体只需将该注解标注在 DAO类上即可。同时,为了让 Spring 能够扫描类路径中的类并识别出 @Repository 注解,需要在 XML 配置文件中启用Bean 的自动扫描功能,这可以通过<context:component-scan/>实现。

//首先使用@Repository将DAO类声明为Beanpackagebookstore.dao;@RepositorypublicclassUserDaoImplimplementsUserDao{……}//其次,在XML配置文件中启动Spring的自动扫描功能<beans…>……<context:component-scanbase-package=”bookstore.dao”/>……</beans>@Component、@Service、@Constroller,它们分别用于软件系统的不同层次:@Component是一个泛化的概念,仅仅表示一个组件(Bean),可以作用在任何层次。@Service通常作用在业务层,但是目前该功能与@Component相同。@Constroller通常作用在控制层,但是目前该功能与@Component相同。