FlowingSun

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

上下文

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;
            }
        }
    }
}
View Code

方法类

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测试");
        }
    }
}
View Code

方法调用测试

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();
        }
    }
}
View Code

配置文件(嵌入的资源)

<?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>
View Code

 

posted on 2013-11-06 00:57  FlowingSun  阅读(241)  评论(0编辑  收藏  举报