本节介绍如何实现将controller加载进spring容器。首先我们看看配置文件,我就以我做的例子来进行说明先看webConfig,在configuration节点下配置以下内容
<configSections>
        <sectionGroup name="spring">
            <section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web"/>
            <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
        </sectionGroup>
        <section name="SpringOverrideProperty" type="System.Configuration.NameValueSectionHandler"/>
        <section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    </configSections>
    <spring>
        <context>
            <resource uri="config://spring/objects"/>
            <resource uri="assembly://SpringDao/SpringDao/spring_hibernate_dao.xml"/>
            <resource uri="assembly://SpringDao/SpringDao/Controller.xml"></resource>
        </context>
        <objects xmlns="http://www.springframework.net"/>
    </spring>
在system.web节点下配置以下内容
<httpModules>
            <add name="Spring" type="Spring.Context.Support.WebSupportModule, Spring.Web"/>
        </httpModules>
        <httpHandlers>
            <add verb="*" path="*.aspx" type="Spring.Web.Support.PageHandlerFactory, Spring.Web"/>
        </httpHandlers>
ok,WebConfig配置完成。再看我写的两个xml,第一个Controller.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 id="ssCode" type="Model.SS_CODE,Model">
        <property name="C_DATA" value="2"></property>
    </object>
    <object id="CodeController" type="Controllers.CodeController,NormalUniversity" singleton="false"    autowire="byName" >
        <property    name="ssCode" ref="ssCode"></property>
    </object>
</objects>
这个文件配置的是Spring要加载进容器的Controller。以及controller中的类(Controllers.CodeController,NormalUniversity,其中Controllers.CodeController是命名空间.类名,NormalUniversity是程序集名称),以及类中的属性。下面是spring_hibernate_dao.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 id="DbProvider" type="SpringDao.SQLProvider, SpringDao">
        <property name="ConnectionString" value="server=.\sql2k5;uid=sa;pwd=sa123;database=test;" />
    </object>

    <object id="SessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate12">
        <property name="DbProvider" ref="DbProvider" />
        <property name="MappingAssemblies">
            <list>
                <value>Model</value>
            </list>
        </property>
        <property name="HibernateProperties">
            <dictionary>
                <entry key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
                <entry key="hibernate.dialect" value="NHibernate.Dialect.MsSql2005Dialect" />
                <entry key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
                <entry key="show_sql" value="true" />
            </dictionary>
        </property>
    </object>

    <object id="HibernateTransactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate12">
        <property name="DbProvider" ref="DbProvider" />
        <property name="sessionFactory" ref="sessionFactory" />
    </object>

    <object id="TransactionInterceptor" type="Spring.Transaction.Interceptor.TransactionInterceptor, Spring.Data">
        <property name="TransactionManager" ref="HibernateTransactionManager" />
        <property name="TransactionAttributeSource">
            <object type="Spring.Transaction.Interceptor.AttributesTransactionAttributeSource, Spring.Data" />
        </property>
    </object>
    <object id="DaoHelper" type="Dao.SS_CodeDao,Dao">
        <property name="sessionFactory" ref="sessionFactory" />
    </object>

