zoukankan      html  css  js  c++  java
  • Spring.Net学习笔记(4)-属性及构造器注入

    一、开发环境

    操作系统:Win10

    编译器:VS2013

    .Net版本:.net framework4.5

    二、涉及程序集

    Spring.Core.dll:1.3.1

    Common.Logging.dll

    三、开发过程

    1.项目结构

    image

    2.编写Product.cs

    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");
            }
        }
    }
  • 相关阅读:
    js,JavaScript,a标签onclick传递参数不对,A标签调用js函数写法总结
    Java两大测试方法Junit和TestNG的比较
    java简单的测试方法执行了多少时间
    利用Chrome的Performance工具排查页面性能问题(原叫timeline)
    P3317 [SDOI2014]重建(Matrix-tree+期望)
    P2221 [HAOI2012]高速公路(线段树)
    P2473 [SCOI2008]奖励关(期望)
    P3302 [SDOI2013]森林(主席树+启发式合并)
    bzoj3932 / P3168 [CQOI2015]任务查询系统(主席树+差分)
    P2219 [HAOI2007]修筑绿化带(单调队列)
  • 原文地址:https://www.cnblogs.com/kimisme/p/5339173.html
Copyright © 2011-2022 走看看