惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog
V
Vulnerabilities – Threatpost
P
Proofpoint News Feed
Google DeepMind News
Google DeepMind News
Y
Y Combinator Blog
V
Visual Studio Blog
阮一峰的网络日志
阮一峰的网络日志
腾讯CDC
月光博客
月光博客
T
Troy Hunt's Blog
博客园_首页
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
Netflix TechBlog - Medium
Microsoft Security Blog
Microsoft Security Blog
Recorded Future
Recorded Future
Blog — PlanetScale
Blog — PlanetScale
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Scott Helme
Scott Helme
T
Threat Research - Cisco Blogs
P
Palo Alto Networks Blog
T
The Exploit Database - CXSecurity.com
Simon Willison's Weblog
Simon Willison's Weblog
Know Your Adversary
Know Your Adversary
SecWiki News
SecWiki News
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Threatpost
Forbes - Security
Forbes - Security
S
Schneier on Security
P
Proofpoint News Feed
T
Tor Project blog
Cyberwarzone
Cyberwarzone
The Hacker News
The Hacker News
Cloudbric
Cloudbric
S
Security @ Cisco Blogs
Webroot Blog
Webroot Blog
Attack and Defense Labs
Attack and Defense Labs
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
Hacker News - Newest:
Hacker News - Newest: "LLM"
C
CERT Recently Published Vulnerability Notes
The Last Watchdog
The Last Watchdog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
S
SegmentFault 最新的问题
V
V2EX
量子位
B
Blog RSS Feed
宝玉的分享
宝玉的分享
T
The Blog of Author Tim Ferriss
罗磊的独立博客
J
Java Code Geeks

博客园 - 涤生

Enterprise Library Caching Application Block(CAB)源代码研究之缓存条目清理后台线程篇 了解ASP.NET底层架构(网络上的翻译) EditPlus中的正则表达式使用--如何使用查找到的原字符 CodeSmith4.0.2连接SqlServer2005――不允许远程连接 many2many的写法与注意点 Nhibernate继承类的写法 One2One主键关联的实现 NHibernate基础学习时遇到的问题 NHibernate学习之一:Many2One遇到的问题 自动提取网页的信息 ,并分析之 权限学习--BlueDavy之技术Blog漫谈权限系统之结尾篇(开源产品、个人观点、知识体系) 权限学习--BlueDavy之技术Blog漫谈权限系统之基于ACL的实现 权限设计学习篇--BlueDavy之技术Blog - 漫谈权限系统之技术策略以及基于RBAC ... 权限设计-之学习篇--来源于BlueDavy之技术Blog Oracle数据库发生Ora-12162错误 Linux下Oracle文件的自动备份与ftp自动上传 BugFree安装手记 CodeSmith3.0x中Oracle的访问 Asp.Net StartKits之Asp.Net Issue Tracker Starter Kit-数据库
Spring.Net学习笔记一——入门、第一个例子
涤生 · 2007-07-23 · via 博客园 - 涤生

从今天开始学习Spring.Net,此系列文章均按照一个新手(如我)在实践过程中遇到的困惑以及困惑的解决过程来写的。作为自己学习Spring.Net的一个学习笔记。
本系列文章假设你已经初步了解Spring.Net的基本理论知识,如IOC,Aop等。重点在掌握相应的理论知识后,如何进行实践学习的过程。
第一章,入门;如何建立自己的第一功Spring.Net的实例
建立的是一共
console的“Hello World”实例。具体需要注意的过程:

1)添加Spring.core的引用

2)写Hello.cs,就是一个简单的属性,HelloWorld.

Hello.cs
namespace SpringStudy_1
{
    
class Hello
    
{
        
private string helloword;

        
public string HelloWord
        
{
            
get return this.helloword; }
            set
 this.helloword = value; }
        }

    }

}

3)添加xml配置文件,配置文件内容如下:

<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="SpringStudy_1.Hello">
    <
property name="HelloWord" value="Hello!Welcome to Spring.Net Word!"/>
  </
object>
  
</
objects>

(4)写调用程序。调用程序要引用

using Spring.Core.IO;

using Spring.Objects.Factory;

using Spring.Objects.Factory.Xml;

调用方法如下

static void Main(string[] args)
        
{
            
//IResource rs = new FileSystemResource("objects-config.xml");

            IResource rs = 
new FileSystemResource("file://objects-config.xml");
            IObjectFactory factory 
= new XmlObjectFactory(rs);

            Hello hello 
= (Hello)factory.GetObject("hello");

            System.Console.Out.WriteLine(hello.HelloWord);
            System.Console.In.Read();
        }

注意点:如果采用的是下面的调用语句

IResource rs = new FileSystemResource("objects-config.xml");

系统会到Bin/debug下面去寻找该文件。所以,需要设置该xml文件的属性为:

复制到输出目录:如果较新则复制

也可以用下面方法来调用,使用IApplicationContextIApplicationContext扩展实现了一些IObjectFactory未实现的高级功能

IApplicationContext context = new XmlApplicationContext("objects-config.xml");

 

            
// of course, an IApplicationContext is also an IObjectFactory

            IObjectFactory factory 
= (IObjectFactory)context;

 

            Hello hello 
= (Hello)factory.GetObject("hello");

另外一种就是消除对”objects-config.xml”的硬编码。增加一个配置文件App.config,如下:

<?xml version="1.0" encoding="utf-8" ?>

<
configuration>

  <
configSections>

    <
sectionGroup name="spring">

      <
section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>

      <
section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />

    </
sectionGroup>

  </
configSections>

  <
spring>

    <
context>

      <
resource uri="file://objects-config.xml"/>

    </
context>

    <
objects xmlns="http://www.springframework.net">

      <
description>An  example that demonstrates simple IoC features.</description>

    </
objects>

  </
spring>

</
configuration>

在配置文件中指明使用的resource资源路径。然后在程序中即可按如下方法调用

IApplicationContext ctx = ContextRegistry.GetContext();

            Hello hello = (Hello) ctx.GetObject("hello");

备注:在vs2005中,当未使用的类的相关引用,可以通过ctrl + shift + F10,来添加相关资源的引用。(与Eclipse的手法相似)

至此,第一个Spring.Net实例已经完成。在Main中对Hello类进行了注入。