<? 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>
这里注意:配置放在<startup>标签后面会报错 “Spring.Context.Support.ContextRegistry”的类型初始值设定项引发异常,放在<startup>标签前面就可以了。
public class UserInfoDal : IUserInfoDal { public string Name { get; set; } public void Show() { Console .WriteLine("zjh and "+Name ); } }
Program里面
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(); } }
结果 zjh and ctt
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>
program代码
static void Main( string[] args) { //IApplicationContext是Spring里面的一个超类,主要操作类 IApplicationContext ctx = ContextRegistry .GetContext(); //创建出对象,主要在配置文件中对UserInfoService的UserInfoDal属性进行复制 UserInfoService userInfoService = ctx.GetObject( "UserInfoService" ) as UserInfoService; userInfoService.Show(); Console.ReadKey(); }
结果 zjh and ctt