原創(chuàng)|其它|編輯:郝浩|2009-02-09 11:11:30.000|閱讀 1704 次
概述:在程序開發(fā)中將一些參數(shù)配置文件放在外部YAML文件里,使應(yīng)用程序代碼不會被污染,有利于提高開發(fā)效率、增強(qiáng)代碼的可讀性。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
在程序開發(fā)中將一些參數(shù)配置文件放在外部YAML文件里,使應(yīng)用程序代碼不會被污染,有利于提高開發(fā)效率、增強(qiáng)代碼的可讀性,相關(guān)Java代碼如下:
# config/initializers/load_config.rb
APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/config.yml")[RAILS_ENV]
# application.rb
def authenticate
if APP_CONFIG['perform_authentication']
authenticate_or_request_with_http_basic do |username, password|
username == APP_CONFIG['username'] && password == APP_CONFIG['password']
end
end
end
# config/config.yml
development:
perform_authentication: false
test:
perform_authentication: false
production:
perform_authentication: true
username: admin
password: secret
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都科技