从Spring.NET XML配置访问ConfigurationManager.AppSettings值 [英] Accessing ConfigurationManager.AppSettings value from Spring.NET xml configuration

查看:248
本文介绍了从Spring.NET XML配置访问ConfigurationManager.AppSettings值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有要求我使用Spring.net获取存储在app.config内的ConnectionString,然后注入检索到的connectionString一个实例化对象。要求

I have a requirement that requires me to use Spring.net to get a connectionstring that is stored inside the app.config, and then inject the retrieved connectionstring to a instantiated object.

我怎样才能做到这一点使用Spring.net的XML配置?

How can I do this using Spring.net's xml configuration?

有关例如,我的$ C $代替CS这样做的:

For e.g., instead of my codes doing this:

// 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
    }
}

我希望它是这样的:

I want it like this:

 // 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 [..] 从Spring.net XML配置?

Is it even possible to call ConfigurationManager.AppSettings[..] from Spring.net xml config?

推荐答案

在我使用的的 EX pression 以实现这一目标,但是通过这个问题,并bbaia的回答,我发现了一个更好的办法来做到这一点是使用 VariablePlaceholderConfigurer 。当您使用 VariablePlaceholderConfigurer ,而不是我恩pression下锅,你不配合自己的的appSettings /的ConnectionStrings 的变量的风格配置:您可以切换到的 VariableSources 通过spring.net甚至提供实现自己的 IVariableSource

In the past I used an expression to achieve this, but through this question and bbaia's answer, I found out that a better way to do this is using a VariablePlaceholderConfigurer. When you use a VariablePlaceholderConfigurer instead of my "expression-hack", you don't tie yourself to the appSettings / connectionStrings style configuration of your variables: you can switch to one of the VariableSources provided by spring.net or even implement your own IVariableSource.

外的开箱即用,Spring.NET提供了 VariablePlaceholderConfigurer s到检索标准的.NET设置变量,如的AppSettings 的ConnectionStrings UserSettings 的applicationSettings 。这部分是由bbaia的回答说明,你会发现下面一个完整的例子。

Out-of-the-box, Spring.NET provides VariablePlaceholderConfigurers to retrieve variables from standard .NET settings like AppSettings, ConnectionStrings, UserSettings and ApplicationSettings. This is partially illustrated by bbaia's answer and you'll find a complete example below.

所以,我不建议你用这个,但是这是我在过去使用的破解,适用于您的配置:

So, I don't advice you to use this, but this is the hack I used in the past, applied to your configuration:

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

您可以使用同样的方法对 ConfigurationManager.AppSettings ,例如:

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 :从Spring.NET XML配置

引用.NET设置

您可以轻松地配置 VariablePlaceholderConfigurer 来检索像的AppSettings ,<$ C $标准的.NET设置变量C>的ConnectionStrings , UserSettings 的applicationSettings 。例如,考虑这个XML配置:

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

You can easily configure a VariablePlaceholderConfigurer to retrieve variables from standard .NET settings like AppSettings, ConnectionStrings, UserSettings and ApplicationSettings. For instance, consider this xml configuration:

<?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>

这些配置从<一个取href="https://github.com/serra/stackoverflow/tree/master/spring-questions/q7991262-accessing-configurationmanager"相对=nofollow>在GitHub上这个工作的样本。

These configurations are taken from this working sample at github.

这篇关于从Spring.NET XML配置访问ConfigurationManager.AppSettings值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