依赖注入:属性和构造函数的注入
一个简单的Demo:
IUserInfoDal接口:
public interface IUserInfoDal { void Show(); string Name { get; set; } } }
UserInfoDal继承IUserInfoDal:
public class UserInfoDal:IUserInfoDal { public void Show() { Console.WriteLine("hellow world"); } }
建立SpringNetDemo的控制台应用程序:
static void Main(string[] args) { //IUserInfoDal userInfoDal = new UserInfoDal(); //userInfoDal.Show(); //容器来 创建UserInfoDal实列 //第一步 初始化容器 IApplicationContext ctx = ContextRegistry.GetContext(); IUserInfoDal dal = ctx.GetObject("UserInfoDal") as IUserInfoDal; dal.Show(); Console.ReadKey(); }
在App中进行配置:(<configuration> 节点中
<!--sectionGroup 中的name要与 下面的标签名字一样 spring--> <configSections> <sectionGroup name="spring"> <!--spring 分成两个子块--> <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/> <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" /> </sectionGroup> </configSections>
对sectionGroup的name进行配置:
<spring> <!--Spring.Net对象容器的配置--> <context> <!--容器里面的所有的对象在哪里配置的?--> <!--使用xml文件的配置--> <!--<resource uri="file://Dals.xml"/>--> <!--使用程序集的配置--> <!--<resource uri="assembly://SpringNetDemo/SpringNetDemo/Dals.xml"/>--> <resource uri="config://spring/objects"/> </context> <!--objects:配置的容器的里面的对象的--> <objects xmlns="http://www.springframework.net"> <description>An example that demonstrates simple IoC features.</description> <!--名字为类名--> <!--<object name="UserInfoDal" type="SpringNetDemo.UserInfoDal, SpringNetDemo">--> <object name="UserInfoDal" type="SpringNetDemo.EFUserInfoDal, SpringNetDemo">
<!--属性的配置--> <property name="Name" value="Spring牛"/> </object> <!-- 把UserInfoDal属性注入到UserInfoServce--> <!--<object name="UserInfoServce" type="SpringNetDemo.UserInfoServce, SpringNetDemo"> <property name="UserInfoDal" ref="UserInfoDal"/> </object>--> </objects> </spring>
XMl文件中的配置:
<?xml version="1.0" encoding="utf-8" ?> <objects xmlns="http://www.springframework.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd"> <object name="UserInfoDal1" type="SpringNetDemo.UserInfoDal, SpringNetDemo"> <!--<constructor-arg index="0" value="movies.txt"/>--> </object> </objects>
Spring.Net与项目结合:
1.在Web.Config中configuration 节点中进行配置
<configSections> <!-- Entity Framework 块配置--> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> <!--Spring.Net块配置--> <sectionGroup name="spring"> <section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc4"/> </sectionGroup> </configSections> <!--Spring.Net 的容器的配置节点--> <spring> <context> <resource uri="file://~/Config/services.xml"/> <resource uri="file://~/Config/controllers.xml"/> </context> </spring>
2.在Global.asax中改成Spring.Web
public class MvcApplication :Spring.Web.Mvc.SpringMvcApplication
3.在packages中引入dll文件
4.在Portal中新建文件夹Config
1.controllers.xml
<?xml version="1.0" encoding="utf-8" ?> <objects xmlns="http://www.springframework.net"> <!--前面是类的全名称 后面是类的程序集 singleton="false" 每次都是创建一个心对象 不是共用一个对象 --> <object type="SunOA.UI.Portal.Controllers.UserInfoController,SunOA.UI.Portal" singleton="false" > <!--属性的注入--> <property name="UserInfoService" ref="UserInfoService" /> </object> </objects>
2.services.xml
<?xml version="1.0" encoding="utf-8" ?> <objects xmlns="http://www.springframework.net"> <object name="UserInfoService" type="SunOA.BLL.UserInfoService, SunOA.BLL" singleton="false" > </object> <object name="OrderInfoService" type="SunOA.BLL.OrderInfoService, SunOA.BLL" singleton="false" > </object> </objects>
3.控制器。
public IUserInfoService UserInfoService { get; set; } public ActionResult Index() { ViewData.Model = UserInfoService.GetEntites(u => true); return View(); }
BLL层的代码(继承于接口):
public class OrderInfoService : BaseService<OrderInfo>, IOrderInfoService
public interface IUserInfoService : IBaseService<UserInfo>
IBLL层的代码:
public interface IOrderInfoService : IBaseService<OrderInfo>
Spring.Net程序的作用:
1.程序加载的时候,第一次请求过来的时候,初始化容器对象。
2.加载XML文件放到内存里面。
3.根据容器里面的XML配置创建对象
4.第二次和以后请求过的时候重复 第三个步骤