当前位置: 移动技术网 > IT编程>开发语言>.net > 接之前的文章,VS2017中使用Spring.NET配置以及使用方法(framework4.6.1超详细)

接之前的文章,VS2017中使用Spring.NET配置以及使用方法(framework4.6.1超详细)

2019年04月04日  | 移动技术网IT编程  | 我要评论

四川爱华航空学院,thinkpad笔记本,老板娘足足叫了一夜

    众所周知,spring在java中是很常见的框架,spring.net虽然体积比较大,但是功能相对齐全,本文介绍在vs2017 .net framework 4.6.1环境下,如何快速使用spring.net的ioc功能。话不多说,开撸demo!

1 准备工作

1.1新建解决方案文件夹,新建bll、ibll类库项目:

1.2 在ibll层中添加测试接口itest.cs

    public interface itest
    {
        string getname();
        int getage();
    }

1.3 在bll层中添加具体的实现类testbll并继承1.2中接口:

public  class testbll: itest
{
        public string getname()
        {
            return "test";
        }

        public int getage()
        {
            return 12;
        }
}

1.4 添加webtest 项目,并添加相应的测试controller:

 

2 引入spring.net所需nuget包

2.1 在webtest中添加nuget包:

2.2 在web.config文件中配置spring.net节点:

<configuration>
  <configsections>
    <sectiongroup name="spring">
      <section name="context" type="spring.context.support.contexthandler,spring.core" />
    </sectiongroup>
  </configsections>
  <!--spring.net节点配置-->
  <spring>
    <!--容器配置-->
    <context>
      <resource uri="file://~/config/controller.xml" />
      <resource uri="file://~/config/service.xml" />
    </context>
  </spring>

2.3 在webtest目录下新建config文件夹并新建controller.xml和service.xml文件,配置如下:

 2.3.1 controller.xml

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
  <description>an  example that demonstrates simple ioc features.</description>
  <!--object的name可以自定义,property中的属性name不能自定义-->
  <object  type="webtest.controllers.springdonettestcontroller,webtest" singleton="false">
    <property name="itest" ref="testservice"></property>
  </object>
</objects>

2.3.2 service.xml

<?xml version="1.0" encoding="utf-8" ?>
<objects>
  <object name="testservice" type="bll.testbll,bll">
  </object>
</objects>

这两个文件在web.config文件中已经被添加,记得controller文件要放在上面。

2.4 让spring.net接管mvcapplication:

在global文件中做如下调整:

3 测试

3.1 controller文件中添加如下代码,记得添加相应的index视图:

 

public class springdonettestcontroller : controller
    {

        public itest itest { get; set; }
        // get: springdonettest
        public actionresult index()
        {
           var name =  itest.getname();
            return view(name);
        }
    }

3.2 打断点测试

 

4 可能出现的情况

4.1  未能加载文件或程序集“system.web.http, version=5.0.0.0, culture=neutral, publickeytoken=31bf3856ad364e35”或它的某一个依赖项。系统找不到指定的文件。

解决方法:nuget安装或更新microsoft.aspnet.webapi,

      安装命令:install-package microsoft.aspnet.webapi

                   更新命令:update-package microsoft.aspnet.webapi

 4.2  

解决方法:引用bll以及ibll

 

5 后记

  本篇demo在github可以下载,下载地址为:https://github.com/wangbank/springdonetforvs2017

   这篇文章是关于spring.net中ioc功能的简单实用,以后会逐渐更新其aop功能以及其他ioc框架,譬如unity、autofac等等,希望大家可以支持。

   

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网