日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

Spring.Net学习

發(fā)布時(shí)間:2025/7/14 asp.net 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring.Net学习 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Spring.NET IoC容器的用法。

通過簡單的例子學(xué)習(xí)Spring.Net

1、先創(chuàng)建一個(gè)控制臺(tái)程序項(xiàng)目。

2、添加IUserInfoDal 接口。

namespace Spring.Net
{
public interface IUserInfoDal
{
void Show();
}
}

?

3、添加AdoNetUserInfoDal類和EFUserInfoDal類,繼承IUserInfoDal接口。

AdoNetUserInfoDal.cs

public class AdoNetUserInfoDal:IUserInfoDal{public void Show(){Console.WriteLine("我是 AdoNet Dal ); }}

EFUserInfoDal.cs

public class EFUserInfoDal: IUserInfoDal{public void Show(){Console.WriteLine("我是EF Dal); } }

4、引用Spring.Net程序集 Spring.Core.dll 和 Common.Logging.dll

5、添加Spring.Net配置節(jié)點(diǎn),配置object節(jié)點(diǎn)

<?xml version="1.0" encoding="utf-8" ?> <configuration><!--一定要在緊跟著configuration下面添加--><configSections><!--跟下面Spring.Net節(jié)點(diǎn)配置是一一對(duì)應(yīng)關(guān)系--><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><startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /></startup><!--Spring.Net節(jié)點(diǎn)配置--><spring><context><!--容器配置--><resource uri="config://spring/objects"/></context><objects xmlns="http://www.springframework.net"><!--這里放容器里面的所有節(jié)點(diǎn)--><description>An example that demonstrates simple IoC features.</description><!--name 必須要唯一的,type=類的全名稱,所在的程序集--><object name="UserInfoDal" type="Spring.Net.EFUserInfoDal, Spring.Net"></object> </spring> </configuration>

6、開始寫主函數(shù),創(chuàng)建spring容器上下文

namespace Spring.Net {class Program{static void Main(string[] args){//控制權(quán)沒有反轉(zhuǎn)//IUserInfoDal infoDal = new EFUserInfoDal();//Spring.Net 創(chuàng)建實(shí)例的方式轉(zhuǎn)為容器幫我們創(chuàng)建//創(chuàng)建spring容器上下文IApplicationContext ctx = ContextRegistry.GetContext();//通過容器創(chuàng)建對(duì)象IUserInfoDal efDal = ctx.GetObject("UserInfoDal") as IUserInfoDal;efDal.Show();Console.ReadKey();}} }

7、屬性注入

<!--Spring.Net節(jié)點(diǎn)配置--><spring><context><!--容器配置--><resource uri="config://spring/objects"/></context><objects xmlns="http://www.springframework.net"><!--這里放容器里面的所有節(jié)點(diǎn)--><description>An example that demonstrates simple IoC features.</description><!--name 必須要唯一的,type=類的全名稱,所在的程序集--><object name="UserInfoDal" type="Spring.Net.EFUserInfoDal, Spring.Net"><property name="Name" value="張三"/><!--ref指向下面的屬性注入--><property name="UserInfo" ref="UserInfo"/></object> <!--屬性注入--><object name="UserInfo" type="Spring.Net.UserInfo, Spring.Net"><property name="Name" value="李四"/><property name="Age" value="15"/></object></objects></spring>

8、構(gòu)造函數(shù)注入

<!--構(gòu)造函數(shù)注入--> <object name="UserInfoDal2" type="Spring.Net.AdoNetUserInfoDal, Spring.Net"><constructor-arg index="0" value="張三"/><constructor-arg index="1" ref="UserInfo"/> </object>

9、容器配置

<context><!--容器配置--><resource uri="config://spring/objects"/><!--xml文件方式,更改屬性,復(fù)制到輸出目錄:始終復(fù)制--><!--<resource uri="file://objects.xml"/>--><!--嵌入程序集方式,assembly://程序集名/項(xiàng)目名/objects.xml,更改屬性,始終復(fù)制,生成操作,嵌入的資源--><!--<resource uri="assembly://Spring.Net/Spring.Net/objects.xml"/>--> </context>

10、完整例子

IUserInfoDal.cs

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace Spring.Net {public interface IUserInfoDal{UserInfo UserInfo { get; set; }string Name { get; set; }void Show();} }

AdoNetUserInfoDal.cs

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace Spring.Net {public class AdoNetUserInfoDal:IUserInfoDal{public AdoNetUserInfoDal(string name, UserInfo userInfo){Name = name;UserInfo = userInfo;}public UserInfo UserInfo { get; set; }public string Name { get; set; }public void Show(){Console.WriteLine("我是 AdoNet Dal ,屬性注入:Name=" + Name);Console.WriteLine("UserInfo ,Name=" + UserInfo.Name + " Age=" + UserInfo.Age);}} }

EFUserInfoDal.cs

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace Spring.Net {public class EFUserInfoDal: IUserInfoDal{public EFUserInfoDal(){}public UserInfo UserInfo { get; set; }public string Name { get; set; }public void Show(){Console.WriteLine("我是EF Dal,屬性注入:Name=" + Name);Console.WriteLine("UserInfo ,Name=" + UserInfo.Name + " Age=" + UserInfo.Age);} } }

App.config

<?xml version="1.0" encoding="utf-8" ?> <configuration><!--一定要在緊跟著configuration下面添加--><configSections><!--跟下面Spring.Net節(jié)點(diǎn)配置是一一對(duì)應(yīng)關(guān)系--><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><startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /></startup><!--Spring.Net節(jié)點(diǎn)配置--><spring><context><!--容器配置--><resource uri="config://spring/objects"/><!--xml文件方式,更改屬性,復(fù)制到輸出目錄:始終復(fù)制--><!--<resource uri="file://objects.xml"/>--><!--嵌入程序集方式,assembly://程序集名/項(xiàng)目名/objects.xml,更改屬性,始終復(fù)制,生成操作,嵌入的資源--><!--<resource uri="assembly://Spring.Net/Spring.Net/objects.xml"/>--></context><objects xmlns="http://www.springframework.net"><!--這里放容器里面的所有節(jié)點(diǎn)--><description>An example that demonstrates simple IoC features.</description><!--name 必須要唯一的,type=類的全名稱,所在的程序集--><object name="UserInfoDal" type="Spring.Net.EFUserInfoDal, Spring.Net"><property name="Name" value="張三"/><!--ref指向下面的屬性注入--><property name="UserInfo" ref="UserInfo"/></object> <!--構(gòu)造函數(shù)注入--><object name="UserInfoDal2" type="Spring.Net.AdoNetUserInfoDal, Spring.Net"><constructor-arg index="0" value="張三"/><constructor-arg index="1" ref="UserInfo"/></object><!--屬性注入--><object name="UserInfo" type="Spring.Net.UserInfo, Spring.Net"><property name="Name" value="李四"/><property name="Age" value="15"/></object></objects></spring> </configuration>

Program.cs

using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;//Spring.Aop.dll 面向切面編程 //Spring.Core.dll spring框架基礎(chǔ) //Common.Logging.dll 這個(gè)必須也要引用namespace Spring.Net {class Program{static void Main(string[] args){//控制權(quán)沒有反轉(zhuǎn)//IUserInfoDal infoDal = new EFUserInfoDal();//Spring.Net 創(chuàng)建實(shí)例的方式轉(zhuǎn)為容器幫我們創(chuàng)建//第一步,引用Spring.Net程序集 Spring.Core.dll 和 Common.Logging.dll//第二步,添加Spring.Net配置節(jié)點(diǎn)//第三步,配置object節(jié)點(diǎn)//第四步,創(chuàng)建spring容器上下文IApplicationContext ctx = ContextRegistry.GetContext();//第五步,通過容器創(chuàng)建對(duì)象IUserInfoDal efDal = ctx.GetObject("UserInfoDal") as IUserInfoDal;efDal.Show();IUserInfoDal adoDal = ctx.GetObject("UserInfoDal2") as IUserInfoDal;adoDal.Show();Console.ReadKey();}} }

?

轉(zhuǎn)載于:https://www.cnblogs.com/luohao123/p/8909425.html

總結(jié)

以上是生活随笔為你收集整理的Spring.Net学习的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。