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環境變量的文件。