您的位置:首页 > 编程语言 > ASP

【搭建Spring.net+EF+Asp.net MVC框架】---(3) 配置详解

2017-12-27 21:04 651 查看
引用块内容

添加引用:

在表示层添加上spring.net需要的dll文件引用,所需的dll文件,分享下载源:链接:https://pan.baidu.com/s/1hser0Ba 密码:kky2

更改配置文件:

一、修改Global.asax文件

主要是修改两个地方,一是添加头文件引用 using Spring.Web.Mvc,二是更改MVC项目的继承为SpringMvcApplication

上代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
using Spring.Web.Mvc;          //1.添加引用

namespace SpringMvcTest
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801 public class MvcApplication : SpringMvcApplication      // System.Web.HttpApplication  2.更改扩展
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();

WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
}


二、新建XML文件:

SpringMvcTest中添加三个配置文件Controller、B层、D层各对应一个



三、修改Web.config文件:

修改SpringMvcTest下的Web.config文件



<!--1.首节点-->
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc4"/>
<!--<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>-->
<section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core" />
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>


<!--2.添加三个配置文件的路径-->
<spring>
<context>
<!--直接配置【配置文件的objects标签】 告诉spring.net容器,对象的配置节点就在 当前配置文件的spring/objects节点中-->
<resource uri="config://spring/objects"/>
<!--以下是三个配置绝对路径文件-->
<resource uri="file://~/XML_Control.xml"/>
<resource uri="file://~/XML_BLL.xml"/>
<resource uri="file://~/XML_DAL.xml"/>
</context>
<objects xmlns="http://www.springframework.net">
</objects>
</spring>


四、编辑三个配置文件

Spring.NET的对象都是在XML里定义的,所以配置文件至关重要。对象需要配置在objects节下,object节的id是对象ID,后面是完整类型名称,如果类含有构造函数,通过构造函数创建对象。关于spring.net配置文件的详解请点击链接,本文仅对该教程中用到的部分节点做总结。Spring.net配置文件各节点详解

object节的属性:默认是单例对象,如果想让对象不单例,需要配置参数type后面跟的是命名空间.类名,程序集名称,

子节点property为设置对象的值,即设置属性值,ref:注入其他类型的对象作为自己的属性.

XML_Control.xml Porperty节点中设置ref属性值为iuserbll,因为我们注入了HelloController.cs引用的IUserBLL接口iuserbll,所以这里添加了ref属性值为iuserbll。Name属性用于找到与之相连的XML_BLL配置文件对象的ID。

同理XML_BLL.xml中的name与XML_DAL.xml的id保持一致,Ref值为注入的对象.

Contorller注入的对象



B层注入的对象



D层没有注入,此处省略。

XML_Control.xml配置

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<description>An  example that demonstrates simple IoC features.</description>

<object type="SpringMvcTest.Controllers.HelloController,SpringMvcTest" singleton="false">
<property name="iuserbll" ref="iuserbll"/>   <!--Controller引用IBLL-->
</object>

</objects>


XML_BLL.xml配置

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<description>An  example that demonstrates simple IoC features.</description>
<!--1.设置id(B层 userbll(对外访问的接口),命名空间.类名,程序集名称, >-->  <!--2.再设置要转接的IDAL层-->
<object id="iuserbll" type="BLL.UserBLL,BLL" singleton="false">
<property name="idal" ref="idal"/>                <!--BLL引用IDAL-->
</object>
</objects>


XML_DAL.xml配置

D层除了实现接口外没有添加对任何层引用,没有对象注入,所以这里没有用到子节点property。

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<description>An  example that demonstrates simple IoC features.</description>

<object id="idal" type="DAL.UserDAL,DAL" singleton="false">
</object>

</objects>


最后更改D层生成路径到表示层:

D层生成的dll文件在DAL的bin下面,但是没有在SpringMvcTest的bin下,会导致因找不到D层的dll文件而无法运行,所以这里需要在D层的属性中改一下生成路径。



效果展示

还记得上篇的demo实现里面我们在D层写的判断么? 输入非零书,则返回true,输入0则返回false,对应显示用户是否存在,话不多说看效果图吧。

输入0,返回false



输入非0,返回true

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