private IUserService _userService;private IUserService UserService{ get { return _userService ?? (_userService = new UserService()); } set { _userService = value; }}面向接口编程,Controller应该只依赖于站点业务层的接口,而不能依赖于具体的实现,否则,就违背了在层之间设置接口的初衷了。
namespace SpringNetDemo{ public interface IClass {string Name { get; set; }Student Monitor { get; set; }string GetMsg(); } public class Class : IClass {public string Name { get; set; }public Student Monitor { get; set; }public string GetMsg(){ return "班级名称:" + Name + ",班长:" + Monitor.Name;} } public class Student {public string Name { get; set; } }}两个类,一个接口,Student类中有一个string类型的属性,为Name,Class类中除了string类型的Name属性外还有一个Student类型的Monitor属性,方法GetMsg可以返回当前Class对象的简介,包括班级名和班长名两个内容。Class类实现IClass接口。
IClass c6=new Class(){ Monitor = new Student() {Name = "李芙蓉" }, Name = "六班"};Console.WriteLine(c6.GetMsg());Console.ReadKey();
需要核心库Spring.Core.dll和Spring.Net使用的日志记录组件Common.Logging.dll
2. 然后我们需要了解当前的程序集名称和命名空间
3. 在项目中新建一个xml文件,命名为services.xml:
<?xml version="1.0" encoding="utf-8" ?><objects xmlns="http://www.springframework.net"> <description>An example that demonstrates simple IoC features.</description> <object name="Class" type="SpringNetDemo.Class,SpringNetDemo"> <property name="Name" value="尖子班"/> <property name="Monitor" ref="Student"/> </object> <object name="Student" type="SpringNetDemo.Student, SpringNetDemo"> <property name="Name" value="陈二蛋"/> </object></objects>在xml中新建objects根节点,其中加入需要容器生成的object子节点,object子节点的type属性中需要指明类的完整名称(带有程序集)和当前命名空间,如果需要为当前类的属性赋默认值,则可以在object节点中增加property节点,配置其value属性来为类的属性赋初值,若类的属性仍然为其他类对象时,可以新建该类型的object节点,并给予其name属性,再在当前属性的property节点中将ref属性,指向新增object节点的name属性。
<?xml version="1.0" encoding="utf-8" ?><configuration> <configSections> <sectionGroup name="spring"><section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/><section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" /> </sectionGroup> </configSections> <spring> <context><resource uri="file://services.xml"/> </context> </spring></configuration>
MVC项目中需要引用的dll文件稍多些,需要五个,除了值钱的两个外,还需要三个Web相关的dll。
2. 为了便于管理,我们在MVC项目更目录新建Config文件夹来保存配置文件,并在其中新建两个xml文件
controllers.xml:
<?xml version="1.0" encoding="utf-8" ?><objects xmlns="http://www.springframework.net">、 <object type="PMS.WebApp.Controllers.UserController , PMS.WebApp" singleton="false" > <property name="UserService" ref="UserService" /> </object></objects>services.xml:
<?xml version="1.0" encoding="utf-8" ?><objects xmlns="http://www.springframework.net"> <object name="UserService" type="PMS.BLL.UserService, PMS.BLL" singleton="false" > </object></objects>同样是出于方便管理考虑,我们将控制器和业务类分两个文件来保存,文件中节点的规则与控制台示例中完全相同。
//private IUserService _userService;//private IUserService UserService//{// get { return _userService ?? (_userService = new UserService()); }// set { _userService = value; }//}private IUserService UserService { get; set; }以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。