ConfigurationManager API

<span style="font-size:24px;">class ConfigurationManager implements Cloneable
本类的作用:是管理一系列的configurable,包括configurable的属性参数,configurable之间的关系。Configurable可以通过xml文件或编码确定。
本类的属性:
1,Map<String, PropertySheet> symbolTable:存储了所有的propertysheet,即转变为configurable所需的信息。可以说是configurable表,存储了所有的configurable。
2,Map<String, RawPropertyData> rawPropertyMap:存储了所有的rawproperty,即原始的component信息。
3,Map<String, String> globalProperties:存储了xml中的globalProperties信息。
4,URL configURL  :配置文件存储路径
5,showCreations  :是否显示创建信息。
6,List<ConfigurationChangeListener> changeListeners:configurable配置(属性)改变的监听器。
本类的构造方法:
本类的构造方法完成后,只是初始化了(对其属性赋值)configURL,globalProperties,rawPropertyMap,showCreations属性。symbolTable没有进行初始化(没有赋值)。设置了系统配置logger。有一个空的构造方法,将什么也不进行初始化,用于动态的创建configurable。
本类一般方法:。
1,	PropertySheet getPropertySheet(String instanceName):作用是通过component的name获得propertysheet,本方法查看symbolTable是否存在与instancename相对应得propertysheet,有则返回此propertysheet,没有就通过rawpropertyMap获得propertysheet,然后把instancename和propertysheet存入symbolTable中,并返回propertysheet。
2,	Collection<String> getInstanceNames(Class<? extends Configurable> type):通过symbolTable根据configurable的Class对象名获得实例名(为component中的name值)集合。返回的为symbolTable中所有的component的name值集合。
3,	Set<String> getComponentNames():获得所有的component的name的集合。
4,	Configurable lookup(String instanceName):通过instancename,通过getPropertySheet方法获得ps,通过ps获得其owner即configurable。
5,	<C extends Configurable> C lookup(Class<C> confClass):本法通过getPropertySheet来获得propertysheet列表,获得此列表中的第一个propertysheet,并转换为输入的类型进行输出。
6,	List<PropertySheet> getPropSheets(Class<? extends Configurable> confClass):本方法获得symbolTable中的所有propertysheet,并放入列表中输出。
7,	addConfigurable (Class<? extends Configurable> confClass, String name, Map<String, Object> props):添加一个component的信息入symbolTable,rawPropertyMap,并添加监听器为此加入configurable。如果要添加的原本在symbolTable中存在则会发生异常。
8,	addConfigurable(Configurable configurable, String name):与7一样只是输入参数不同。
9,	renameConfigurable(String oldName, String newName):改变的是symbolTable,rawPropertyMap中的configurable的对应的名字,其监听改变成对新名字的监听。
10,	removeConfigurable(String name):symbolTable,rawPropertyMap移除与name相对应的configurable(component)信息,并移除相应的监听器。
addSubConfiguration(ConfigurationManager subCM, boolean doOverrideComponents):本方法输入的ConfigurationManager(必须是non-instantiated)中的中的symbolTable,rawPropertyMap,globalProperties中的内容放入本ConfigurationManager的相应属性中。
11,	Map<String, String> getGlobalProperties():获得CM中的globalProperties的所有内容。即获得CM的globalProperties。
12,	String getGlobalProperty(String propertyName):根据propertyName,获得某一确定的GlobalProperty。Configurable属性。
13,	String getGloPropReference(String propertyName):返回的为globalProperties.get(propertyName),即与getGlobalProperty一样。
14,	URL getConfigURL():若通过xml配置的,返回其URL路径,动态配置返回为null。
15,	setGlobalProperty(String propertyName, String value):若输入value为null,则移除此GlobalProperty,否则就设置把相应的propertyName的value设置成此值。并且对使用了此GlobalProperty的所有propertysheet和configurable进行更新。
16,	String getStrippedComponentName(String propertyName):返回的是globalProperty的value值,作用是某些component中使用了globalProperty的值是以${}形式,本方法是用globalProperty的value来代替${}形式。
17,addConfigurationChangeListener(ConfigurationChangeListener l):为CM添加监听器。
18,removeConfigurationChangeListener(ConfigurationChangeListener l):移除监听器。
19,void fireConfChanged(String configurableName, String propertyName):{
        assert getComponentNames().contains(configurableName);
        for (ConfigurationChangeListener changeListener : changeListeners)
            changeListener.configurationChanged(configurableName, propertyName, this);
    }
20,void fireRenamedConfigurable(String oldName, String newName) {
        assert getComponentNames().contains(newName);
        for (ConfigurationChangeListener changeListener : changeListeners) {
            changeListener.componentRenamed(this, getPropertySheet(newName), oldName);
        }
    }
