代码改变世界

Spring.NET配置数据库连接

2011-12-07 11:37  Ref Tian  阅读(797)  评论(0编辑  收藏  举报

学习spring.net以来一直使用网上流传比较广的一份中文参考文档,最近在配置数据库连接时总是报错,后来查了下最新的英文帮助文档,发现命名空间改变了现将新旧连接做记录,今后还是要多看原始的文档啊~~~~~

就版本:

<objects xmlns='http://www.springframework.net'
xmlns:d
="http://www.springframework.net/database">

<d:dbProvider id="DbProvider"
provider
="System.Data.SqlClient"
connectionString
="Data Source=(local);Database=Spring;User ID=springqa;Password=springqa;Trusted_Connection=False"/>

<object id="adoTemplate" type="Spring.Data.AdoTemplate, Spring.Data">
<property name="DbProvider" ref="DbProvider"/>
</object>

</objects>

主程序的配置文件中注册

<configuration>

  <configSections>
    <sectionGroup name="spring">
      <section name="parsers" type="Spring.Context.Support.ConfigParsersSectionHandler, Spring.Core" />
    </sectionGroup>

  </configSections>

  <spring>
    <parsers>
      <parser namespace="http://www.springframework.net/database"
              type="Spring.Data.DatabaseConfigParser, Spring.Data"
              schemaLocation="assembly://Spring.Data/Spring.Data/spring-database.xsd" />
    </parsers>
  </spring>

</configuration>

新版:

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns='http://www.springframework.net'
xmlns:db
="http://www.springframework.net/database">

<db:provider id="DbProvider"
provider
="System.Data.SqlClient"
connectionString
="Data Source=10.194.18.158;Database=efox;User ID=efox;Password=efox$88;Trusted_Connection=False"/>

<object id="adoTemplate" type="Spring.Data.Core.AdoTemplate, Spring.Data">
<property name="DbProvider" ref="DbProvider"/>
</object>

</objects>

注册配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
<section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>

<spring>

<parsers>
<parser type="Spring.Remoting.Config.RemotingNamespaceParser, Spring.Services" />
<parser type="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data" />
</parsers>

<context>
<resource uri="assembly://ServerMain/ServerMain.config/Components.xml"/><!--配置注入的容器-->
<resource uri="assembly://ServerMain/ServerMain.config/DbProvider.xml"/>
<resource uri="config://spring/objects" />
</context>
<objects xmlns="http://www.springframework.net" xmlns:r="http://www.springframework.net/remoting">
<!--這個是必須的-->
</objects>
</spring>
<!--配置通信的信道類型和端口-->
<system.runtime.remoting>
<application>
<channels>
<channel ref="tcp" port="8000"></channel>
</channels>
</application>
</system.runtime.remoting>

</configuration>

粗体部分是有变化的地方,原配置是直接 拷的中文文档,新的是我用到的配置项目。