C#.NET编程----Spring.NET NHibernate整合(一)
生活随笔
收集整理的這篇文章主要介紹了
C#.NET编程----Spring.NET NHibernate整合(一)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
第一個(gè)Spring.NET的程序
?建立項(xiàng)目
項(xiàng)目名稱為:SpringSample,NameSpace為“OKEC.Sample.Spring”。
?添加HelloTest類
HelloTest.cs
namespace?OKEC.Sample.Spring
{
????///?<summary>
????///?HelloTest?的摘要說(shuō)明。
????///?</summary>
????public?class?HelloTest
????{
????????public?HelloTest()
????????{
????????????//
????????????//?TODO:?在此處添加構(gòu)造函數(shù)邏輯
????????????//
????????}
????????public?void?Test()
????????{
????????????Console.WriteLine("This?is?Spring.NET?Sample?Test!");
????????????Console.WriteLine("Please?press?Enter?close?the?windows!");
????????????Console.ReadLine();//讓程序停留,回車關(guān)閉。
????????}
????}
}
添加Spring.NET的配置文件
文件名:Spring_bean.xml,屬性設(shè)置為:嵌入的資源/ Embedded Resource
<?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="Hello"?type="OKEC.Sample.Spring.HelloTest,SpringSample"?/>
</objects>
建立Spring.NET的容器初始化對(duì)像
SpringContext.cs using?System;
using?Spring.Core;
using?Spring.Aop;
using?System;
using?Spring.Core;
using?Spring.Aop;
using?Spring.Context;
using?Spring.Context.Support;
namespace?OKEC.Sample.Spring
{
????///?<summary>
????///?SpringFactory?的摘要說(shuō)明。
????///?</summary>
????public?class?SpringContext
????{
????????public?SpringContext()
????????{
????????????//
????????????//?TODO:?在此處添加構(gòu)造函數(shù)邏輯
????????????//
????????}
????????private?static?bool?isInit?=?false;
????????private?static?IApplicationContext?context;
????????public?static?void?init()
????????{
????????????string[]?xmlFiles?=?new?string[1];????????????
????????????xmlFiles[0]?=?"assembly://SpringSample/OKEC.Sample.Spring/Spring_bean.xml";
????????????context?=?new?XmlApplicationContext(xmlFiles);
????????????isInit?=?true;
????????}
????????public?static?IApplicationContext?Context
????????{
????????????get{
????????????????if(!isInit)
????????????????{
????????????????????init();
????????????????}
????????????????return?context;
????????????}
????????}
????}
}
添加啟動(dòng)程序
StartMain.cs
using?System;
namespace?OKEC.Sample.Spring
{
????///?<summary>
????///?StartMain?的摘要說(shuō)明。
????///?</summary>
????public?class?StartMain
????{
????????public?StartMain()
????????{
????????????//
????????????//?TODO:?在此處添加構(gòu)造函數(shù)邏輯
????????????//
????????}
????????[STAThread]
????????static?void?Main()?
????????{
????????????//Startup?Spring?Content
????????????SpringContext.init();
????????????//Test?Spring?IOC
????????????HelloTest?test?=?(HelloTest)SpringContext.Context.GetObject("Hello");
????????????test.Test();
????????}
????}
}
運(yùn)行程序
結(jié)果為:
Please?press?Enter?close?the?windows!
你的第一個(gè)Spring.NET的程序成功了!
文檔中的項(xiàng)目源代碼請(qǐng)從以下地址下載:
http://www.springframework.cn/read.php?fid=8&tid=2&toread=1
完整的文檔請(qǐng)下載PDF文檔:
http://www.springframework.cn/read.php?fid=2&tid=1&toread=1
轉(zhuǎn)載于:https://www.cnblogs.com/it1630/archive/2006/12/01/579059.html
新人創(chuàng)作打卡挑戰(zhàn)賽發(fā)博客就能抽獎(jiǎng)!定制產(chǎn)品紅包拿不停!總結(jié)
以上是生活随笔為你收集整理的C#.NET编程----Spring.NET NHibernate整合(一)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: sharepoint 添加解决方案 遇到
- 下一篇: 吐血整理!12种通用知识图谱项目简介