OA项目Ioc DI(二)




依赖注入:属性和构造函数的注入
一个简单的Demo:
IUserInfoDal接口:
public interface IUserInfoDal
{
void Show();
string Name { get; set; }
}
}
UserInfoDal继承IUserInfoDal:
public class UserInfoDal:IUserInfoDal
{ public void Show()
{
Console.WriteLine("hellow world");
}
}
建立SpringNetDemo的控制台应用程序:
static void Main(string[] args)
{
//IUserInfoDal userInfoDal = new UserInfoDal();
//userInfoDal.Show(); //容器来 创建UserInfoDal实列 //第一步 初始化容器
IApplicationContext ctx = ContextRegistry.GetContext(); IUserInfoDal dal = ctx.GetObject("UserInfoDal") as IUserInfoDal;
dal.Show(); Console.ReadKey();
}
在App中进行配置:(<configuration> 节点中
<!--sectionGroup 中的name要与 下面的标签名字一样 spring-->
<configSections>
<sectionGroup name="spring">
<!--spring 分成两个子块-->
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>
对sectionGroup的name进行配置:
<spring>
<!--Spring.Net对象容器的配置-->
<context>
<!--容器里面的所有的对象在哪里配置的?--> <!--使用xml文件的配置-->
<!--<resource uri="file://Dals.xml"/>-->
<!--使用程序集的配置-->
<!--<resource uri="assembly://SpringNetDemo/SpringNetDemo/Dals.xml"/>--> <resource uri="config://spring/objects"/> </context>
<!--objects:配置的容器的里面的对象的-->
<objects xmlns="http://www.springframework.net">
<description>An example that demonstrates simple IoC features.</description> <!--名字为类名-->
<!--<object name="UserInfoDal" type="SpringNetDemo.UserInfoDal, SpringNetDemo">-->
<object name="UserInfoDal" type="SpringNetDemo.EFUserInfoDal, SpringNetDemo">
<!--属性的配置-->
<property name="Name" value="Spring牛"/>
</object>
<!-- 把UserInfoDal属性注入到UserInfoServce-->
<!--<object name="UserInfoServce" type="SpringNetDemo.UserInfoServce, SpringNetDemo">
<property name="UserInfoDal" ref="UserInfoDal"/>
</object>--> </objects>
</spring>
XMl文件中的配置:
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.net
http://www.springframework.net/xsd/spring-objects.xsd"> <object name="UserInfoDal1" type="SpringNetDemo.UserInfoDal, SpringNetDemo">
<!--<constructor-arg index="0" value="movies.txt"/>-->
</object> </objects>
Spring.Net与项目结合:
1.在Web.Config中configuration 节点中进行配置
<configSections>
<!-- Entity Framework 块配置-->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> <!--Spring.Net块配置-->
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc4"/>
</sectionGroup>
</configSections> <!--Spring.Net 的容器的配置节点-->
<spring>
<context>
<resource uri="file://~/Config/services.xml"/>
<resource uri="file://~/Config/controllers.xml"/>
</context>
</spring>
2.在Global.asax中改成Spring.Web
public class MvcApplication :Spring.Web.Mvc.SpringMvcApplication
3.在packages中引入dll文件

4.在Portal中新建文件夹Config
1.controllers.xml
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"> <!--前面是类的全名称 后面是类的程序集 singleton="false" 每次都是创建一个心对象 不是共用一个对象 -->
<object type="SunOA.UI.Portal.Controllers.UserInfoController,SunOA.UI.Portal" singleton="false" >
<!--属性的注入-->
<property name="UserInfoService" ref="UserInfoService" />
</object>
</objects>
2.services.xml
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"> <object name="UserInfoService" type="SunOA.BLL.UserInfoService, SunOA.BLL" singleton="false" >
</object>
<object name="OrderInfoService" type="SunOA.BLL.OrderInfoService, SunOA.BLL" singleton="false" >
</object> </objects>
3.控制器。
public IUserInfoService UserInfoService { get; set; }
public ActionResult Index()
{
ViewData.Model = UserInfoService.GetEntites(u => true);
return View();
}
BLL层的代码(继承于接口):
public class OrderInfoService : BaseService<OrderInfo>, IOrderInfoService
public interface IUserInfoService : IBaseService<UserInfo>
IBLL层的代码:
public interface IOrderInfoService : IBaseService<OrderInfo>
Spring.Net程序的作用:
1.程序加载的时候,第一次请求过来的时候,初始化容器对象。
2.加载XML文件放到内存里面。
3.根据容器里面的XML配置创建对象
4.第二次和以后请求过的时候重复 第三个步骤
OA项目Ioc DI(二)的更多相关文章
- OA项目实战(二) 开发准备
上次的博文OA系统实践(一) 概述中,我们已经了解了OA的相关概念.从本次博文开始,我们做一个简单的OA实例. 在OA开发之前,有几个工作们需要提前做.一个是对需求进行分析,另一个是对开发环境进行选择 ...
- 第一周博客之二---OA项目环境搭建及开发包部署
OA项目环境搭建 一个项目想要能够在开发人员打包好项目包之后进行测试,就必须进行项目测试环境的搭建,要根据开发工程师的开发环境采用不同的测试环境,以下只是浅谈下Java项目OA(办公自动化平台)的环境 ...
- Spring系列(二):Spring IoC/DI的理解
这几天重新学习了一下Spring,在网上找了相关的ppt来看,当看到Spring IoC这一章节的时候,先大致浏览了一下内容,有将近50页的内容,内心窃喜~QAQ~,看完这些内容能够对IoC有更深层次 ...
- Spring 学习教程(二): IOC/DI+AOP
1. IOC / DI Spring是一个基于IOC和AOP的结构J2EE系统的框架 IOC 反转控制 是Spring的基础,Inversion Of Control 简单说就是创建对象由以前的程序员 ...
- Spring框架-IOC/DI详细学习
一.IOC/DI概念 参考博客:https://www.cnblogs.com/xdp-gacl/p/4249939.html IOC(inversion of control, 控制反转)是一种设计 ...
- AutoFac IoC DI 依赖注入
AutoFac IoC DI 依赖注入 记录点点滴滴知识,为了更好的服务后来者! 一.为什么使用AutoFac? 之前介绍了Unity和Ninject两个IOC容器,但是发现园子里用AutoFac的貌 ...
- SSH项目搭建(二)
本章讲解SSH项目需要到哪些jar包,及各个jar包的作用 一.struts2 1.下载好struts2,struts2文件夹>>>>apps>>>>a ...
- 【Java EE 学习 67 上】【OA项目练习】【JBPM工作流的使用】
OA项目中有极大可能性使用到JBPM框架解决流程控制问题,比如请假流程.报销流程等等. JBPM:JBoss Business Process Management,翻译过来就是业务流程管理.实际上就 ...
- 关于依赖注入IOC/DI的感想
之前一直不明白依赖注入有什么好处,甚至觉得它是鸡肋,现在想想,当时真是可笑. 这个想法正如同说接口是没有用处一样. 当整个项目非常庞大,各个方法之间的调用非常复杂,那么,可以想象一下,假设说没有任何的 ...
随机推荐
- HOJ 13102 Super Shuttle (圆的反演变换)
HOJ 13102 Super Shuttle 链接:http://49.123.82.55/online/?action=problem&type=show&id=13102 题意: ...
- python 调用aiohttp
1. aiohttp安装 pip3 install aiohttp 1.1. 基本请求用法 async with aiohttp.get('https://github.com') as r: a ...
- ASP.NET中常用输出JS脚本的类(来自于周公博客)
using System; using System.Collections.Generic; using System.Text; using System.Web; using System.We ...
- 【AtCoder】AGC005 F - Many Easy Problems 排列组合+NTT
[题目]F - Many Easy Problems [题意]给定n个点的树,定义S为大小为k的点集,则f(S)为最小的包含点集S的连通块大小,求k=1~n时的所有点集f(S)的和取模92484403 ...
- 20155306 2016-2017-2 《Java程序设计》第七周学习总结
20155306 2016-2017-2 <Java程序设计>第七周学习总结 教材学习内容总结 第十三章 时间与日期 三种时间: 格林威治标准时间(GMT)的正午是太阳抵达天空最高点之时, ...
- 20145226夏艺华 《Java程序设计》第6周学习总结
教材学习内容总结 学习目标 理解流与IO 理解InputStream/OutPutStream的继承架构 理解Reader/Writer继承架构 会使用装饰类 会使用多线程进行并发程序设计 第十章 输 ...
- GDB基本用法
基本命令 进入GDB:#gdb test test是要调试的程序,由gcc test.c -g -o test生成.进入后提示符变为(gdb) . 查看源码:(gdb) l 源码会进行行号提示. 如果 ...
- OpenCV LIBTIFF_4.0 link errors
以前用Caffe用的好好的,今天重装后居然报了很多这样的错误 /usr/lib/libopencv_highgui.so.' 1> /usr/lib/libopencv_highgui.so.' ...
- 【译】第十一篇 Integration Services:日志记录
本篇文章是Integration Services系列的第十一篇,详细内容请参考原文. 简介在前一篇,我们讨论了事件行为.我们分享了操纵事件冒泡默认行为的方法,介绍了父子模式.在这一篇,我们会配置SS ...
- [转]CNN 中千奇百怪的卷积方式大汇总
https://www.leiphone.com/news/201709/AzBc9Sg44fs57hyY.html 推荐另一篇很好的总结:变形卷积核.可分离卷积?卷积神经网络中十大拍案叫绝的操作. ...