说明:接续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. 【NLP CS224N笔记】Lecture 2 - Word Vector Representations: word2vec

    I. Word meaning Meaning的定义有很多种,其中有: the idea that is represented by a word,phrase,etc. the idea that ...

  2. 20165325《Java程序设计》第九周学习总结

    一.教材学习笔记 ch13 1.URL类 URL类是java.net包中的一个重要的类,URL的实例封装着一个统一资源定位符,使用URL创建对象的应用程序称作客户端程序. 一个URL对象通常包含最基本 ...

  3. SpringCloud Feign

    ⒈Feign是什么? Feign是Netflix开发的声明式.模板化的HTTP客户端, Feign可以帮助我们更快捷.优雅地调用HTTP API. SpringCloud微服务项目之间调用是通过Res ...

  4. Graham 扫描法找凸包(convexHull)

    凸包定义 通俗的话来解释凸包:给定二维平面上的点集,凸包就是将最外层的点连接起来构成的凸多边型,它能包含点集中所有的点  Graham扫描法 由最底的一点 \(p_1\) 开始(如果有多个这样的点, ...

  5. 看完此文还不懂NB-IoT,你就过来掐死我吧...

    看完此文还不懂NB-IoT,你就过来掐死我吧....... 1 1G-2G-3G-4G-5G 不解释,看图,看看NB-IoT在哪里? 2 NB-IoT标准化历程 3GPP NB-IoT的标准化始于20 ...

  6. 设计模式C++学习笔记之十七(Chain of Responsibility责任链模式)

      17.1.解释 概念:使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系.将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止. main(),客户 IWom ...

  7. centos6.8上PHP5.3升级到PHP5.4及更高版本方法

    执行命令下载.安装yum源,当前是针对Centos6并且64位版本的源: [root@T_249 yum.repos.d]# rpm -ivh http://dl.fedoraproject.org/ ...

  8. centos6.8安装httpd后无法访问

    1.打开 httpd.conf 将里面的 #ServerName localhost:80 注释去掉 2.修改SELinux状态: 1)/usr/sbin/sestatus -v      ##如果S ...

  9. LOJ 6277-6280 数列分块入门 1-4

    数列分块是莫队分块的前置技能,练习一下 1.loj6277 给出一个长为n的数列,以及n个操作,操作涉及区间加法,单点查值. 直接分块+tag即可 #include <bits/stdc++.h ...

  10. Linux下的进程结构

    Linux系统是一个多进程的系统,它的进程之间具有并行性.互不干扰等特点.也就是说,每个进程都是一个独立的运行单位,拥有各自的权利和责任.其中,各个进程都运行在独立的虚拟地址空间.因此,即使一个进程发 ...