详解 Spring 3.0 基于 Annotation 的依赖注入实现.docxVIP

  • 4
  • 0
  • 约2.56万字
  • 约 12页
  • 2016-11-28 发布于重庆
  • 举报

详解 Spring 3.0 基于 Annotation 的依赖注入实现.docx

详解 Spring 3.0 基于 Annotation 的依赖注入实现

使用 @Repository、@Service、@Controller 和 @Component 将类标识为 Bean Spring 自 2.0 版本开始,陆续引入了一些注解用于简化 Spring 的开发。@Repository 注解便属于最先引入的一批,它用于将数据访问层 (DAO 层 ) 的类标识为 Spring Bean。具体只需将该注解标注在 DAO 类上即可。同时,为了让 Spring 能够扫描类路径中的类并识别出 @Repository 注解,需要在 XML 配置文件中启用 Bean 的自动扫描功能,这可以通过 context:component-scan/ 实现。如下所示: // 首先使用 @Repository 将 DAO 类声明为 Bean package bookstore.dao; @Repository public class UserDaoImpl implements UserDao{ …… } // 其次,在 XML 配置文件中启动 Spring 的自动扫描功能 beans … …… context:component-scan base-package=”bookstore.dao” / …… /beans 如此,我们就不再需要在 XML 中显式使用 bean/ 进行 Bean 的配置。Spring 在容器初

文档评论(0)

1亿VIP精品文档

相关文档