</objects>
配置了Spring的工厂,拦截器,事务。以及我写的一个Dao类。要实现controller的加载,必须写一个自定义的ControllerFactory实现IControllerFactory接口。代码如下
asp.net MVC2 初探十一_休闲using System;
asp.net MVC2 初探十一_休闲using System.Collections.Generic;
asp.net MVC2 初探十一_休闲using System.Linq;
asp.net MVC2 初探十一_休闲using System.Text;
asp.net MVC2 初探十一_休闲using Spring.Context;
asp.net MVC2 初探十一_休闲using System.Web.Mvc;
asp.net MVC2 初探十一_休闲using System.Web.Routing;
asp.net MVC2 初探十一_休闲using System.Web;
asp.net MVC2 初探十一_休闲namespace SpringDao
asp.net MVC2 初探十一_休闲{
asp.net MVC2 初探十一_休闲        public class ControllerFactory : IControllerFactory
asp.net MVC2 初探十一_休闲        {
asp.net MVC2 初探十一_休闲                private static DefaultControllerFactory defalutf = null;
asp.net MVC2 初探十一_休闲
asp.net MVC2 初探十一_休闲                public IController CreateController(RequestContext requestContext, string controllerName)
asp.net MVC2 初探十一_休闲                {
asp.net MVC2 初探十一_休闲                        string controller = controllerName + "Controller";
asp.net MVC2 初探十一_休闲                    
asp.net MVC2 初探十一_休闲                        IApplicationContext ctx = Container.GetContext();
asp.net MVC2 初探十一_休闲                        if (ctx.ContainsObject(controller))
asp.net MVC2 初探十一_休闲                        {
asp.net MVC2 初探十一_休闲                                object controllerf = ctx.GetObject(controller);
asp.net MVC2 初探十一_休闲                                return (IController)controllerf;
asp.net MVC2 初探十一_休闲                        }
asp.net MVC2 初探十一_休闲                        else
asp.net MVC2 初探十一_休闲                        {
asp.net MVC2 初探十一_休闲                                if (defalutf == null)
asp.net MVC2 初探十一_休闲                                {
asp.net MVC2 初探十一_休闲                                        defalutf = new DefaultControllerFactory();
asp.net MVC2 初探十一_休闲                                }
asp.net MVC2 初探十一_休闲
asp.net MVC2 初探十一_休闲                                return defalutf.CreateController(requestContext, controllerName);
asp.net MVC2 初探十一_休闲                        }
asp.net MVC2 初探十一_休闲                }
asp.net MVC2 初探十一_休闲
asp.net MVC2 初探十一_休闲                public void ReleaseController(IController controller)
asp.net MVC2 初探十一_休闲                {
asp.net MVC2 初探十一_休闲                        IApplicationContext ctx = Container.GetContext();
asp.net MVC2 初探十一_休闲
asp.net MVC2 初探十一_休闲                        if (!ctx.ContainsObject(controller.GetType().Name))
asp.net MVC2 初探十一_休闲                        {
asp.net MVC2 初探十一_休闲                                if (defalutf == null)
asp.net MVC2 初探十一_休闲                                {
asp.net MVC2 初探十一_休闲                                        defalutf = new DefaultControllerFactory();
asp.net MVC2 初探十一_休闲                                }
asp.net MVC2 初探十一_休闲
asp.net MVC2 初探十一_休闲                                defalutf.ReleaseController(controller);
asp.net MVC2 初探十一_休闲                        }
asp.net MVC2 初探十一_休闲                }
asp.net MVC2 初探十一_休闲
asp.net MVC2 初探十一_休闲        }
asp.net MVC2 初探十一_休闲}
asp.net MVC2 初探十一_休闲using System;
asp.net MVC2 初探十一_休闲using System.Collections.Generic;
asp.net MVC2 初探十一_休闲using System.Linq;
asp.net MVC2 初探十一_休闲using System.Text;
asp.net MVC2 初探十一_休闲using Spring.Context;
asp.net MVC2 初探十一_休闲using Spring.Context.Support;
asp.net MVC2 初探十一_休闲using System.Collections;
asp.net MVC2 初探十一_休闲namespace SpringDao
asp.net MVC2 初探十一_休闲{
asp.net MVC2 初探十一_休闲        public class Container
asp.net MVC2 初探十一_休闲        {
asp.net MVC2 初探十一_休闲                /// <summary>
asp.net MVC2 初探十一_休闲                /// 获取应用程序上下文.
asp.net MVC2 初探十一_休闲                /// </summary>
asp.net MVC2 初探十一_休闲                /// <returns><see cref="IApplicationContext"/>应用程序上下文.</returns>
asp.net MVC2 初探十一_休闲                public static IApplicationContext GetContext()
asp.net MVC2 初探十一_休闲                {
asp.net MVC2 初探十一_休闲                        if (FApplicationContext == null)
asp.net MVC2 初探十一_休闲                        {
asp.net MVC2 初探十一_休闲                                FApplicationContext = ContextRegistry.GetContext();
asp.net MVC2 初探十一_休闲                        }
asp.net MVC2 初探十一_休闲                        return FApplicationContext;
asp.net MVC2 初探十一_休闲                }
asp.net MVC2 初探十一_休闲
asp.net MVC2 初探十一_休闲                /// <summary>
asp.net MVC2 初探十一_休闲                /// 获取应用程序上下文.
asp.net MVC2 初探十一_休闲                /// </summary>
asp.net MVC2 初探十一_休闲                /// <param name="name"><see cref="IApplicationContext"/>应用程序上下文名称.</param>
asp.net MVC2 初探十一_休闲                /// <returns><see cref="IApplicationContext"/>应用程序上下文.</returns>
asp.net MVC2 初探十一_休闲                public static IApplicationContext GetContext(string name)
asp.net MVC2 初探十一_休闲                {
asp.net MVC2 初探十一_休闲                        return ContextRegistry.GetContext(name);
asp.net MVC2 初探十一_休闲                }
asp.net MVC2 初探十一_休闲
asp.net MVC2 初探十一_休闲                /// <summary>
asp.net MVC2 初探十一_休闲                /// 获取对象.
asp.net MVC2 初探十一_休闲                /// </summary>
asp.net MVC2 初探十一_休闲                /// <typeparam name="T">对象的类型.</typeparam>
asp.net MVC2 初探十一_休闲                /// <param name="id">标识.</param>
asp.net MVC2 初探十一_休闲                /// <returns></returns>
asp.net MVC2 初探十一_休闲                public static T GetObject<T>(string id)
asp.net MVC2 初探十一_休闲                {
asp.net MVC2 初探十一_休闲                        return (T)GetContext().GetObject(id);
asp.net MVC2 初探十一_休闲                }
asp.net MVC2 初探十一_休闲
asp.net MVC2 初探十一_休闲                /// <summary>
asp.net MVC2 初探十一_休闲                /// 获取对象类表.
asp.net MVC2 初探十一_休闲                /// </summary>
asp.net MVC2 初探十一_休闲                /// <typeparam name="T">对象的类型.</typeparam>
asp.net MVC2 初探十一_休闲                /// <returns></returns>
asp.net MVC2 初探十一_休闲                public static IList<T> GetObjects<T>()
asp.net MVC2 初探十一_休闲                {
asp.net MVC2 初探十一_休闲                        IEnumerable items = GetContext().GetObjectsOfType(typeof(T));
asp.net MVC2 初探十一_休闲                        IList<T> objects = new List<T>();
asp.net MVC2 初探十一_休闲                        foreach (DictionaryEntry item in items)
asp.net MVC2 初探十一_休闲                        {
asp.net MVC2 初探十一_休闲                                objects.Add((T)item.Value);
asp.net MVC2 初探十一_休闲                        }
asp.net MVC2 初探十一_休闲                        return objects;
asp.net MVC2 初探十一_休闲                }
asp.net MVC2 初探十一_休闲
asp.net MVC2 初探十一_休闲                [ThreadStatic]
asp.net MVC2 初探十一_休闲                private static IApplicationContext FApplicationContext;
asp.net MVC2 初探十一_休闲
asp.net MVC2 初探十一_休闲        }
asp.net MVC2 初探十一_休闲}
这些代码是我从网上直接copy的,直接拿来用。完了之后我们要把controller的工厂重新指定到我们自定义的工厂。
在Global.asax的Application_Start()方法中,加入以下代码
asp.net MVC2 初探十一_休闲ControllerBuilder.Current.SetControllerFactory(typeof(SpringDao.ControllerFactory));
然后再看看我们的controller
asp.net MVC2 初探十一_休闲using System;
asp.net MVC2 初探十一_休闲using System.Collections.Generic;
asp.net MVC2 初探十一_休闲using System.Collections;
asp.net MVC2 初探十一_休闲using System.Linq;
asp.net MVC2 初探十一_休闲using System.Web;
asp.net MVC2 初探十一_休闲using System.Web.Mvc;
asp.net MVC2 初探十一_休闲using Model;
asp.net MVC2 初探十一_休闲using NHibernate.Expression;
asp.net MVC2 初探十一_休闲namespace Controllers
asp.net MVC2 初探十一_休闲{
asp.net MVC2 初探十一_休闲        public class CodeController : BaseController<SS_CODE>
asp.net MVC2 初探十一_休闲        {
asp.net MVC2 初探十一_休闲                private SS_CODE ssCode { set; get; }
asp.net MVC2 初探十一_休闲
asp.net MVC2 初探十一_休闲                public ActionResult Index()
asp.net MVC2 初探十一_休闲                {
asp.net MVC2 初探十一_休闲                        ssCode.C_DATA = "aaaa";
asp.net MVC2 初探十一_休闲                        return base.Index1("~/Views/System/Code/Index.aspx");
asp.net MVC2 初探十一_休闲                }
asp.net MVC2 初探十一_休闲        }
asp.net MVC2 初探十一_休闲}
在这里我们不用实例化SS_CODE 就可以直接使用其对象。最后讲一下Log4Net。用于在客户端写入日志,配置如下
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
    <logger name="WebLogger">
        <level Value="DEBUG"></level>
        <appender-ref ref="LogFileAppender" />
        <appender-ref ref="RollingLogFileAppender"></appender-ref>
    </logger>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
        <!--日志文件名开头-->
        <file value="App_Data\\Log.txt" />
        <!--是否追加到文件-->
        <appendToFile value="true" />
        <!--混合使用日期和文件大小变换日志文件名-->
        <rollingStyle value="Composite" />
        <!--日期的格式-->
        <datePattern value="yyyyMMdd" />
        <!--最大变换数量-->
        <maxSizeRollBackups value="10" />
        <!--最大文件大小-->
        <maximumFileSize value="1MB" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
        </layout>
    </appender>
