1.添加对Common.Logging.dll 、Spring.Core.dll、Spring.Web.dll、Spring.Web.Mvc3.dll的引用
2.到web.config里面配置web结点
<configSections> <!--Spring。Net配置节点--> <sectionGroup name="spring"> <section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc3"/> </sectionGroup> </configSections> <!--Spring.Net节点详细配置--> <spring> <context> <!--config文件夹下的controllers.xml文件--> <resource uri="file://~/Config/controllers.xml"/> </context> </spring>
注意: congfigsection结点放在configuration结点下的首位 , 同时建立一个Config文件夹 并在里面创建controllers.xml文件用来存储相关数据
3.global.asax文件 将MvcApplication类修改继承自SpringMvcApplication
4.在controllers.xml中添加需要注入的结点配置
例如:给RoleInfo控制器下的RoleInfoService属性注入
namespace Sept10.OA.MVC.Controllers { public class RoleInfoController : BaseController { public IRoleInfoService RoleInfoService { get; set; } // GET: /RoleInfo/ public ActionResult Index() {
//注入后不会报错 ViewData.Model = RoleInfoService.LoadEntities(u => u.ID == id).FirstOrDefault(); return View(); } } }
controllers.xml配置:
1 <!--type=命名空间+类型,程序集名称 singleton确定是否为单例模式--> 2 <object type="Sept10.OA.MVC.Controllers.RoleInfoController,Sept10.OA.MVC" singleton="false" > 3 <!--类的属性名称,ref参考来源名称--> 4 <property name="RoleInfoService" ref="RoleInfoService" /> 5 </object> 6 7 8 9 <!--配置Service-----type=需要实例化对象的命名空间+类型,程序集名称->
10 <object name="RoleInfoService" type="Sept10.OA.BLL.RoleInfoService,Sept10.OA.BLL" singleton="false" > 11 </object>
5.spring注入一般属性,也可以直接注入 。如 <property name="name" value="张三"></property>
如果是静态的属性的话,如果想让它有注入的值,那么必须先创建一个实例后,才会注入。
因为静态方法调用的时候,不需要spring容器创建实例。所以属性并没有注入进去。
例如CacheHelper类中的静态属性 ICacheWriter CacheWriter {get;set;}
那么在类的静态构造函数内部,逼迫spring容器给我们创建一个CacheHelper,那么顺便
就给当前 的静态属性CacheWriter做注入操作了。所以,下面的所有的静态方法在用的时
侯就可以有实例了。
static CacheHelper() { //创建 spring容器上下文
IApplicationContext ctx = ContextRegistry.GetContext();
var userInfoDal = ctx.GetObject("CacheHelper") as CacheHelper;
}
<object name="CacheHelper" type="Test.OA.Common.CacheHelper,Test.OA.Common" singleton="false" > <property name="CacheWriter" ref="MemmcachedWriter" /> </object> <object name="MemmcachedWriter" type="Test.OA.Common.MemmcachedWriter,Test.OA.Common" singleton="false" > </object>
spring 不仅可以注入属性 ,也可以构造器参数注入
<constructor-arg index="0" value="xxx">
其中 index表示第几个参数。
此外spring还可以替换方法,这是一种用的比较少的注入方式。在Spring的配置中,通过replaced-method在需要替换类中指定需要被替换的方法,以及被哪个类替换。替换类应该实现Spring.Objects.Factory.Support中的IMethodReplacer接口。IMethodReplacer接口只有一个object Implement(object target, MethodInfo method, object[] arguments)方法。当我们调用被替换类中相应方法时,会执行Implement方法。
如下面的例子:
<object name="UserInfoDal" type="SpringNetDemo.AdoNetUserInfoDal,SpringNetDemo"> <property name="Name" value="Demo"/> <replaced-method name="Show" replacer="TestDal"> <object id="TestDal" type="xxxxxx,xxxx">
public class TestDal : IMethodReplacer { public object Implement(object target, MethodInfo method, object[] arguments) { Console.WriteLine("类的名称:{0}",target.ToString()); Console.WriteLine("方法名为 :{0}",method.Name); if (arguments.Length > 0) { Console.WriteLine("传入的第一个参数是 :{0}", arguments[0].ToString()); } return new object(); } }
就成功的替换了userinfodal 里面的show方法。
6.控制台应用程序下 spring 通过容器创建实例 通过读取相关配置 创建容器获取对应的object节点对应的实例 如下就创建UserInfoDal了实例:
// 创建 spring容器上下文 IApplicationContext ctx = ContextRegistry.GetContext(); IUserInfoDal userInfoDal = ctx.GetObject("UserInfoDal") as IUserInfoDal;
<objects xmlns="http://www.springframework.net"> <!--放容器里面的所有的节点--> <object name="UserInfoDal" type="SpringNetDemo.AdoNetUserInfoDal,SpringNetDemo"> <property name="Name" value="Demo"/> </object> </objects>