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=""/>
</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(属性注入)的更多相关文章
- spring学习(三)属性注入
用的是IDEA的maven工程,pom.xml文件导包依赖省略 本文主要写set方式注入 (一).一般类型注入 一.写两个实体类Car.User public class Car { private ...
- Spring:特殊数据类型的属性注入(基于配置文件)
该处提到的特殊数据类型指的是除了基础数据类型和String以外的其他常用的数据类型,如:List.Map.Set.以及pojo对象等.则我们创建的Person类定义为: package bjtu.we ...
- spring学习笔记之---bean属性注入
bean属性注入 (一)构造方法的属性注入 1.Student.java package entity; public class Student { private String name; pri ...
- Spring boot 工具类静态属性注入及多环境配置
由于需要访问MongoDB,但是本地开发环境不能直接连接MongoDB,需要通过SecureCRT使用127.0.0.2本地IP代理.但是程序部署到线上生产环境后,是可以直接访问MongoDB的,因此 ...
- Spring.Net 简单实例-01(IOC)
1.话不多说看操作.新建"Windows窗体应用程序" 2:通过配置文件创建IOC容器 首先引入安装包 3:定义一个接口(更好的体现封装性,当然也可以直接使用类) 定义一个类,实现 ...
- 16~25.spring+hibernate简单实例 .连接数据库并进行增删改查
1.概念 Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQ ...
- Maven+Eclipse+Spring MVC简单实例
1. ToolsVersion and Preparations: Eclipse: 3.5 (eclipse-jee-galileo-win32) Maven: 2.0.11 Spring MVC ...
- Spring.Net学习笔记(4)-属性及构造器注入
一.开发环境 操作系统:Win10 编译器:VS2013 .Net版本:.net framework4.5 二.涉及程序集 Spring.Core.dll:1.3.1 Common.Logging.d ...
- 【初识Spring】对象(Bean)实例化及属性注入(xml方式)
title: [初识Spring]对象(Bean)实例化及属性注入(xml方式) date: 2018-08-29 17:35:15 tags: [Java,Web,Spring] --- #初识S ...
随机推荐
- Redis protected-mode属性解读
redis3.2版本后新增protected-mode配置,默认是yes,即开启.设置外部网络连接redis服务,设置方式如下: 1.关闭protected-mode模式,此时外部网络可以直接访问 2 ...
- ODS、DW和DM
参考资料: 数据仓库ODS.DW和DM概念区分:https://www.jianshu.com/p/72e395d8cb33
- C++ 仿函数
先考虑一个简单的例子:假设有一个vector<string>,你的任务是统计长度小于5的string的个数,如果使用count_if函数的话,你的代码可能长成这样: 1 bool Leng ...
- bootstrap模态框显示时被遮罩层遮住了
<style>.modal-backdrop{z-index:0;}</style>
- RabbitMQ四种交换机类型介绍
RabbitMQ 原文地址: https://baijiahao.baidu.com/s?id=1577456875919174629&wfr=spider&for=pc 最新版本的 ...
- Contains Duplicate I & III
Contains Duplicate I Given an array of integers, find if the array contains any duplicates. Your fun ...
- C实战:项目构建Make,Automake,CMake【转】
转自:https://blog.csdn.net/dc_726/article/details/48978849 版权声明:本文为博主原创文章,未经博主允许不得转载.欢迎访问 http://blog. ...
- FireDac 的一些应用提示
FireDac 的一些应用提示: 1. 客户端 dataset 能用 TFDQuery 连接 dataset provider 获取数据 ? Can I use TFDQuery and conne ...
- 虚拟化之kvm --(vnc控制台)
作者:邓聪聪 随着日益不同的需求增多,为了满足主机供求,get到这一招虚拟化技术,以增加点见识! 1.使用yum安装: yum -y install qemu-kvm libvirt python-v ...
- VC显示网页验证码、模拟CSDN网页登录
摘要:by:koma 这两天,本来想花点时间研究一下QQ空间.农场外挂,于是抓包分析一了下,只可惜,在QQ网页登录时进行了加密处理,可惜我对网页编程一窍不通.有些朋友曾讲过那些是通过JS代码进行加密, ...