@Repository、@Service、@Controller 和 @Component
@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相同。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。