Spring MVC是用于构建Web应用程序的Java框架。它遵循模型视图控制器的设计模式。A弹簧MVC提供了一种优雅的解决方案,可在DispatcherServ的帮助下在弹簧框架中使用MVC。
@annotations
春季是铁轨应用程序预加载器。它通过保持应用程序在后台运行而加快开发加快开发,因此您无需每次运行测试,耙式任务或迁移时都可以启动它。
rails new以生成应用程序的情况下安装的) Spring广泛使用Process.fork ,因此无法在不支持分叉(Windows,Jruby)的平台上提供速度。
将弹簧添加到您的gemfile:
gem "spring" , group : :development (注意:使用gem "spring", git: "..."不起作用,也不是使用弹簧的支持方式。)
建议在bin/目录中“弹簧”可执行文件:
$ bundle install
$ bundle exec spring binstub --all
这将生成一个bin/spring可执行文件,并将一小件代码插入到相关的现有可执行文件中。片段看起来像这样:
begin
load File . expand_path ( '../spring' , __FILE__ )
rescue LoadError
end在安装和支撑弹簧的平台上,此片段挂钩弹出命令的执行。在其他情况下,摘要将被默默地忽略,并将其在正常情况下执行之后。
如果您不想将每个命令前缀使用bin/ tote,则可以使用direnv自动将./bin cd到您的PATH中时。只需在您的Rails目录中使用命令PATH_add bin创建.envrc文件即可。
对于本演练,我已经生成了一个新的Rails应用程序,并且Run rails generate scaffold post name:string 。
让我们进行测试:
$ time bin/rake test test/controllers/posts_controller_test.rb
Running via Spring preloader in process 2734
Run options:
# Running tests:
.......
Finished tests in 0.127245s, 55.0121 tests/s, 78.5887 assertions/s.
7 tests, 10 assertions, 0 failures, 0 errors, 0 skips
real 0m2.165s
user 0m0.281s
sys 0m0.066s
这不是特别快,因为这是第一次运行,因此Spring必须启动应用程序。现在正在运行:
$ bin/spring status
Spring is running:
26150 spring server | spring-demo-app | started 3 secs ago
26155 spring app | spring-demo-app | started 3 secs ago | test mode
下一个运行更快:
$ time bin/rake test test/controllers/posts_controller_test.rb
Running via Spring preloader in process 8352
Run options:
If we edit any of the application files, or test files, the changes will
be picked up on the next run without the background process having to
restart. This works in exactly the same way as the code reloading
which allows you to refresh your browser and instantly see changes during
development.
But if we edit any of the files which were used to start the application
(configs, initializers, your gemfile), the application needs to be fully
restarted. This happens automatically.
Let's "edit" `config/application.rb`:
$ touch config/application.rb $ bin/spring状态弹簧正在运行:
26150春季服务器|春季播放|开始于36秒前26556春季应用|春季播放|从1秒开始|测试模式
The application detected that `config/application.rb` changed and
automatically restarted itself.
If we run a command that uses a different environment, then that
environment gets booted up:
$ bin /rake途径通过春季预加载器在过程2363帖子get /posts(。:format)帖子#index post /posts> post /posts( /posts /:id(。:format)帖子#update delete delete /posts/:id(.:format)帖子#销毁
$ bin/spring状态春季正在运行:
26150春季服务器|春季播放|开始1分钟前26556春季应用|春季播放|从42秒开始|测试模式26707春季应用|春季播放|开始2秒前|开发模式
There's no need to "shut down" Spring. This will happen automatically
when you close your terminal. However if you do want to do a manual shut
down, use the `stop` command:
$ bin/spring stop弹簧停止。
From within your code, you can check whether Spring is active with `if defined?(Spring)`.
### Removal
To remove Spring:
* 'Unspring' your bin/ executables: `bin/spring binstub --remove --all`
* Remove spring from your Gemfile
### Deployment
You must not install Spring on your production environment. To prevent it from
being installed, provide the `--without development test` argument to the
`bundle install` command which is used to install gems on your production
machines:
$捆绑安装 - 无开发测试
## Commands
### `rake`
Runs a rake task. Rake tasks run in the `development` environment by
default. You can change this on the fly by using the `RAILS_ENV`
environment variable. The environment is also configurable with the
`Spring::Commands::Rake.environment_matchers` hash. This has sensible
defaults, but if you need to match a specific task to a specific
environment, you'd do it like this:
``` ruby
Spring::Commands::Rake.environment_matchers["perf_test"] = "test"
Spring::Commands::Rake.environment_matchers[/^perf/] = "test"
# To change the environment when you run `rake` with no arguments
Spring::Commands::Rake.environment_matchers[:default] = "development"
rails console , rails generate , rails runner这些执行您已经知道和喜欢的铁轨命令。如果您运行另一个子命令(例如rails server ),则Spring会自动将其传递到基础rails可执行文件(无需加速)。
您可以将它们添加到您的gemfile中以获取其他命令:
Test::Unit测试很有用,因为只有Rails 4允许您使用rake test path/to/test来运行特定的测试/目录。如果您不希望将与弹簧相关的代码签到您的源存储库中,则可以使用弹簧而无需添加弹簧。但是,不支持使用弹簧binstubs而不将弹簧添加到Gemfile中。
要使用这样的弹簧,请执行gem install spring ,然后使用spring前缀命令。例如,您不会运行bin/rake -T ,而是运行spring rake -T 。
如果您使用的是Spring binstubs,但暂时不希望命令在弹簧中运行,请设置DISABLE_SPRING环境变量。
Spring使用Rails的类重新加载机制( ActiveSupport::Dependencies ),以使您的代码保持最新的测试运行之间。这是相同的机制,可让您在刷新页面时在开发过程中看到更改。但是,您可能以前从未在test环境中使用过这种机制,这可能会导致问题。
重要的是要意识到,代码重新加载意味着您应用程序中的常数是文件更改后的不同对象:
$ bin/rails runner 'puts User.object_id'
70127987886040
$ touch app/models/user.rb
$ bin/rails runner 'puts User.object_id'
70127976764620
假设您有一个initializer config/initializers/save_user_class.rb这样:
USER_CLASS = User这节省了User类的第一个版本,在重新加载代码后,该类别与User的对象不同:
$ bin/rails runner 'puts User == USER_CLASS'
true
$ touch app/models/user.rb
$ bin/rails runner 'puts User == USER_CLASS'
false
因此,为避免此问题,请勿在初始化代码中保存对应用程序常数的引用。
截至春季1.7,有一些支持。有关如何使用Docker进行的信息,请参见此示例存储库。
Spring将为自定义设置读取~/.spring.rb和config/spring.rb 。请注意, ~/.spring.rb已在捆绑器之前加载,但是在Bundler之后加载了config/spring.rb 。因此,如果您安装了任何spring-commands-*宝石,您希望在所有项目中使用,而无需将其添加到项目的gemfile中,请在您的~/.spring.rb中需要它们。
config/spring_client.rb也已在捆绑器之前加载,并且在启动服务器进程之前,它可用于添加新的顶级命令。
春季必须知道如何找到您的铁路申请。如果您有一个普通的应用程序,则一切都可以使用。如果您正在从事具有特殊设置的项目(例如引擎),则必须告诉Spring您的应用程序所在:
Spring . application_root = './test/dummy'没有Spring.before_fork回调。要在叉子之前运行某些内容,您可以将其放入~/.spring.rb或config/spring.rb或在应用程序初始化时运行的任何文件中,例如config/application.rb , config/environments/*.rb或config/initializers/*.rb 。
您可能需要在弹簧分叉后运行代码,但在运行实际命令之前。如果您必须连接到外部服务,进行一些常规清理或设置动态配置,则可能需要使用after_fork回调。
Spring . after_fork do
# run arbitrary code
end如果要注册多个回调,则可以简单地调用Spring.after_fork多次使用不同的块。
Spring将自动检测到服务器靴时加载的任何文件的文件更改。更改将导致受影响的环境重新启动。
如果还有其他要触发应用程序重新启动的文件或目录,则可以使用Spring.watch .:
Spring . watch "config/some_config_file.yml"默认情况下,Spring对文件系统进行一次调查,每0.2秒更改一次。此方法需要零配置,但是如果您发现它使用过多的CPU,则可以通过安装Spring-Watcher-Listen GEM来使用基于事件的文件系统侦听。
为了禁用“通过Spring Preloader”消息,该消息每次运行时都会显示:
Spring . quiet = trueSpring使用以下环境变量:
DISABLE_SPRING如果设置,弹簧将被绕过,您的应用程序将在前景过程中启动SPRING_LOG将将日志消息写入的文件的路径。SPRING_TMP_PATH应编写其临时文件(pidfile和套接字)的目录。默认情况下,我们使用XDG_RUNTIME_DIR环境变量,或Dir.tmpdir ,然后在该名称的spring-$UID中创建一个目录。我们不使用您的Rails应用程序的tmp/ Directory,因为这可能是在不支持UNIX插座的文件系统上。SPRING_APPLICATION_ID用于识别不同的导轨应用程序。默认情况下,它是当前RUBY_VERSION的MD5哈希,也是导轨项目根的路径。SPRING_SOCKET用于与长期运行的Spring服务器过程通信的UNIX插座的路径。默认情况下,这是SPRING_TMP_PATH/SPRING_APPLICATION_ID 。SPRING_PIDFILE应用于存储长期运行弹簧服务器过程的PID的路径。默认情况下,这与套接字路径有关;如果套接字路径为/foo/bar/spring.sock ,则pidfile将为/foo/bar/spring.pid 。SPRING_SERVER_COMMAND命令在尚未运行时运行以启动Spring服务器。默认为spring _[version]_ server --background 。 如果您想获得有关Spring在做什么的更多信息,则可以在单独的终端中明确运行Spring:
$ spring server
记录输出将打印到Stdout。您还可以将日志输出发送到具有SPRING_LOG环境变量的文件。