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 ...
随机推荐
- 【Java编程思想笔记】-集合1
1.为什么要用集合? 一般情况下,数组是保存一组对象(或基本数据类型)最有效的方式.但是数组有着固定的尺寸,而在更一般的情况下,我们在写程序时不知道将需要保存多少个对象,或者是否需要更复杂的存储结构来 ...
- pythonのpygame安装
本地环境: python 3.7.0 windows7 64bit pip pygame地址: https://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame 通 ...
- 论文笔记:Deep Residual Learning
之前提到,深度神经网络在训练中容易遇到梯度消失/爆炸的问题,这个问题产生的根源详见之前的读书笔记.在 Batch Normalization 中,我们将输入数据由激活函数的收敛区调整到梯度较大的区域, ...
- 关于Mac或Linux下GO的Permission denied提示错误
有时候当你下载第三方库的时候,编译时会提示Permission denied 权限不足, 出现这种错误因为权限不够.其中一种办法是需要把你项目目录和go的pck.bin权限放开. chmod -R 7 ...
- python进程.线程和协程的总结
I.进程: II.多线程threading总结 threading用于提供线程相关的操作,线程是应用系统中工作的最小单位(cpu调用的最小单位). Python当前版本的多线程没有实现优先级,线程组, ...
- 用ethtool 命令解决Linux 网卡丢包【转】
转自:https://blog.csdn.net/chengxuyuanyonghu/article/details/73739516 生产中有一台Linux设备并发比较大,droped包比较多,尤其 ...
- GNU Wget 1.19.1 static built on mingw32
http://pan.baidu.com/s/1sluFAVj #wget --version GNU Wget 1.19.1 built on mingw32. -cares +digest -gp ...
- 洛谷P5162 WD与积木 [DP,NTT]
传送门 思路 真是非常套路的一道题-- 考虑\(DP\):设\(f_n\)为\(n\)个积木能搭出的方案数,\(g_n\)为所有方案的高度之和. 容易得到转移方程: \[ \begin{align*} ...
- 为什么在移动端用rem圆角不圆
rem是根据网页效果图的尺寸来计算的,当然还要借助媒体查询来完成.例如你的设计稿如果是宽720px的话,那你的文字就要以原始大小除以11.25,就是对应下面媒体查询720px:例如16px的话就要16 ...
- Flex布局新旧混合写法详解
flex是个非常好用的属性,如果说有什么可以完全代替 float 和 position ,那么肯定是非它莫属了(虽然现在还有很多不支持 flex 的浏览器).然而国内很多浏览器对 Flex 的支持都不 ...