一步一步学Spring.Net——2、第一个程序“Hello,World”

1、新建一个类Framework.cs

public class Framework
{
public Framework()
{
//
//TODO: 在此处添加构造函数逻辑	
//
}
    public string Name { set; get; }

}

2、在Index.aspx页面添加一个label

   <div>
        <h1><asp:Label runat="server" ID="lblFramework"></asp:Label></h1>
    </div>

3、Index.aspx.cs

public partial class Index : System.Web.UI.Page
{
    // 定义一个注入点
    public Framework FrameworkName { set; get; } 

    protected void Page_Load(object sender, EventArgs e)
    {
        this.lblFramework.Text = this.FrameworkName.Name;
    }
}

定义对象主要有两种方式,直接定义在 web.config 中,或者定义在外部的配置文件中。

4直接定义在 web.config 中,使用 Spring.Context.Support.DefaultSectionHandler。这样可以在配置文件中直接定义。

 <configSections>
    <!-- Spring 的配置 -->
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web"/>
     <!-- 支持在 web.config 中定义对象 -->
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
    </sectionGroup>
  </configSections>
 <spring>
    <context>
      <resource uri="config://spring/objects"/>
    </context>
    <!-- 直接定义在 web.config 中的对象 -->
    <objects>
      <object id="framework" type="Framework"><!--类名-->
        <property name="Name" value="Hello,world"/><!--属性名称,值-->
      </object>
      <!-- 页面对象 -->
      <object type="~/Index.aspx">
        <!-- ref 表示引用的对象 -->
        <property name="FrameworkName" ref="framework"/><!--Index.aspx页面的属性名称-->
      </object>
    </objects>
  </spring>

5、浏览Index.aspx

 

6、在单独的配置文件中配置对象。

在网站中创建一个名为 Config 的文件夹,以保存独立的配置文件。

在 Config 文件夹中,创建一个名为 objects.xml 的 Xml 配置文件。添加名为 objects 的根元素,添加默认命名空间 xmlns="http://www.springframework.net"

找到如下架构文件,复制到vs安装目录:C:\Program Files (x86)\Microsoft Visual Studio 10.0\Xml\Schemas

这样,我们在xml文件中就具备智能感知功能了。

 

添加原来对象定义到这里。 

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"><!--默认命名空间-->
  <object id="framework" type="Framework">
    <!--类名-->
    <property name="Name" value="Hello,China"/>
    <!--属性名称,值-->
  </object>
  <!-- 页面对象 -->
  <object type="~/Index.aspx">
    <!-- ref 表示引用的对象 -->
    <property name="FrameworkName" ref="framework"/>
    <!--Index.aspx页面的属性名称-->
  </object>
</objects>

将原来在 Web.config 中配置的 objects 配置节删除,将原来 context 配置节中的配置替换为如下的内容。

    <context>
      <resource uri="~/Config/objects.xml"/>
      <!--<resource uri="config://spring/objects"/>-->
    </context>

6、重新浏览Index.aspx

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

邹琼俊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值