码迷,mamicode.com
首页 > 编程语言 > 详细

Spring .NET学习笔记001:环境搭配及Hello World

时间:2014-08-26 21:17:36      阅读:296      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   使用   io   strong   文件   ar   问题   

【版权声明:本文为特维博客原创,未经准许谢绝转载。如需转载,请务必在转载时注明本博客地址。】

1.前言

很久之前都接触了Spring .NET,只是一直没有在项目中使用,今天就开始尝试在新项目里面使用。

2.步骤

1、新建控制台项目

bubuko.com,布布扣

2、安装依赖

在PM Condole输入:Install-Package Spring.Core –Pre

会自动安装Spring.Core和Common.Logging

bubuko.com,布布扣

3、新建App.config,代码如下:

 
  1. <?xml version="1.0" encoding="utf-8" ?> 
  2. <configuration
  3.     <configSections
  4.         <sectionGroup name="spring"
  5.             <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>              
  6.         </sectionGroup
  7.     </configSections
  8.     <spring
  9.         <context
  10.             <resource uri="file://spring.xml.config"/> 
  11.         </context
  12.     </spring
  13. </configuration

4、新建spring.xml.config(这个名字可自定,如需改名,则在App.config的rescource里也需一并修改),代码如下:

 
  1. <?xml version="1.0" encoding="utf-8" ?> 
  2. <objects xmlns="http://www.springframework.net"
  3.     <object id="hello" type="SpringNetDemo.Hello"
  4.         <property name="HelloWord" value="Hello!你能看到这个证明你成功了!"/> 
  5.     </object
  6. </objects

5、修改项目原来的Program.cs文件:

 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using Spring.Context;  
  5. using Spring.Context.Support;  
  6.  
  7. namespace SpringNetDemo  
  8. {  
  9.     public class Hello  
  10.     {  
  11.         private string helloword;  
  12.  
  13.         public string HelloWord  
  14.         {  
  15.             get { return this.helloword; }  
  16.             set { this.helloword = value; }  
  17.         }  
  18.     }  
  19.     public class Program  
  20.     {  
  21.         static void Main(string[] args)  
  22.         {  
  23.             IApplicationContext context = ContextRegistry.GetContext();  
  24.             Hello hello = (Hello)context.GetObject("hello");  
  25.             Console.Write(hello.HelloWord);  
  26.             Console.Read();  
  27.         }  
  28.     }  

说明:这里的hello.HelloWord就是Spring.Net通过xml的配置实现注入的。有问题的可以留言。

3.注意

在这里可能会出错,提示Common.Logging版本过低,解决办法是重新安装Common.Logging:

Install-Package Common.Logging

bubuko.com,布布扣

4.Web项目注意事项

需要输入:

5.Install-Package Spring.Web.Mvc4 –Pre

bubuko.com,布布扣

如有大家有什么问题,可以给我留言,我会抽时间一一解答。

笔者微博:@LeaveBugsAway欢迎叨扰。

Spring .NET学习笔记001:环境搭配及Hello World

标签:style   http   color   使用   io   strong   文件   ar   问题   

原文地址:http://www.cnblogs.com/tangyouwei/p/3938150.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!