上下文
using System; using Spring.Core; using Spring.Aop; using System; using Spring.Core; using Spring.Aop; using Spring.Context; using Spring.Context.Support; namespace SpringTest { /// <summary> /// SpringFactory 的摘要说明。 /// </summary> public class SpringContext { public SpringContext() { // // TODO: 在此处添加构造函数逻辑 // } private static bool isInit = false; private static IApplicationContext context; public static void init() { string[] xmlFiles = new string[1]; xmlFiles[0] = "assembly://Spring/SpringTest/Spring_bean.xml"; //context = new XmlApplicationContext(new string[1] { "assembly://SpringTest/SpringTest/Spring_bean.xml" }); context = new XmlApplicationContext(xmlFiles); isInit = true; } public static IApplicationContext Context { get { if (!isInit) { init(); } return context; } } } }
方法类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SpringTest.Main { public class SpringMain { public SpringMain() { } public void Test() { Console.WriteLine("Spring.NET测试"); } } }
方法调用测试
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SpringTest.Main { public class SpringTest { public SpringTest() { } [STAThread] public static void MainFun() { //Startup Spring Content SpringContext.init(); //Test Spring IOC SpringMain test = (SpringMain)SpringContext.Context.GetObject("SpringTestConfigPoint"); test.Test(); } } }
配置文件(嵌入的资源)
<?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 id="SpringTestConfigPoint" type="SpringTest.Main.SpringMain,Spring" /> </objects>