</log4net>
然后新建一个txt如下
asp.net MVC2 初探十一_ASP.NET MVC2_141
打开Assembly,加入
asp.net MVC2 初探十一_职场_142[assembly: log4net.Config.XmlConfigurator(ConfigFile = @"App_Data\Log4Net.Config.xml", Watch = true)]
在web.config configSections加入以下下配置
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
controller中的代码
asp.net MVC2 初探十一_职场_142 ILog lg = log4net.LogManager.GetLogger("WebLogger");
asp.net MVC2 初探十一_职场_142                        {
asp.net MVC2 初探十一_职场_142                                if (lg.IsErrorEnabled)
asp.net MVC2 初探十一_职场_142                                {
asp.net MVC2 初探十一_职场_142                                        lg.Error(this.GetType().ToString()+"xxxx李磊");
asp.net MVC2 初探十一_职场_142                                        
asp.net MVC2 初探十一_职场_142                                }
asp.net MVC2 初探十一_职场_142                        }
我们发现不能写入汉字,晕啊
asp.net MVC2 初探十一_休闲_151
怎么办呢?找了好久,我才找到这个办法。首先我们把Log4Net.dll给反射出来
asp.net MVC2 初探十一_ASP.NET MVC2_152
反射到F盘下,我们看看反射的程序集
asp.net MVC2 初探十一_ASP.NET MVC2_153
我们展开log4net\Util,打开Transform.cs,注释掉private static Regex INVALIDCHARS哪一行,重新编译生成dll,就可以写入汉字了。
asp.net MVC2 初探十一_ASP.NET MVC2_154
有什么问题联系我QQ:316575647