rails 程序是如何加载的

来源:互联网 发布:口口网络用语什么意思 编辑:程序博客网 时间:2024/06/01 07:39

接触rails不久,对在启动一个rails程序的时候(比如 rails s)需要加载哪些文件,加载的顺序这个问题了解得不是特别透彻,查了一些资料,记录了下来。

首先加载的其中一个是config/boot.rb, 一共会有三个文件参与加载和部署一个rails程序。

boot.rb: 配置Bundler以及加载路径
application.rb: 根据不同的启动环境(Rails.env)加载不同的rails gems, 配置应用程序
environment.rb: 加载所有的initializers
这三个文件都在启动整个rails环境时加载。

在rails 3以前的版本中,很多配置都在config/enviriment.rb中。但在rails 3中,这些配置都被移到了config/application.rb中,
这也是唯一一个被config/environment.rb所依赖的文件。
当对application.rb做修改后,需要重启服务器来使改变生效。
下面我们来逐步地看rails自动生成的application.rb的具体内容。

首先,

require File.expand_path('../boot', __FILE__)
这一行是真正启动应用--加载config/boot.rb. 值得注意是,boot.rb是rails application 自动生成的文件,
在一般情况下,我们不需要改变它。

接着,rails 3能让我们有选择性地加载需要的组件。
require 'rails/all'# To pick the frameworks you want, remove 'require "rails/all"'# and list the framework railties that you want:## require "active_model/railtie"# require "active_record/railtie"# require "action_controller/railtie"# require "action_view/railtie"# require "action_mailer/railtie"# require "active_resource/railtie"
接下去就可以定义我们应用的配置,用module和class的形式。
module Demo  class Application < Rails::Application    # Settings in config/environments/* take precedence over those specified here.    # Application configuration should go into files in config/initializers    # -- all .rb files in that directory are automatically loaded.    ...    endend
这样做的好处是可以让多个rails程序运行在一个进程里。
在这个module里面还有很多配置可以定义:
# Custom directories with classes and modules you want to be autoloadable# config.autoload_paths += %W(#{config.root}/extras)
默认情况下,rails会去app/models,app/controllers里去找代码。上面两行配置文件可以让程序去自定义的
路径下加载代码。例如
 config.autoload_paths += %W(#{config.root}/app/observers)

# Only load the plugins named here, in the order given (default is alphabetical).# :all can be used as a placeholder for all plugins not explicitly named.# config.plugins = [ :exception_notification, :ssl_requirement, :all ]

正常情况下,rails会按照字母顺序加载插件,但如果你遇到了问题,需要改变加载顺序,上面的配置代码可以完成这一点。

# Activate observers that should always be running.# config.active_record.observers = :cacher, :garbage_collector, :forum_observer

rails的Active Record obervers是rails用来执行一些指定任务的对象(比如清空缓存)。上面的代码可以指定
这些observers.

# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.# config.time_zone = 'Central Time (US & Canada)'
rails默认时区是UTC,上面的配置可以改变时区。
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]# config.i18n.default_locale = :de
这用来自定义语言

当然除了这些配置外,我们还可以自定义一些其它的配置。

从rails 2开始,我们可以把配置文件拆分成小的ruby文件放在config/initializers目录下。这个目录下的文件
在程序启动时被自动加载。除了默认的文件外,我们也可以加入自定义的ruby文件。有以下5个文件是默认就存在的。
backtrace_silencers:定义发生异常时trace的深度。
inflections:用来定义单词单复数的转换。
mime_types:用来注册除了rails默认支持的MIME类型外的类型。
session_store: 把session存在数据库而不是cookie


当然,除此之外你还可以进行其他的例如日志的配置。写得有点乱,大家可以对照rails new自动生成的文件看看。

原创粉丝点击