我有一个需求,要求我使用Spring.net获取一个存储在app.config中的connectionstring,然后将检索到的connectionstring注入到实例化的对象中。
如何使用Spring.net的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[..]
从Spring.net xml配置?
过去我使用一个表达式来实现这一点,但是通过这个问题和bbaia的回答,我发现更好的方法是使用VariablePlaceholderConfigurer
。当你使用VariablePlaceholderConfigurer
而不是我的"expression-hack"时,你不会把自己束缚在变量的appSettings / connectionStrings
风格配置上:你可以切换到spring.net提供的VariableSources
之一,甚至实现你自己的IVariableSource
。
开箱即用的,春天。.NET提供了VariablePlaceholderConfigurer
来从标准。NET设置(如AppSettings
、ConnectionStrings
、UserSettings
和ApplicationSettings
)中检索变量。bbaia的回答部分说明了这一点,您将在下面找到一个完整的示例。
"Expression hack":从xml config中调用ConfigurationManager
所以,我不建议你使用这个,但这是我过去使用的hack,应用到你的配置:
<object object name="myService" type="com.acme.MyService, com.acme">
<constructor-arg name="Connection"
expression="T(System.Configuration.ConfigurationManager).ConnectionStrings['myConnectionName']" />
</object>
您可以对ConfigurationManager.AppSettings
使用相同的方法,例如:
<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设置。. NET xml config
你可以很容易地配置VariablePlaceholderConfigurer
来从标准的。net设置中检索变量,如AppSettings
, ConnectionStrings
, UserSettings
和ApplicationSettings
。例如,考虑以下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>
另一个解决方案是使用VariablePlaceholderConfigurer:http://www.springframework.net/doc-latest/reference/html/objects.html objects-variablesource
ConnectionStringsVariableSource实现允许您从配置文件中的ConnectionString部分获取值。
<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>
来自Spring的示例。网论坛:http://forum.springframework.net/showthread.php?3961