spring.net ioc配置问题请教
下面是我配置完成spring.net后操作数据库对象的代码:
public ISysUserService SysUserService { get; set; }
IApplicationContext applicationContext = ContextRegistry.GetContext();
SysUserService = (ISysUserService)applicationContext.GetObject("Server.SysUser");
SysUserService.Save(new SYSUSER { USERNAME="1232222",PASSWORD="111111111"});
我在网上下载了一个demo项目配置完成后直接使用下面语句就可以操作数据库对象了
SysUserService.Save(new SYSUSER { USERNAME="1232222",PASSWORD="111111111"});
请问需要怎么配置。
另外一个问题:我在执行上面一个操作数据对象的时候需要平均需要3-4秒钟才能执行完成。怎么会这么慢呢。 --------------------编程问答-------------------- app.config 代码如下:
--------------------编程问答-------------------- 对应的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=152368
-->
<configuration>
<!--配置节点-->
<configSections>
<!--spring节点-->
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
<section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core" />
</sectionGroup>
<!--log4net节点-->
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
<section name="databaseSettings" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<!--log4net-->
<log4net debug="true">
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="Logs\Log.log" />
<param name="datePattern" value="MM-dd HH:mm" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
</layout>
</appender>
<appender name="HttpTraceAppender" type="log4net.Appender.ASPNetTraceAppender">
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
</layout>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
</layout>
</appender>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="Logs/Log.log" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="10" />
<param name="MaximumFileSize" value="100K" />
<param name="RollingStyle" value="Size" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
</layout>
</appender>
<root>
<level value="ERROR" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
<!--spring-->
<spring>
<context>
<!--Dao-->
<resource uri="assembly://Config/Config.Dao/DaoBase.xml" />
<resource uri="assembly://Config/Config.Dao/Dao.xml" />
<!--Service-->
<resource uri="assembly://Config/Config.Service/ServiceBase.xml" />
<resource uri="assembly://Config/Config.Service/Service.xml" />
<!--Web-->
<resource uri="assembly://Config/Config.Web/Controllers.xml" />
</context>
</spring>
<!--databaseSettings-->
<databaseSettings>
<!--SQL Server-->
<!--local-->
<!--<add key="provider" value="System.Data.SqlClient" />
<add key="connectionString" value="server=.\sqlexpress;database=WeCompanySite;uid=sa;pwd=;" />
<add key="dialect" value="NHibernate.Dialect.MsSql2008Dialect" />
<add key="driver_class" value="NHibernate.Driver.SqlClientDriver"/>-->
<!--Oracle-->
<add key="provider" value="System.Data.OracleClient" />
<add key="connectionString" value="Data Source=zm;User ID=hlhfuser;Password=hlhfuser001;" />
<add key="dialect" value="NHibernate.Dialect.Oracle9iDialect" />
<add key="driver_class" value="NHibernate.Driver.OracleClientDriver" />
<!--自动建表-->
<!--<add key="hbm2ddl.auto" value="update" />-->
</databaseSettings>
<appSettings>
<add key="Spring.Data.NHibernate.Support.OpenSessionInViewModule.SessionFactoryObjectName" value="NHibernateSessionFactory"/>
<add key="FckProvider" value="WebSite.Core.UserPathFckProvider, WebSite"/>
<add key="IsDebug" value="true"/>
</appSettings>
<system.web>
<httpRuntime requestValidationMode="2.0"/>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
</compilation>
<authentication mode="Forms">
<forms loginUrl="~/LogOn" timeout="2880" />
</authentication>
<pages validateRequest="false">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
</namespaces>
</pages>
<httpModules>
<add name="OpenSessionInView" type="Spring.Data.NHibernate.Support.OpenSessionInViewModule, Spring.Data.NHibernate30"/>
</httpModules>
<!--显示错误-->
<customErrors mode="Off"/>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true" >
<add name="OpenSessionInView" type="Spring.Data.NHibernate.Support.OpenSessionInViewModule, Spring.Data.NHibernate30"/>
</modules>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<object type="Test.SessionManagerFixture, Test" singleton="false" >
<property name="SysUserService" ref="Server.SysUser"/>
</object>
</objects>
虽然我app.config里面引用了 Controllers.xml文件。但是使用语句:
(ISysUserService)applicationContext.GetObject("SysUserService");
时报错提示:
Test 'Test.SessionManagerFixture.CreateUser' failed: Spring.Objects.Factory.NoSuchObjectDefinitionException : No object named 'SysUserService' is defined : Cannot find definition for object [SysUserService]
如果使用
(ISysUserService)applicationContext.GetObject("Server.SysUser");
则成功,如果这样那不就是跳过了 Controllers.xml了吗 --------------------编程问答-------------------- 没有人吗 --------------------编程问答-------------------- 自己顶 --------------------编程问答-------------------- IApplicationContext applicationContext = ContextRegistry.GetContext();
SysUserService = (ISysUserService)applicationContext.GetObject("Server.SysUser");
----------------------
注入应该是写在配置里。
如果配置正确的话,这两句代码就可以删除了。。。 --------------------编程问答--------------------
我也是认为,不需要上面两行代码的,我看别人的demo里面也没有。
配置相同的,弄了很长时间还不行
--------------------编程问答-------------------- 有人吗
补充:.NET技术 , ASP.NET