spring.net+.net mvc 的搭建方法

本项目使用.net mvc5 和spring.net2

使用nuget引入 spring.core、spring.data、spring.Aop、spring.Web等基础DLL后加入spring.web.mvc5

修改 Global.asax文件

 public class MvcApplication : SpringMvcApplication  
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        protected void Application_Error(object sender, EventArgs args)
        {
            Exception ex = Server.GetLastError();
            ILoger iLoger = new Log4NetLogging();
            iLoger.Error("webapp",ex);
            if (ex is HttpException)
            {
                Response.Redirect("~/Account/Error/" + ((HttpException)ex).GetHttpCode());
            }
            else 
            {
                Response.Redirect("~/Account/Error");
            }
        }
    }

web.config中加入 spring配置文件

<configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc5" />
      <section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core" />
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
    </sectionGroup>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    <section name="razorJSSettings" type="RazorJS.Configuration.RazorJSSettings, RazorJS" />
  </configSections>

 

<spring>
    <context>
      <resource uri="~/Config/SpringConfigs/EntityConfig.xml" />
      <resource uri="~/Config/SpringConfigs/RepositoryConfig.xml" />
      <resource uri="~/Config/SpringConfigs/ServiceConfig.xml" />
      <resource uri="~/Config/SpringConfigs/ControllerConfig.xml" />
      <resource uri="~/Config/SpringConfigs/Aop_AdviceConfig.xml" />
      <resource uri="~/Config/SpringConfigs/Aop_PointcutConfig.xml" />
      <resource uri="~/Config/SpringConfigs/AopConfig.xml" />
      <resource uri="~/Config/SpringConfigs/Infrastructure/LoggingConfig.xml" />
      <resource uri="~/Config/SpringConfigs/Infrastructure/SchedulerConfig.xml" />
      <resource uri="~/Config/SpringConfigs/Hosting/HandlerConfig.xml" />
    </context>
    <parsers>
            <parser type="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data" />
            <parser type="Spring.Transaction.Config.TxNamespaceParser, Spring.Data" />
    </parsers>
  </spring>

将Controller类配置到IOC池中

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"  xmlns:aop="http://www.springframework.net/aop">
  <object id="AccountControllerService" type="Com.JingPai.Pip.UI.Controllers.Account.AccountController, Com.JingPai.Pip.UI" singleton="false" >
    <property name="AccountService" ref="AccountService"></property>
  </object>
  <object id="HomeControllerService" type="Com.JingPai.Pip.UI.Controllers.Home.HomeController, Com.JingPai.Pip.UI" singleton="false" >
    <property name="AccountService" ref="AccountService"></property>
  </object>
  <object id="SchedulerControllerService" type="Com.JingPai.Pip.UI.Controllers.Infrastructure.SchedulerController, Com.JingPai.Pip.UI" singleton="false">
    <property name="SchedulerService" ref="SchedulerService"></property>
  </object>
  <object id="PowerControllerService" type="Com.JingPai.Pip.UI.Controllers.Power.PowerController, Com.JingPai.Pip.UI" singleton="false">
    <property name="PowerService" ref="PowerService"></property>
  </object>
  <object id="PowerHelperControllerService" type="Com.JingPai.Pip.UI.Controllers.Tools.PowerHelperController, Com.JingPai.Pip.UI" singleton="false">
    <property name="PowerHelperService" ref="PowerHelperService"></property>
  </object>
  <object id="CommonControllerService" type="Com.JingPai.Pip.UI.Controllers.SystemSet.CommonController, Com.JingPai.Pip.UI" singleton="false">
    <property name="SystemSetService" ref="SystemSetService"></property>
    <property name="PowerHelperControllerService" ref="PowerHelperControllerService"></property>
    
  </object>
  
</objects>

欧了

posted @ 2016-02-04 09:34  红色十字军  阅读(277)  评论(0)    收藏  举报