配置节点简单使用

在web开发中一般我们读取web.config里面的信息,都是通过
WebConfigurationManager.AppSettings
WebConfigurationManager.ConnectionStrings
或者时候1.1里面老的方式实现IConfigurationSection,其实都无所谓了。
这里主要讨论一下使用ConfigurationSection抽象类的方式,定义配置节,可以实现多配置节,其实和IConfigurationSection一样,没感觉有什么很大区别。做个笔记而已了
首先web.config
1 < configSections >
2    < section  name ="mqtest"  type ="test, __code" />
3 </ configSections >
4 < mqtest >
5    < aaForm  asdfa ="10" ></ aaForm >
6 </ mqtest >
调用
 1
 2 /**/ /// <summary>
 3/// 配置节的访问
 4/// </summary>

 5 public   class  test : ConfigurationSection
 6 {
 7    //元素
 8    [ConfigurationProperty("aaForm", IsRequired = true)]
 9    public aaFormElement aaForm
10    {
11        get return (aaFormElement)base["aaForm"]; }
12    }

13
14    [ConfigurationProperty("time")]
15    public DateTime Time
16    {
17        get return (DateTime)this["time"]; }
18        set this["time"= value; }
19    }

20
21    [ConfigurationProperty("tt")]
22    public int tt
23    {
24        get return (int)this["tt"]; }
25        set this["tt"= value; }
26    }

27
28    public override bool IsReadOnly()
29    {
30        return false;
31    }

32}

33
34 /**/ ///配置节文件中的配置元素
35 public   class  aaFormElement : ConfigurationElement
36 {
37
38    [ConfigurationProperty("asdfa", DefaultValue = "10")]
39    public string aaaa
40    {
41        get return (string)base["asdfa"]; }
42        set base["asdfa"= value; }
43    }

44}

45
页面访问
test Settings1  =  (test)WebConfigurationManager.GetSection( " mqtest " );
Settings1.tt 
=   100 ;

==============直接取属性
< system .web >
     
< httpRuntime  maxRequestLength ="4096"  executionTimeout ="120"   ></ httpRuntime >
</ system.web >
Configuration config  =  WebConfigurationManager.OpenWebConfiguration( this .Request.ApplicationPath);
 HttpRuntimeSection myHttpRuntimeSection 
=  (HttpRuntimeSection)config.GetSection( " system.web/httpRuntime " );



demo

转载于:https://www.cnblogs.com/ant520/archive/2007/11/19/964434.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Java API读取Consul配置节点的数据,您可以按照以下步骤: 1. 创建Consul客户端:您需要使用Consul.Builder类创建Consul客户端。您可以指定Consul服务器的IP地址,端口号等信息。 ```java Consul client = Consul.builder() .withUrl("http://localhost:8500") .build(); ``` 2. 读取配置节点的数据:您需要使用KeyValueClient类获取Consul配置节点的数据。例如,要获取名为“my-config”的配置节点的值,您可以使用以下代码: ```java KeyValueClient keyValueClient = client.keyValueClient(); String value = keyValueClient.getValueAsString("my-config").get(); System.out.println("Value of my-config: " + value); ``` 这将返回名为“my-config”的配置节点的值。 3. 检查节点是否存在:在读取节点的值之前,您可能需要检查该节点是否存在。您可以使用以下代码检查节点是否存在: ```java boolean isExist = keyValueClient.getKeys("my-config").getResponse().contains("my-config"); if (isExist) { String value = keyValueClient.getValueAsString("my-config").get(); System.out.println("Value of my-config: " + value); } else { System.out.println("Node my-config does not exist!"); } ``` 这将检查名为“my-config”的节点是否存在。如果存在,则返回该节点的值,否则打印出“Node my-config does not exist!”的信息。 这是一个简单的示例,您可以使用Consul API读取更多配置节点的数据,例如获取多个节点的值等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值