21,ConfigurationManager clone():把本CM的changeListeners,symbolTable,globalProperties,rawPropertyMap都放入进新的CM中。
22,static <C extends Configurable> C getInstance(Class<C> targetClass):本方法得到一个targetClass的Class对象的类或接口。
23,static <C extends Configurable> C getInstance(Class<C> targetClass, Map<String, Object> props):与22一样。
24,static <C extends Configurable> C getInstance(Class<C> targetClass, Map<String, Object> props, String compName):与22一样。
25,static PropertySheet getPropSheetInstanceFromClass(Class<? extends Configurable> targetClass, Map<String, Object> defaultProps, String componentName, ConfigurationManager cm):本方法返回new PropertySheet(targetClass, componentName, cm, rpd)。即根据Class对象,component名字,CM,得到一个,defaultprops都放入了rpd中。
26,Logger getRootLogger():通得根logger。
</span>

### 回答1: ConfigurationManager.AppSettings是一个.NET Framework中的类,用于读取和写入应用程序的配置文件中的键/值对。它允许开发人员在应用程序中存储和检索配置信息,例如数据库连接字符串、API密钥等。这个类可以在应用程序的任何地方使用,包括代码中和配置文件中。 ### 回答2: configurationmanager.appsettings是一个.NET Framework中提供的类,它可以用于读取应用程序的配置文件中的设置。在.NET Framework中配置文件是一种XML格式的文本文件,其中包含应用程序的设置信息,如数据库连接字符串、用户名、密码、邮件服务器、日志等等。 这个类提供了两种方法来读取配置文件的值:GetSection和AppSettings,其中AppSettings是最常用的方法。 使用AppSettings方法读取配置文件的值很简单,只需在应用程序代码中加入以下代码: string value = ConfigurationManager.AppSettings[“key”]; 其中key是配置文件中的配置项的键名,value是配置项对应的值。如果要读取多个配置项,可以使用下面的代码: foreach (string key in ConfigurationManager.AppSettings.Keys) { string value = ConfigurationManager.AppSettings[key]; Console.WriteLine(key + ": " + value); } 读取配置文件中的值之前,要先确保配置文件已经被正确的设置。在Visual Studio中创建一个.NET Framework应用程序时,应用程序会有一个默认的App.config文件,我们就可以在这个文件中设置应用程序的配置。如下图所示: ![](https://cdn.nlark.com/yuque/0/2021/png/221981/1632274878446-55a307f5-7ccb-41d8-9f0f-209e3fa6f2d2.png) 在这个文件中,我们可以设置一些键值对,如以下示例代码: <?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings> <add key="ServerName" value="MyServer"/> <add key="DatabaseName" value="MyDatabase"/> <add key="UserName" value="MyUserName"/> <add key="Password" value="MyPassword"/> </appSettings> </configuration> 上面的代码定义了四个配置项,分别是ServerName、DatabaseName、UserName、Password。这些配置项的值可以在应用程序代码中通过configurationmanager.appsettings类获取,如下所示: string serverName = ConfigurationManager.AppSettings["ServerName"]; string databaseName = ConfigurationManager.AppSettings["DatabaseName"]; string userName = ConfigurationManager.AppSettings["UserName"]; string password = ConfigurationManager.AppSettings["Password"]; 最后需要注意的是,configurationmanager.appsettings类只能读取配置文件中的设置值,不能修改它们。要修改配置文件中的值,需要手动修改配置文件。 ### 回答3: Configuration Manager是.net框架中常用的一个类,用于读取和操作应用程序或者服务的配置文件。其中,appsettings是Configuration Manager中的一个重要成员,主要用于获取和管理应用程序设定值。 在.net框架中,应用程序的配置文件通常存储在XML格式的文件中,包含了许多应用程序的信息,比如数据库连接字符串、Web服务路径、日志记录级别等等。这些信息可以通过Configuration Manager进行读取和设置。 ConfigurationManager.AppSettings就是通过Configuration Manager来获取应用程序设定值的方法之一,通常用于获取app.config或者web.config文件中的键值对,其中键表示设定值的名称,而值则表示设定值本身。通过appsettings,我们可以更加直观地管理应用程序的设定值,方便在应用程序中访问和使用。 通常,我们可以将常见的应用程序设定值存储在app.config或者web.config文件中,比如数据库连接字符串、SMTP服务器地址和端口号等等。这些设定值可以通过Configuration Manager进行读取和管理,从而让我们的应用程序更加灵活和适应不同的环境。 举个例子,假设我们的应用程序需要连接数据库,那么我们可以将数据库连接字符串存储在app.config或者web.config文件中,并使用ConfigurationManager.AppSettings来读取该设定值。这样,当生产环境和测试环境中的数据库连接字符串不同的时候,我们只需要在相应的配置文件中修改该设定值,而不需要对应用程序的源代码进行修改。这样,代码的维护和部署就更加方便了。 总之,ConfigurationManager.AppSettings是一个非常重要的类,在应用程序中使用非常广泛。通过appsettings,我们能够更加方便、灵活地管理应用程序的设定值,为应用程序的开发、维护和部署带来了极大的便利。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值