如何配置 appconfig从config里载入配置

你的浏览器禁用了JavaScript, 请开启后刷新浏览器获得更好的体验!
rt,把config文件copy出来放在app平级的根目录下,试了下配置也没用,求大神们解答
默认情况下, Lumen 使用单一的 .env 文件来配置你的应用, 然而, 你也可以使用 Laravel 风格 的配置方法.
只需要把 vendor/laravel/lumen/config 文件夹下对应的配置文件复制到根目录下的 config 文件里面就行.
使用这种方法, 能有更多的配置灵活度.
自定义配置文件
你可以创建自定义的配置文件, 并使用 $app->configure() 方法进行加载.
举个栗子, 把自定义配置文件放置于 config/options.php, 可以这样加载:
$app->configure('options');
----------------------
-: 如果你需要使用 .env 来管理你的配置信息的话, 请在 bootstrap/app.php 文件里面把这一行去掉注释 Dotenv::load().
use Illuminate\Support\Facades\C
like: Config::get('config.bucket_key.2')
要回复问题请先或
浏览: 9089
关注: 3 人博客访问: 428573
博文数量: 81
博客积分: 2689
博客等级: 少校
技术积分: 897
注册时间:
一个有目标,为自己的未来努力奋斗的人
IT168企业级官微
微信号:IT168qiye
系统架构师大会
微信号:SACC2013
分类: Python/Ruby
python读写配置文件还是比较方便得。
1) 基本的读取配置文件
& & &-read(filename) 直接读取ini文件内容
& & &-sections() 得到所有的section,并以列表的形式返回
& & &-options(section) 得到该section的所有option
& & &-items(section) 得到该section的所有键值对
& & &-get(section,option) 得到section中option的值,返回为string类型
& & &-getint(section,option) 得到section中option的值,返回为int类型,还有相应的getboolean()和getfloat() 函数。
2) 基本的写入配置文件
& & &-add_section(section) 添加一个新的section
& & &-set( section, option, value) 对section中的option进行设置,需要调用write将内容写入配置文件。
配置文件如下:
db_port = 3306
db_user = root
db_host = 127.0.0.1
db_pass = xgmtest
[concurrent]
processor = 20
thread = 10
示例代码如下
#/usr/bin/python
import ConfigParser
import string, os, sys
cf = ConfigParser.ConfigParser()
cf.read("test.conf")
#return all section
secs = cf.sections()
print 'sections:', secs
opts = cf.options("db")
print 'options:', opts
kvs = cf.items("db")
print 'db:', kvs
#read by type
db_host = cf.get("db", "db_host")
db_port = cf.getint("db", "db_port")
db_user = cf.get("db", "db_user")
db_pass = cf.get("db", "db_pass")
threads = cf.getint("concurrent", "thread")
processors = cf.getint("concurrent", "processor")
print "db_host:", db_host
print "db_port:", db_port
print "db_user:", db_user
print "db_pass:", db_pass
print "thread:", threads
print "processor:", processors
#modify one value and write to file
cf.set("db", "db_pass", "xgmtest")
cf.write(open("test.conf", "w"))
3) Python的ConfigParser Module 中定义了3个类对INI文件进行操作。分别是RawConfigParser、ConfigParser、SafeConfigParser。 RawCnfigParser是最基础的INI文件读取类,ConfigParser、SafeConfigParser支持对%(value)s变量的 解析。
设定配置文件test2.conf
url = http://%(host)s:%(port)s/Portal
host = localhost
port = 8080
使用RawConfigParser:
import ConfigParser
cf = ConfigParser.RawConfigParser()
print "use RawConfigParser() read"
cf.read("test2.conf")
print cf.get("portal", "url")
print "use RawConfigParser() write"
cf.set("portal", "url2", "%(host)s:%(port)s")
print cf.get("portal", "url2")
得到终端输出:
use RawConfigParser() read
http://%(host)s:%(port)s/Portal
use RawConfigParser() write
%(host)s:%(port)s
改用ConfigParser:
import ConfigParser
cf = ConfigParser.ConfigParser()
print "use ConfigParser() read"
cf.read("test2.conf")
print cf.get("portal", "url")
print "use ConfigParser() write"
cf.set("portal", "url2", "%(host)s:%(port)s")
print cf.get("portal", "url2")
得到终端输出:
use ConfigParser() read
http://localhost:8080/Portal
use ConfigParser() write
localhost:8080
改用SafeConfigParser:
import ConfigParser
cf = ConfigParser.SafeConfigParser()
print "use SafeConfigParser() read"
cf.read("test2.conf")
print cf.get("portal", "url")
print "use SateConfigParser() write"
cf.set("portal", "url2", "%(host)s:%(port)s")
print cf.get("portal", "url2")
得到终端输出(效果同ConfigParser):
use SafeConfigParser() read
http://localhost:8080/Portal
use SateConfigParser() write
localhost:8080
阅读(34082) | 评论(0) | 转发(1) |
相关热门文章
给主人留下些什么吧!~~
请登录后评论。博客分类:
老外写的一段代码,在Server中编写这个类读取配置文件比较实用
//Config.h
#pragma once
#include &string&
#include &map&
#include &iostream&
#include &fstream&
#include &sstream&
* \brief Generic configuration Class
class Config {
protected:
std::string m_D
//!& separator between key and value
std::string m_C
//!& separator between value and comments
std::map&std::string,std::string& m_C
//!& extracted keys and values
typedef std::map&std::string,std::string&::
typedef std::map&std::string,std::string&::const_
// Methods
Config( std::string filename,std::string delimiter = "=",std::string comment = "#" );
template&class T& T Read( const std::string& in_key )
//!&Search for key and read value or optional default value, call as read&T&
template&class T& T Read( const std::string& in_key, const T& in_value )
template&class T& bool ReadInto( T& out_var, const std::string& in_key )
template&class T&
bool ReadInto( T& out_var, const std::string& in_key, const T& in_value )
bool FileExist(std::string filename);
void ReadFile(std::string filename,std::string delimiter = "=",std::string comment = "#" );
// Check whether key exists in configuration
bool KeyExists( const std::string& in_key )
// Modify keys and values
template&class T& void Add( const std::string& in_key, const T& in_value );
void Remove( const std::string& in_key );
// Check or change configuration syntax
std::string GetDelimiter() const { return m_D }
std::string GetComment() const { return m_C }
std::string SetDelimiter( const std::string& in_s )
{ std::string old = m_D
m_Delimiter = in_s; }
std::string SetComment( const std::string& in_s )
{ std::string old = m_C
m_Comment =
// Write or read configuration
friend std::ostream& operator&&( std::ostream& os, const Config& cf );
friend std::istream& operator&&( std::istream& is, Config& cf );
protected:
template&class T& static std::string T_as_string( const T& t );
template&class T& static T string_as_T( const std::string& s );
static void Trim( std::string& inout_s );
// Exception types
struct File_not_found {
File_not_found( const std::string& filename_ = std::string() )
: filename(filename_) {} };
struct Key_not_found {
// thrown only by T read(key) variant of read()
Key_not_found( const std::string& key_ = std::string() )
: key(key_) {} };
/* static */
template&class T&
std::string Config::T_as_string( const T& t )
// Convert from a T to a string
// Type T must support && operator
return ost.str();
/* static */
template&class T&
T Config::string_as_T( const std::string& s )
// Convert from a string to a T
// Type T must support && operator
std::istringstream ist(s);
/* static */
template&&
inline std::string Config::string_as_T&std::string&( const std::string& s )
// Convert from a string to a string
// In other words, do nothing
/* static */
template&&
inline bool Config::string_as_T&bool&( const std::string& s )
// Convert from a string to a bool
// Interpret "false", "F", "no", "n", "0" as false
// Interpret "true", "T", "yes", "y", "1", "-1", or anything else as true
std::string sup =
for( std::string::iterator p = sup.begin(); p != sup.end(); ++p )
*p = toupper(*p);
// make string all caps
if( sup==std::string("FALSE") || sup==std::string("F") ||
sup==std::string("NO") || sup==std::string("N") ||
sup==std::string("0") || sup==std::string("NONE") )
template&class T&
T Config::Read( const std::string& key ) const
// Read the value corresponding to key
mapci p = m_Contents.find(key);
if( p == m_Contents.end() ) throw Key_not_found(key);
return string_as_T&T&( p-&second );
template&class T&
T Config::Read( const std::string& key, const T& value ) const
// Return the value corresponding to key or given default value
// if key is not found
mapci p = m_Contents.find(key);
if( p == m_Contents.end() )
return string_as_T&T&( p-&second );
template&class T&
bool Config::ReadInto( T& var, const std::string& key ) const
// Get the value corresponding to key and store in var
// Return true if key is found
// Otherwise leave var untouched
mapci p = m_Contents.find(key);
bool found = ( p != m_Contents.end() );
if( found ) var = string_as_T&T&( p-&second );
template&class T&
bool Config::ReadInto( T& var, const std::string& key, const T& value ) const
// Get the value corresponding to key and store in var
// Return true if key is found
// Otherwise set var to given default
mapci p = m_Contents.find(key);
bool found = ( p != m_Contents.end() );
if( found )
var = string_as_T&T&( p-&second );
template&class T&
void Config::Add( const std::string& in_key, const T& value )
// Add a key with given value
std::string v = T_as_string( value );
std::string key=in_
trim(key);
m_Contents[key] =
// Config.cpp
#include "Config.h"
Config::Config( string filename, string delimiter,
string comment )
: m_Delimiter(delimiter), m_Comment(comment)
// Construct a Config, getting keys and values from given file
std::ifstream in( filename.c_str() );
if( !in ) throw File_not_found( filename );
in && (*this);
Config::Config()
: m_Delimiter( string(1,'=') ), m_Comment( string(1,'#') )
// Construct a C empty
bool Config::KeyExists( const string& key ) const
// Indicate whether key is found
mapci p = m_Contents.find( key );
return ( p != m_Contents.end() );
/* static */
void Config::Trim( string& inout_s )
// Remove leading and trailing whitespace
static const char whitespace[] = " \n\t\v\r\f";
inout_s.erase( 0, inout_s.find_first_not_of(whitespace) );
inout_s.erase( inout_s.find_last_not_of(whitespace) + 1U );
std::ostream& operator&&( std::ostream& os, const Config& cf )
// Save a Config to os
for( Config::mapci p = cf.m_Contents.begin();
p != cf.m_Contents.end();
os && p-&first && " " && cf.m_Delimiter && " ";
os && p-&second && std::
void Config::Remove( const string& key )
// Remove key and its value
m_Contents.erase( m_Contents.find( key ) );
std::istream& operator&&( std::istream& is, Config& cf )
// Load a Config from is
// Read in keys and values, keeping internal whitespace
typedef string::size_
const string& delim
// separator
const string& comm
// comment
const pos skip = delim.length();
// length of separator
string nextline = "";
// might need to read ahead to see where value ends
while( is || nextline.length() & 0 )
// Read an entire line at a time
if( nextline.length() & 0 )
// use it now
nextline = "";
std::getline( is, line );
// Ignore comments
line = line.substr( 0, line.find(comm) );
// Parse the line if it contains a delimiter
pos delimPos = line.find( delim );
if( delimPos & string::npos )
// Extract the key
string key = line.substr( 0, delimPos );
line.replace( 0, delimPos+skip, "" );
// See if value continues on the next line
// Stop at blank line, next line with a key, end of stream,
// or end of file sentry
bool terminate =
while( !terminate && is )
std::getline( is, nextline );
terminate =
string nlcopy =
Config::Trim(nlcopy);
if( nlcopy == "" )
nextline = nextline.substr( 0, nextline.find(comm) );
if( nextline.find(delim) != string::npos )
Config::Trim(nlcopy);
if( nlcopy != "" ) line += "\n";
terminate =
// Store key and value
Config::Trim(key);
Config::Trim(line);
cf.m_Contents[key] =
// overwrites if key is repeated
bool Config::FileExist(std::string filename)
bool exist=
std::ifstream in( filename.c_str() );
void Config::ReadFile( string filename, string delimiter,
string comment )
m_Delimiter =
m_Comment =
std::ifstream in( filename.c_str() );
if( !in ) throw File_not_found( filename );
in && (*this);
//main.cpp
#include "Config.h"
int main()
std::string ipA
const char ConfigFile[]= "config.txt";
Config configSettings(ConfigFile);
port = configSettings.Read("port", 0);
ipAddress = configSettings.Read("ipAddress", ipAddress);
username = configSettings.Read("username", username);
password = configSettings.Read("password", password);
std::cout&&"port:"&&port&&std::
std::cout&&"ipAddress:"&&ipAddress&&std::
std::cout&&"username:"&&username&&std::
std::cout&&"password:"&&password&&std::
config.txt的文件内容:
ipAddress=10.10.90.125
port=3001
username=mark
password=2d2df5a
编译运行输出:
port:3001
ipAddress:10.10.90.125
username:mark
password:2d2df5a
这个类还有很多其他的方法,可以调用试试。
浏览 15806
& 这代码你自己有测试么?
引用自哪里也没有说!
测过
mylove2060
浏览: 192595 次
来自: 杭州
navylq 写道
这代码你自己有测试么?
引用自哪里也没有 ...
这代码你自己有测试么?引用自哪里也没有说!
&div class=&quote_title ...
出现了乱码,怎么弄啊
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'欢迎加入我们,一同切磋技术。 &
用户名: &&&
密 码: &
共有 4497 人关注过本帖
标题:如何动态读取刚更改的app.config里的配置项的值?
等 级:新手上路
帖 子:147
&&问题点数:0&&回复次数:8&&&
如何动态读取刚更改的app.config里的配置项的值?
我在程序里用app.config配置项来记录收费日期,更改了后,再马上读取就不行,要重启程序才能行。
通过查阅资料,我加了ConfigurationManager.RefreshSection(&appSettings&);来刷新。但还是不行。请问如何解决呢?
代码如下:
&&&&&&&&&&&&ConfigurationManager.RefreshSection(&appSettings&);
&&&&&&&&&&&&//
&&&&&&&&&&&&int oneyear = Convert.ToInt32(ConfigManager.GetValue(&one_year&));
&&&&&&&&&&&&int onemonth = Convert.ToInt32(ConfigManager.GetValue(&one_month&));
&&&&&&&&&&&&this.numericUpDown1.Value =
&&&&&&&&&&&&this.numericUpDown2.Value =
搜索更多相关主题的帖子:
等 级:新手上路
帖 子:147
有没有好办法
等 级:新手上路
帖 子:928
这个好像不能动态修改吧,你可以把这些配置写到其他文件中
public class 人生历程 extends Thread{public void run(){while(true){努力,努力,再努力!!;Thread.sleep(0);}}}
等 级:新手上路
帖 子:147
C#中动态读写App.config配置文件
日期: 18:14:59 作者:山人 人气:     来源:网络
--------------------------------------------------------------------------------
using System.Collections.G
using System.T
using System.IO;
using System.X
mon.TaoCommon
/// C#中动态读写App.config配置文件
public class AppConfig
public AppConfig()
/// TODO: 在此处添加构造函数逻辑
/// 写操作
public static void ConfigSetValue(string strExecutablePath,string AppKey, string AppValue)
XmlDocument xDoc = new XmlDocument();
//获取可执行文件的路径和名称
xDoc.Load(strExecutablePath + &.config&);
XmlNode xN
XmlElement xElem1;
XmlElement xElem2;
xNode = xDoc.SelectSingleNode(&//connectionStrings&);
// xDoc.Load(System.Windows.Forms.Application.ExecutablePath + &.config&);
xElem1 = (XmlElement)xNode.SelectSingleNode(&//add[@name='& + AppKey + &']&);
if (xElem1 != null) xElem1.SetAttribute(&connectionString&, AppValue);
xElem2 = xDoc.CreateElement(&add&);
xElem2.SetAttribute(&name&, AppKey);
xElem2.SetAttribute(&connectionString&, AppValue);
xNode.AppendChild(xElem2);
xDoc.Save(strExecutablePath + &.config&);
/// 读操作
public string ConfigGetValue(string strExecutablePath, string appKey)
XmlDocument xDoc = new XmlDocument();
xDoc.Load(strExecutablePath + &.config&);
XmlNode xN
XmlElement xE
xNode = xDoc.SelectSingleNode(&//appSettings&);
xElem = (XmlElement)xNode.SelectSingleNode(&//add[@key='& + appKey + &']&);
if (xElem != null)
return xElem.GetAttribute(&value&);
return &&;
catch (Exception)
return &&;
这是我找到的资料,为什么不行呢,调用时找不到ConfigGetValue这个函数。
如果行的话,如何调用,参数怎么写?
等 级:新手上路
帖 子:147
我的设计目的是这样的,因为有个收费日期,不想让用户每次都选,所以要存储一下这个“年”和“月”,如“2007”和“9”这两个数。下次,用户点按钮直接读取就行了。除了app.config这个方法外,大家还有没有其他的方法都好些。
我用的是sqlserver,不想直接存在远程数据中,最好在本地就行了。
等 级:新手上路
帖 子:147
等 级:新手上路
帖 子:163
我也有同感呀
只能第一次管用
在更改了就不行了
楼主解决了没,共享下
我还在编程路上转悠,偶而看到一两盏灯不是为我而亮
等 级:新手上路
帖 子:147
解决了,就是上面的代码,只是在实例化一下,再调用就行了。现在没有问题了。
等 级:新手上路
帖 子:163
是哪个代码呀楼主
ConfigurationManager
这个我怎么不能引用呢。
我还在编程路上转悠,偶而看到一两盏灯不是为我而亮
版权所有,并保留所有权利。
Powered by , Processed in 0.060552 second(s), 7 queries.
Copyright&, BCCN.NET, All Rights Reserved博客分类:
因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装入系统,这就需要利用Spring去动态加载某一位置下的配置文件,所以就总结了下Spring中加载xml配置文件的方式,我总结的有6种, xml是最常见的spring 应用系统配置源。Spring中的几种容器都支持使用xml装配bean,包括:
XmlBeanFactory,ClassPathXmlApplicationContext,FileSystemXmlApplicationContext,XmlWebApplicationContext
一: XmlBeanFactory 引用资源
Resource resource = new ClassPathResource("appcontext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
二: ClassPathXmlApplicationContext& 编译路径
ApplicationContext factory=new ClassPathXmlApplicationContext("classpath:appcontext.xml");
// src目录下的
ApplicationContext factory=new ClassPathXmlApplicationContext("appcontext.xml");
ApplicationContext factory=new ClassPathXmlApplicationContext(new String[] {"bean1.xml","bean2.xml"});
// src/conf 目录下的
ApplicationContext factory=new ClassPathXmlApplicationContext("conf/appcontext.xml");
ApplicationContext factory=new ClassPathXmlApplicationContext("file:G:/Test/src/appcontext.xml");
三: 用文件系统的路径
ApplicationContext factory=new FileSystemXmlApplicationContext("src/appcontext.xml");
//使用了& classpath:& 前缀,作为标志,& 这样,FileSystemXmlApplicationContext 也能够读入classpath下的相对路径
ApplicationContext factory=new FileSystemXmlApplicationContext("classpath:appcontext.xml");
ApplicationContext factory=new FileSystemXmlApplicationContext("file:G:/Test/src/appcontext.xml");
ApplicationContext factory=new FileSystemXmlApplicationContext("G:/Test/src/appcontext.xml");
四: XmlWebApplicationContext是专为Web工程定制的。
ServletContext servletContext = request.getSession().getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext );
五: 使用BeanFactory
BeanDefinitionRegistry reg = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(reg);
reader.loadBeanDefinitions(new ClassPathResource("bean1.xml"));
reader.loadBeanDefinitions(new ClassPathResource("bean2.xml"));
BeanFactory bf=(BeanFactory)
六:Web 应用启动时加载多个配置文件
通过ContextLoaderListener 也可加载多个配置文件,在web.xml文件中利用
&context-pararn&元素来指定多个配置文件位置,其配置如下:
&context-param&
&!-- Context Configuration locations for Spring XML files --&
&param-name&contextConfigLocation&/param-name&
&param-value&
./WEB-INF/**/Appserver-resources.xml,
classpath:config/aer/aerContext.xml,
classpath:org/codehaus/xfire/spring/xfire.xml,
./WEB-INF/**/*.spring.xml
&/param-value&
&/context-param&
这个方法加载配置文件的前提是已经知道配置文件在哪里,虽然可以利用“*”通配符,但灵活度有限。
文章地址:
浏览 34115
浏览: 2296690 次
来自: 北京
但是 java.util.ArrayList 实现了 remo ...
1.因为 Arrays.asList 返回的是 Arrays内 ...
用java8新特性String testStr = &quot ...
我之前所在的项目就是日本一家证券公司的项目。完全使用的是j2e ...
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'}

我要回帖

更多关于 如何配置 appconfig 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信