程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> Spring.Net學習筆記(4)-屬性及構造器注入,spring.net學習筆記

Spring.Net學習筆記(4)-屬性及構造器注入,spring.net學習筆記

編輯:C#入門知識

Spring.Net學習筆記(4)-屬性及構造器注入,spring.net學習筆記


一、開發環境

操作系統:Win10

編譯器:VS2013

.Net版本:.net framework4.5

二、涉及程序集

Spring.Core.dll:1.3.1

Common.Logging.dll

三、開發過程

1.項目結構

namespace SpringNetDi { public class Product { public string Name { get; set; } public decimal UnitPrice { get; set; } } }

3.編寫ProductFactory.cs

namespace SpringNetDi
{
    public class ProductFactory
    {
        public Product FactoryProduct { get; set; }
    }
}

4.編寫Article.cs

namespace SpringNetDi
{
    public class Article
    {
        private string name;
        private string writer;

        public Article(string name, string writer)
        {
            this.name = name;
            this.writer = writer;
        }

        public void GetMsg()
        {
            Console.WriteLine("你好,我是" + writer);
        }
    }
}

5.配置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>
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler,Spring.Core"/>
    </sectionGroup>
  </configSections>

  <spring>
    <context>
      <resource uri="config://spring/objects"></resource>
    </context>
    <objects>
      <!--01屬性注入-值類型-->
      <object name =" product" type="SpringNetDi.Product,SpringNetDi">
        <property name="Name" value="記號筆"></property>
        <property name="UnitPrice" value="5"></property>
      </object>
      <!--02屬性注入-引用類型-->
      <object name="factory" type="SpringNetDi.ProductFactory,SpringNetDi">
        <property name="FactoryProduct" ref="product"></property>
      </object>
      <!--03構造函數注入-->
      <object name="article" type="SpringNetDi.Article,SpringNetDi">
        <constructor-arg name="name" value="依賴注入"></constructor-arg>
        <constructor-arg name="writer" value="Kimisme"></constructor-arg>
      </object>
    </objects>
  </spring>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>

6.控制台代碼

namespace SpringNetDi
{
    class Program
    {
        static void Main(string[] args)
        {
            IApplicationContext context = ContextRegistry.GetContext();
            Product product = context.GetObject("product") as Product;
            Console.WriteLine(product.Name);

            ProductFactory factory = context.GetObject("factory") as ProductFactory;
            Console.WriteLine(factory.FactoryProduct.Name);

            Article article = context.GetObject("article") as Article;
            article.GetMsg();
            Console.WriteLine("ok");
        }
    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved