6 Stimmen

Zugriff auf den Wert ConfigurationManager.AppSettings aus der Spring.NET xml-Konfiguration

Ich habe eine Anforderung, die erfordert, dass ich Spring.net verwenden, um eine Verbindungszeichenfolge zu erhalten, die innerhalb der app.config gespeichert ist, und dann injizieren die abgerufene Verbindungszeichenfolge zu einem instanziierten Objekt.

Wie kann ich dies mit der xml-Konfiguration von Spring.net tun?

Zum Beispiel, anstatt meiner Codes dies zu tun:

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

Ich möchte es so:

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

Ist es überhaupt möglich, die ConfigurationManager.AppSettings[..] von Spring.net xml config?

6voto

Marijn Punkte 10067

In der Vergangenheit habe ich eine Ausdruck um dies zu erreichen, aber durch diese Frage und die Antwort von bbaia habe ich herausgefunden, dass eine bessere Möglichkeit darin besteht, eine VariablePlaceholderConfigurer . Wenn Sie eine VariablePlaceholderConfigurer anstelle meines "Ausdrucks-Hacks" binden Sie sich nicht an den appSettings / connectionStrings Stilkonfiguration Ihrer Variablen: Sie können zu einer der VariableSources die von spring.net zur Verfügung gestellt werden, oder implementieren Sie sogar Ihre eigenen IVariableSource .

Spring.NET bietet von Haus aus VariablePlaceholderConfigurer s zum Abrufen von Variablen aus Standard-.NET-Einstellungen wie AppSettings , ConnectionStrings , UserSettings y ApplicationSettings . Dies wird teilweise durch die Antwort von bbaia veranschaulicht, und ein vollständiges Beispiel finden Sie weiter unten.

"Expression hack": Aufruf ConfigurationManager von xml config

Ich rate Ihnen also nicht, dies zu verwenden, aber dies ist der Hack, den ich in der Vergangenheit verwendet habe, angewandt auf Ihre Konfiguration:

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

Diesen Ansatz können Sie auch anwenden für ConfigurationManager.AppSettings , z.B.:

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

VariablePlaceholderConfigurer Referenz .NET-Einstellungen aus Spring.NET xml config

Sie können leicht eine VariablePlaceholderConfigurer zum Abrufen von Variablen aus Standard-.NET-Einstellungen wie AppSettings , ConnectionStrings , UserSettings y ApplicationSettings . Nehmen wir zum Beispiel diese xml-Konfiguration:

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

Es liest die Einstellungen aus diesem 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>

Diese Konfigurationen sind entnommen aus dieses Arbeitsbeispiel auf github .

3voto

bbaia Punkte 1173

Eine andere Lösung ist die Verwendung des VariablePlaceholderConfigurer : http://www.springframework.net/doc-latest/reference/html/objects.html#objects-variablesource

Die ConnectionStringsVariableSource-Implementierung ermöglicht es Ihnen, den Wert aus dem Abschnitt ConnectionString in Ihrer Konfigurationsdatei zu erhalten.

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

Beispiel aus dem Spring.NET-Forum: http://forum.springframework.net/showthread.php?3961

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X