<? xml version =" 1.0 " encoding =" utf-8 " ?> < configuration> < configSections> < sectionGroup name = "spring " > <!-- 這下面有context和object在下面都有對象的標籤 --> < section name = "context " type = "Spring.Context.Support.ContextHandler, Spring.Core " /> < section name = "objects " type = "Spring.Context.Support.DefaultSectionHandler, Spring.Core " /> </ sectionGroup > </ configSections> < spring> <!-- Spring.Net對象容器的配置 --> < context > <!-- 容器的全部的對象在哪裏,這裏用uri說明地址 --> < resource uri = "config://spring/objects " /> <!-- 可使用外部xml文件 --> </ context > <!-- objects:配置的容器裏面的對象 --> < objects xmlns = "http://www.springframework.net " > < description >An example that demonstrates simple IoC features. </ description> <!-- name最好用類名,type第一個是類的全稱加上程序集,後面一個是項目名稱 --> < object name = "UserInfoDal " type = "SpringNetDemo.UserInfoDal, SpringNetDemo " > <!-- 在這裏是設置對象的屬性,將Name的值設置成ctt --> < property name = "Name " value = "ctt " /> </ object > </ objects > </ spring> </ configuration>
public class UserInfoDal : IUserInfoDal { public string Name { get; set; } public void Show() { Console .WriteLine("zjh and "+Name ); } }
class Program { static void Main(string [] args) { //IApplicationContext是Spring裏面的一個超類,主要操做類 IApplicationContext ctx = ContextRegistry .GetContext(); //GetObject從配置文件中讀取信息後,反射產生相應的對象,用as強轉成對象的接口 IUserInfoDal userInfoDal = ctx.GetObject("UserInfoDal" ) as IUserInfoDal; //輕鬆加愉快,就這樣出來了 userInfoDal.Show(); Console .ReadKey(); } }
public class UserInfoService { public IUserInfoDal UserInfoDal { get ; set; } public void Show() { UserInfoDal.Show(); Console .WriteLine("it is service" ); } }
<? xml version =" 1.0 " encoding =" utf-8 " ?> < configuration> < configSections> < sectionGroup name = "spring " > <!-- 這下面有context和object在下面都有對象的標籤 --> < section name = "context " type = "Spring.Context.Support.ContextHandler, Spring.Core " /> < section name = "objects " type = "Spring.Context.Support.DefaultSectionHandler, Spring.Core " /> </ sectionGroup > </ configSections> < spring> <!-- Spring.Net對象容器的配置 --> < context > <!-- 容器的全部的對象在哪裏,這裏用uri說明 --> < resource uri = "config://spring/objects " /> <!-- 可使用外部xml文件 --> </ context > <!-- objects:配置的容器裏面的對象 --> < objects xmlns = "http://www.springframework.net " > < description >An example that demonstrates simple IoC features. </ description> <!-- name最好用類名,type第一個是類的全稱加上程序集,後面一個是項目名稱 --> < object name = "UserInfoDal " type = "SprintNetDemo.UserInfoDal, SprintNetDemo " > <!-- 在這裏是設置對象的屬性,將Name的值設置成ctt --> < property name = "Name " value = "ctt " /> </ object > <!-- 在這裏配置UserInfoService對象 --> < object name = "UserInfoService " type = "SprintNetDemo.UserInfoService, SprintNetDemo " > <!-- 在這裏配置UserInfoService對象的UserInfoDal屬性,執行上面產生的對象 --> < property name = "UserInfoDal " ref = "UserInfoDal " /> </ object > </ objects > </ spring> </ configuration>
static void Main( string[] args) { //IApplicationContext是Spring裏面的一個超類,主要操做類 IApplicationContext ctx = ContextRegistry .GetContext(); //建立出對象,主要在配置文件中對UserInfoService的UserInfoDal屬性進行復制 UserInfoService userInfoService = ctx.GetObject( "UserInfoService" ) as UserInfoService; userInfoService.Show(); Console.ReadKey(); }