释加牟尼真身佛指舍利:Spring and Struts 2 Spring的好处,避免硬编码,处理依赖
来源:百度文库 编辑:九乡新闻网 时间:2024/05/15 11:36:58
如在EditAction 中存在EditService
private EditService editService = new EditServiceInMemory();
则出现硬编码,要使用EditService,就必须NEW一个EditServiceInMemory
The above statement hard-codes a dependency between the EditActionclass and the EditServiceInMemory class. This is poor design for tworeasons.
- If I need to replace the EditServiceInMemory with another class that implements the EditService interface I'll have to hunt down and replace all statements where I hard-coded the dependency.
- I cannot test EditAction without using the EditServiceInMemory class. I cannot isolate EditAction by using a stub implementation of EditService when writing my test case because the use of EditServiceInMemory is hard-coded.
Spring provides a mechanism to manage dependencies by injecting themat run time. Struts 2 ActionSupport classes—like any other Javaclass—can be injected with a dependent object by the Spring framework.So instead of having the above code, I would have this statement inEditAction.
EditAction Class No Hard-Coded Dependency
private EditService editService ;
At run time the Spring framework will provide an object of a class that implements the EditService interface.
Spring and Struts 2 Spring的好处,避免硬编码,处理依赖
Spring与Struts的区别
Struts Hibernate Spring 概要
spring struts hibernate分页实例
Integrating Spring and EHCache
Spring Struts Hibernate快速入门实例
一个spring的简单例子2
spring的ioc实
Spring的AOP例子
Struts VS Spring两种MVC框架比较
Spring注解详解2
spring面试题(2)
spring jar包的相关性
Spring 系列,第 2 部分: 当 Hibernate 遇上 Spring
spring MVC 基本操作方法2
J2EE学习之Spring(2) 配置DispatcherServlet - 我的文章 -...
spring festival
Spring colors
【引用】Spring
spring 定时器
Spring入门教程
Spring 入门教程
Spring定时器
Spring 系列: Spring 框架简介