说明:接续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=""/>
</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=""/>
</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=""/>
</object>-->
</objects>
</spring>
</configuration>

App.config

运行 OK

Spring.Net 简单实例-02(属性注入)的更多相关文章

  1. spring学习(三)属性注入

    用的是IDEA的maven工程,pom.xml文件导包依赖省略 本文主要写set方式注入 (一).一般类型注入 一.写两个实体类Car.User public class Car { private ...

  2. Spring:特殊数据类型的属性注入(基于配置文件)

    该处提到的特殊数据类型指的是除了基础数据类型和String以外的其他常用的数据类型,如:List.Map.Set.以及pojo对象等.则我们创建的Person类定义为: package bjtu.we ...

  3. spring学习笔记之---bean属性注入

    bean属性注入 (一)构造方法的属性注入 1.Student.java package entity; public class Student { private String name; pri ...

  4. Spring boot 工具类静态属性注入及多环境配置

    由于需要访问MongoDB,但是本地开发环境不能直接连接MongoDB,需要通过SecureCRT使用127.0.0.2本地IP代理.但是程序部署到线上生产环境后,是可以直接访问MongoDB的,因此 ...

  5. Spring.Net 简单实例-01(IOC)

    1.话不多说看操作.新建"Windows窗体应用程序" 2:通过配置文件创建IOC容器 首先引入安装包 3:定义一个接口(更好的体现封装性,当然也可以直接使用类) 定义一个类,实现 ...

  6. 16~25.spring+hibernate简单实例 .连接数据库并进行增删改查

    1.概念 Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQ ...

  7. Maven+Eclipse+Spring MVC简单实例

    1. ToolsVersion and Preparations: Eclipse: 3.5 (eclipse-jee-galileo-win32) Maven: 2.0.11 Spring MVC ...

  8. Spring.Net学习笔记(4)-属性及构造器注入

    一.开发环境 操作系统:Win10 编译器:VS2013 .Net版本:.net framework4.5 二.涉及程序集 Spring.Core.dll:1.3.1 Common.Logging.d ...

  9. 【初识Spring】对象(Bean)实例化及属性注入(xml方式)

    title: [初识Spring]对象(Bean)实例化及属性注入(xml方式) date: 2018-08-29 17:35:15 tags: [Java,Web,Spring] --- #初识S ...

随机推荐

  1. jquery判断表单内容是否为空

    //判断表单数据是否为空 var t = $('form').serializeArray(); $.each(t,function(i,item){ if(item['value'] == '') ...

  2. 20165234 《Java程序设计》第八周学习总结

    第八周学习总结 教材内容学习 第十二章 Java 多线程机制 进程与线程 进程是程序的一次动态执行过程,对应了从代码加载.执行至执行完毕的一个完整过程. 线程不是进程,是比进程更小的执行单位. 一个进 ...

  3. 【Udacity并行计算课程笔记】- Lesson 4 Fundamental GPU Algorithms (Applications of Sort and Scan)

    I. Scan应用--Compact 在介绍这节之前,首先给定一个情景方便理解,就是因为某种原因我们需要从扑克牌中选出方块的牌. 更formal一点的说法如下,输入是 \(s_0,s_1,...\), ...

  4. Zookeeper环境搭建

    zookeeper支持windows.linux.mac等操作系统,其搭建方式也有集群.伪集群.单机环境.下面研究三种方式的搭建. 单机环境:windows操作系统 伪集群:windows 集群:li ...

  5. java的引用

    一.值类型与引用类型 1.变量初始化 int num = 10; String str = "hello"; num是int基本类型变量,值就直接保存在变量中.str是String ...

  6. Latex 编辑数学公式——快速上手

    参考链接: https://blog.csdn.net/fansongy/article/details/45368915 特殊符号: https://blog.csdn.net/caiandyong ...

  7. pycharm安装numpy和scipy(window)

    win 10,64位,python3.6 用的pycharm,依次点file-settings-project-project interpreter,出现下图,(一般按正常安装流程,Package里 ...

  8. Tour HDU - 3488 有向环最小权值覆盖 费用流

    http://acm.hdu.edu.cn/showproblem.php?pid=3488 给一个无源汇的,带有边权的有向图 让你找出一个最小的哈密顿回路 可以用KM算法写,但是费用流也行 思路 1 ...

  9. /var/run/yum.pid 已被锁定,PID 为 2925 的另一个程序正在运行

    解决办法:直接在终端运行 rm -f /var/run/yum.pid 将该文件删除,然后再次运行yum.

  10. 软件包.deb的安装及卸载------dpkg

    文章链接:https://blog.csdn.net/qq_36764147/article/details/81332606 删除带有rc的软件包:https://blog.csdn.net/chr ...