zoukankan      html  css  js  c++  java
  • Spring.Net 简单实例-02(属性注入)

    说明:接续Spring.Net 简单实例-01(IOC)

    话不多说看操作

    1:为UserInfo添加属性

    2: 修改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="config://spring/objects"/>
        </context>
        <objects xmlns="http://www.springframework.net">
          <description>An  example that demonstrates simple IoC features.</description>
          <!--//type属性值必须是包含程序集名称在内的类型全名    "命名空间,程序集"-->
          <object name="UserInfoService"  type="YK.OA.SpringNet.UserInfoService, YK.OA.SpringNet">
            <!-- using setter injection... -->
            <!--//name代表属性名:UserName-->
            <property name="UserName" value="逍遥小天狼"/>
            </object>
        </objects>
      </spring>
    </configuration>
    App.config

    3:进一步了解

    添加一个Person类  

    4:同时修改UserInfoService类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace YK.OA.SpringNet
    {
       public class UserInfoService:IUserInfoService
        {
           //1添加属性
           public string UserName { get; set; }
           //2再次添加一个属性
           public Person Person { set; get; }
            public string ShowMsg()
            {
                //此时没有给属性赋值,通过ioc容器实例化时赋值
                return "Hello World:" + UserName+",年龄: "+Person.Age;
            }
        }
    }
    UserInfoService

    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 name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
        </sectionGroup>
      </configSections>
      <spring>
        <context>
          <resource uri="config://spring/objects"/>
        </context>
        <objects xmlns="http://www.springframework.net">
          <description>An  example that demonstrates simple IoC features.</description>
          <!--//type属性值必须是包含程序集名称在内的类型全名    "命名空间,程序集"-->
          <object name="UserInfoService"  type="YK.OA.SpringNet.UserInfoService, YK.OA.SpringNet">
            <!-- using setter injection... -->
            <!--//name代表属性名:UserName-->
            <property name="UserName" value="逍遥小天狼"/>
            <!--通过ref对Person属性进行关联-->
            <!--name="表示UserInfo属性"  ref表示关联的object名称-->
            <property name="Person"  ref="Person"/>
          </object>
          <!--Person也是类,所以先object-->
          <object name="Person"  type="YK.OA.SpringNet.Person, YK.OA.SpringNet">
            <property name="Age" value="24"/>
          </object>
        </objects>
      </spring>
    </configuration>
    App.config

     6运行结果

     7:文件分离:当类文件过多后App.config文件过于臃肿

     添加Xml文件实现代码分离,并将属性设置为"如果更新则复制"(很重要否则配置文件找不到xml文件)

     

    <?xml version="1.0" encoding="utf-8" ?>
    <objects xmlns="http://www.springframework.net">
      <description>An  example that demonstrates simple IoC features.</description>
      <!--//type属性值必须是包含程序集名称在内的类型全名    "命名空间,程序集"-->
      <object name="UserInfoService"  type="YK.OA.SpringNet.UserInfoService, YK.OA.SpringNet">
        <!-- using setter injection... -->
        <!--//name代表属性名:UserName-->
        <property name="UserName" value="逍遥小天狼"/>
        <!--通过ref对Person属性进行关联-->
        <!--name="表示UserInfo属性"  ref表示关联的object名称-->
        <property name="Person"  ref="Person"/>
      </object>
      <!--Person也是类,所以先object-->
      <object name="Person"  type="YK.OA.SpringNet.Person, YK.OA.SpringNet">
        <property name="Age" value="24"/>
      </object>
    </objects>
    services.xml

    app.config可简化

    一定 要在<context>到加入<resource uri="file://services.xml"/>

    <?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="config://spring/objects"/>
          <!--//添加引用-->
          <resource uri="file://services.xml"/>
        </context>
        <objects xmlns="http://www.springframework.net">
          <!--<description>An  example that demonstrates simple IoC features.</description>
          --><!--//type属性值必须是包含程序集名称在内的类型全名    "命名空间,程序集"--><!--
          <object name="UserInfoService"  type="YK.OA.SpringNet.UserInfoService, YK.OA.SpringNet">
            --><!-- using setter injection... --><!--
            --><!--//name代表属性名:UserName--><!--
            <property name="UserName" value="逍遥小天狼"/>
            --><!--通过ref对Person属性进行关联--><!--
            --><!--name="表示UserInfo属性"  ref表示关联的object名称--><!--
            <property name="Person"  ref="Person"/>
          </object>
          --><!--Person也是类,所以先object--><!--
          <object name="Person"  type="YK.OA.SpringNet.Person, YK.OA.SpringNet">
            <property name="Age" value="24"/>
          </object>-->
        </objects>
      </spring>
    </configuration>
    App.config

    运行 OK

  • 相关阅读:
    九九乘法表
    计算器界面
    3.2封装的日期类
    杨辉三角
    100以内的素数
    九九 乘法表
    七、logging模块
    六、MySQLdb 模块
    四、浏览器运行模式
    五、configparser模块
  • 原文地址:https://www.cnblogs.com/YK2012/p/6597230.html
Copyright © 2011-2022 走看看