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

推荐订阅源

Engineering at Meta
Engineering at Meta
博客园_首页
J
Java Code Geeks
Jina AI
Jina AI
B
Blog RSS Feed
量子位
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
L
LangChain Blog
Microsoft Security Blog
Microsoft Security Blog
小众软件
小众软件
博客园 - 聂微东
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 三生石上(FineUI控件)
Last Week in AI
Last Week in AI
MongoDB | Blog
MongoDB | Blog
I
InfoQ
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
爱范儿
爱范儿
Y
Y Combinator Blog
Vercel News
Vercel News
雷峰网
雷峰网

博客园 - 雨人(ralpher)

volatile VS synchronized 记录一个小问题:游标中声明排序的写法问题 Great feature for Sql 2005--Row_number()分页 Recommend a site. 推荐一文:程序员的灯下黑:坚持和良好心态近乎道 EnglishTalk----Too Much Test Automation? 我这几年-----对于初学者的几点建议 面向对象设计原则(转) 系分考试论文实例12篇 一道禅语 好久没来了 First trip to BeiJing 飘到北京了 IOC模式的学习笔记 IBatisNet之运行时多数据库切换方案 读写xml配置文件 在IBatisNet中使用存储过程 装箱 与 虚方法分发 IBatisNet初步体验
体验Castle中从配置文件注册组件以及传递参数的一个小例子
雨人(ralpher) · 2006-01-13 · via 博客园 - 雨人(ralpher)

最近看了一些IOC模式的介绍以及关于Castle的文章,从网上下下来关于Introducing Castle PartI的例子,不过在运行的时候出现错误,老是提示"Could not resolve non-optional dependency for smtpemailsender. Parameter 'host' type String"。最后决定自己做个简单的例子来体验一下,要做就作最简单的,只有一个组件。所需要的Castle的库是Castle.Windor.dll,Castle.DynamicProxy.dll,Caslte.Model.dll,Castle.MicroKernel.dll.

using System;

namespace IOC.Test.Component
{
    
/// <summary>
    
/// MyComponent1 's abstract。
    
/// </summary>

    public class MyComponent1
    
{
        
private String _name = null ;

        
/*
        // If I make the constructor with none parameters be valid. It will be ok. 
        public MyComponent1()
        {
            _name = "default";
        }
        
*/

        
public MyComponent1(String name)
        
{
            _name 
= name;
        }


        
public string Introducing()
        
{
            
return "My name is "+_name;
        }

    }

}

这里是调用的代码.

static void Main() 
{
    
//开始采用改构造函数创建实例不行,最后去网上需求hamment的帮助
    
//WindsorContainer container = new WindsorContainer( @"..\..\app.config" );
    
    WindsorContainer container 
= new WindsorContainer( new XmlInterpreter(  ) );
    
    
//这里如果组件已经在配置文件中说明的话,那么就不需要在这里重复注册了
    
//container.AddComponent("myCompo1",typeof(MyComponent1));
    MyComponent1 myCompo1 = (MyComponent1)container["myCompo1"];
    MessageBox.Show(
"hello,"+myCompo1.Introducing());
}


 

注意这里好象自己声明配置文件老是不生效,(我也正纳闷原因是啥呢,谁知道了告诉我)只有默认的配置文件才生效,对于Web的话,默认的是web.config,对于windows程序,默认是可执行程序名称.config.譬如我的可执行程序名字是IOC.Test.exe,那么默认的配置文件就是IOC.Test.exe.config,该文件和可执行程序放在同一个文件夹中。

配置文件的内容如下

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

<configuration>

    
<configSections>
        
<section name="castle"
          type
="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, 
                Castle.Windsor" />
    </configSections>

    
<castle>
        
<components>
            
<component id="myCompo1" Service="IOC.Test.Component.MyComponent1,IOC.Test" 

type
="IOC.Test.Component.MyComponent1,IOC.Test">
                
<parameters>
                    
<name>rhf</name>
                
</parameters>
            
</component>
        
</components>
    
</castle>

</configuration>