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 ...
随机推荐
- Nginx系列6:对称加密与非对称加密各自的应用场景
强推:推荐一篇通俗易懂的对称加密和非对称加密的文章:https://segmentfault.com/a/1190000004461428 推荐一篇文章:对称加密算法与非对称加密算法的优缺点:http ...
- nginx基础命令
1.nginx 启动nginx service nginx start 查看nginx状态 service nginx status 关闭nginx service nginx stop 热重启(前提 ...
- 在Python中用Selenium执行JavaScript
Selenium自己不带浏览器, 需要与第三方浏览器结合在一起使用.例如在Firefox上运行Selenium. PhantomJS是一个"无头"浏览器. 它会把网站加载到内存并执 ...
- 非极大值抑制(NMS)的几种实现
因为之前对比了RoI pooling的几种实现,发现python.pytorch的自带工具函数速度确实很慢,所以这里再对Faster-RCNN中另一个速度瓶颈NMS做一个简单对比试验. 这里做了四组对 ...
- LwIP Application Developers Manual6---Application API layers
1.前言 lwIP提供3种应用编程接口来跟TCP/IP内核通信,如下所示: 低水平的内核/回调或raw API 2个高水平序列API: 1) netconn API 2) socket API(为了兼 ...
- 如何操控DevExpress的 SpreadSheet 控件 并与 XAF 结合应用
DevExpress的XAF 框架通常使用 GridControl 控件来操作数据库表中的数据,但导入导出.非结构化数据的管理可以使用SpreadSheet 控件. SpreadSheet 控件模拟微 ...
- css利用padding百分比实现图片自适应高度
应用场景 宽高比率,实现图片自适应高度,防止图片加载过程高度为0,加载完图片高度撑起,它下面的div抖动问题 重点:CSS百分比padding都是相对宽度计算的 <div class=" ...
- installshield路径
InstallShield系统变量 BATCH_INSTALL 指示当传输文件采用LOCKEDFILE或SHAREDFILE时是否锁定文件 CMDLINE Setup.exe传递的命令行参数 COMM ...
- shutil.copy()、os.walk()、os.rename()实例
#!/usr/bin/python # -*- coding: UTF-8 -*- import os import shutil Path = "panel/" PNPath = ...
- URLConnection和HttpURLConnection
URLConnection和HttpURLConnection使用的都是java.net中的类,属于标准的java接口. HttpURLConnection继承自URLConnection,差别在与H ...