English 中文(简体)
出入配置 经理。 AppSettings Value from Spring. NET×ml配置
原标题:Accessing ConfigurationManager.AppSettings value from Spring.NET xml configuration

我的一项要求是,要求我使用普林内联网,以获得在座标内储存的连接,然后将检索的连接注入一个瞬时物体。

我如何利用春天这样做。 净Xml配置?

例如,我没有这样做的守则:

// Spring.net config:
<object name="myService" type="com.acme.MyService, com.acme">
    <constructor-arg type="System.String" value="myConnectionName"/>
</object>

// Web.config:
<connectionStrings>
    <add name="myConnectionName" connectionString="DB_connectionstring"/>
</connectionStrings>
// Codes:
public class MyService {
    public MyService(string connectionName) {
        var connectionString = ConfigurationManager.AppSettings[connectionName];
        // use connectionString to create a DB connection, etc
    }
}

我想这样说:

 // Spring.net config:
<object name="myService" type="com.acme.MyService, com.acme">
    <constructor-arg type="System.String" ref="retrievedConnectionString"/>
</object>    
// How to make a call similar to "ConfigurationManager.AppSettings[connectionName]" and get the connection string from Web.config and put inside "retrievedConnectionString"?

// Web.config:
<connectionStrings>
    <add name="myConnectionName" connectionString="DB_connectionstring"/>
</connectionStrings>
// Codes:
public class MyService {
    public MyService(string connectionString) {
        // use connectionString to create a DB connection, etc
    }
}

甚至可以打电话<代码> ConfigurationManager.AppSettings [.] from Spring.net xml config?

最佳回答

过去,我曾使用,以达到这一目的,但通过这一问题和bbaia的回答,我发现,这样做的最好办法是使用。 VariablePlaceholderConfigurer 。 当你使用<代码>VariablePlace holderConfigurer而不是我的“expression-hack”,你不与appSettings /linkStrings位位位位位位位位别:您可转向VariableSources /code>><<<<<<>>>>> 由春季提供。 页: 1

方框外,春季,NET提供<代码>可变PlaceholderConfigurer,以从标准中检索变量。 诸如AppSettings,ConnectionStrings,UserSettingsApplicationSettings。 bbaia的回答部分说明了这一点,你举了一个完整的例子。

"Expression hack": calling ConfigurationManager from xml config

因此,我不建议你这样做,但这是我过去使用的黑板,适用于你的配置:

<object object name="myService" type="com.acme.MyService, com.acme">
  <constructor-arg name="Connection" 
                   expression="T(System.Configuration.ConfigurationManager).ConnectionStrings[ myConnectionName ]" />
</object>

You can use this same approach for ConfigurationManager.AppSettings, e.g.:

<object object name="myService" type="com.acme.MyService, com.acme">
  <constructor-arg name="AnotherConstructorArgument" 
                   expression="T(System.Configuration.ConfigurationManager).AppSettings[ mySetting ]" />
</object>

VariablePlaceholderConfigurer: reference .NET settings from Spring.NET xml config

您可以轻而易举地配置一个<代码>可变的PlaceholderConfigurer,以便从标准中检索变量。 诸如AppSettings,ConnectionStrings,UserSettingsApplicationSettings。 例如,考虑这一xml配置:

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

  <object type="Spring.Objects.Factory.Config.VariablePlaceholderConfigurer, Spring.Core">
    <property name="VariableSources">
      <list>
        <object type="Spring.Objects.Factory.Config.ConnectionStringsVariableSource, Spring.Core" />
        <object type="Spring.Objects.Factory.Config.ConfigSectionVariableSource, Spring.Core">
          <!-- Sections to read, sepearated by comma (leave out spaces) -->
          <property name="SectionNames"
                    value="appSettings,applicationSettings/q7991262.Properties.Settings,userSettings/q7991262.Properties.Settings" />
        </object>
      </list>
    </property>
  </object>

  <!-- Note that you have to append  .connectionstring  to the key! -->
  <object id="usingConnectionStringsVariableSource" 
          type="q7991262.MyService, q7991262">
    <property name="Connection" 
              value="${myConnectionName.connectionString}" />
  </object>

  <object id="configSectionVariableSource" 
          type="q7991262.MyService, q7991262">
    <property name="Connection" 
              value="${myConnectionNameAppSettings}" />
  </object>

  <object id="userSettingsSection" 
          type="q7991262.MyService, q7991262">
    <property name="Connection" 
              value="${myConectionNameUserSetting}" />
  </object>

  <object id="applicationSetting" 
          type="q7991262.MyService, q7991262">
    <property name="Connection" 
              value="${myConectionNameApplicationSetting}" />
  </object>

</objects>

本条案文为<代码>app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="q7991262.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
    </sectionGroup>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="q7991262.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>

  <connectionStrings>
    <add name="myConnectionName" 
         connectionString="From connection string section."/>
  </connectionStrings>

  <appSettings>
    <add key="myConnectionNameAppSettings" 
         value="From app setting section." />
  </appSettings>

  <userSettings>
    <q7991262.Properties.Settings>
      <setting name="myConectionNameUserSetting" serializeAs="String">
        <value>My connection from user settings.</value>
      </setting>
    </q7991262.Properties.Settings>
  </userSettings>

  <applicationSettings>
    <q7991262.Properties.Settings>
      <setting name="myConectionNameApplicationSetting" serializeAs="String">
        <value>My connection from application settings.</value>
      </setting>
    </q7991262.Properties.Settings>
  </applicationSettings>

</configuration>

这些配置从。 该工作样本见

问题回答

Another solution is to use the VariablePlaceholderConfigurer : http://www.springframework.net/doc-latest/reference/html/objects.html#objects-variablesource

“联系渠道”的实施使你能够从联系科获得贵座档案中的价值。

<object type="Spring.Objects.Factory.Config.VariablePlaceholderConfigurer, Spring.Core">
  <property name="VariableSources">
    <list>
      <object type="Spring.Objects.Factory.Config.ConnectionStringsVariableSource, Spring.Core"/>
    </list>
  </property>
</object>

<object name="myService" type="com.acme.MyService, com.acme">
    <constructor-arg type="System.String" value="${myConnectionName.connectionString}"/>
</object>

Example from the Spring.NET forum: http://forum.springframework.net/showthread.php?3961





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签