Spring MVC는 웹 응용 프로그램을 구축하는 데 사용되는 Java 프레임 워크입니다. Model-View-Controller 설계 패턴을 따릅니다. Spring MVC는 Dispatcherservlet의 도움으로 스프링 프레임 워크에서 MVC를 사용할 수있는 우아한 솔루션을 제공합니다.
@annotations
Spring은 Rails Application Preloader입니다. 응용 프로그램을 백그라운드에서 실행하여 개발 속도를 높이므로 테스트, 레이크 작업 또는 마이그레이션을 실행할 때마다 부팅 할 필요가 없습니다.
rails new 버전 : 4.2, 5.0, 5.1, 5.2, 6.0 Spring은 Process.fork 를 광범위하게 사용하므로 Forking (Windows, Jruby)을 지원하지 않는 플랫폼에서 속도를 높일 수 없습니다.
보석에 스프링을 추가하십시오.
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
endSpring이 설치되고 지원되는 플랫폼 에서이 스 니펫은 명령 실행으로 스프링됩니다. 다른 경우, 스 니펫은 조용히 무시되고 정상으로 실행 된 후 선이 실행됩니다.
bin/ 로 입력 한 모든 명령을 접두사하지 않으려면 Direnv를 사용하여 응용 프로그램에 cd 에있을 때 PATH 에 ./bin 자동으로 추가 할 수 있습니다. 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/스프링 상태 스프링이 실행 중입니다.
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) 게시물#색인 포스트 /posts(.: format) 게시물#create new_post get /posts/new(.: format) 게시물#new edit_post get /posts/:id/edit(.:format) 게시물#idposts/:. /posts/:ID(.: format) 게시물#업데이트 /posts/:ID(.:Format) 게시물#Destroy
$ bin/spring status 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 Spring이 중지되었습니다.
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 이들은 이미 알고 있고 사랑하는 레일 명령을 실행합니다. 다른 하위 명령 (EG rails server )을 실행하면 Spring이 자동으로 기본 rails 실행 파일로 전달됩니다 (속도 업없이).
추가 명령을 위해 보석에 추가 할 수 있습니다.
rake test path/to/test 사용하여 특정 테스트/디렉토리를 실행할 수 있으므로 Rails 3의 Test::Unit스프링 관련 코드가 소스 저장소에 체크인을 원하지 않으면 Gemfile에 추가하지 않고 Spring을 사용할 수 있습니다. 그러나 젬 파일에 스프링을 추가하지 않고 스프링 빈 스튜브를 사용하는 것은 지원되지 않습니다.
이렇게 스프링을 사용하려면 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
이니셜 라이저 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.rb 및 config/spring.rb 읽습니다. ~/.spring.rb Bundler 이전에 로드되었지만 Bundler 이후에 config/spring.rb 가로드됩니다. 따라서 spring-commands-* 보석이 설치된 경우 프로젝트의 보석에 추가하지 않고도 모든 프로젝트에 사용할 수 있도록하려면 ~/.spring.rb 에 필요합니다.
config/spring_client.rb 는 Bundler 전에로드되며 서버 프로세스가 시작되기 전에 새로운 최상위 명령을 추가하는 데 사용될 수 있습니다.
봄은 Rails 응용 프로그램을 찾는 방법을 알아야합니다. 일반적인 앱이있는 경우 모든 것이 상자 밖으로 작동합니다. 특별한 설정 (예 : 엔진)이있는 프로젝트를 진행하는 경우 앱이 위치한 위치에 Spring을 알려야합니다.
Spring . application_root = './test/dummy' Spring.before_fork 콜백이 없습니다. 포크 전에 무언가를 실행하려면 ~/.spring.rb 또는 config/spring.rb 에 배치하거나 config/application.rb , config/environments/*.rb 또는 config/initializers/*.rb 와 같이 응용 프로그램이 초기화 될 때 실행되는 파일에 배치 할 수 있습니다.
Spring이 프로세스를 시작한 후 코드를 실행할 수 있지만 실제 명령이 실행되기 전에는 코드를 실행할 수 있습니다. 외부 서비스에 연결하거나 일반적인 정리 또는 동적 구성을 설정 해야하는 경우 after_fork 콜백을 사용하고 싶을 수도 있습니다.
Spring . after_fork do
# run arbitrary code
end 여러 콜백을 등록하려면 Spring.after_fork 여러 번 다른 블록으로 여러 번 전화 할 수 있습니다.
스프링은 서버 부츠시로드 된 파일의 파일 변경을 자동으로 감지합니다. 변화로 인해 영향을받는 환경이 다시 시작됩니다.
응용 프로그램 재시작을 트리거 해야하는 추가 파일 또는 디렉토리가 있으면 Spring.watch 로 지정할 수 있습니다.
Spring . watch "config/some_config_file.yml"기본적으로 스프링은 0.2 초마다 한 번씩 변경에 대한 파일 시스템을 폴링합니다. 이 방법에는 구성이 없지만 CPU가 너무 많은 경우 스프링 시계가 등리스트 인 GEM을 설치하여 이벤트 기반 파일 시스템 청취를 사용할 수 있습니다.
명령이 실행될 때마다 표시되는 "스프링 프리 로더를 통해 실행"메시지를 비활성화하려면 다음과 같습니다.
Spring . quiet = true다음 환경 변수는 봄에 사용됩니다.
DISABLE_SPRING 설정하면 스프링이 우회되고 응용 프로그램이 전경 프로세스에서 부팅됩니다.SPRING_LOG 스프링이 로그 메시지를 작성하는 파일의 경로.SPRING_TMP_PATH 스프링이 임시 파일 (pidfile 및 소켓)을 작성 해야하는 디렉토리. 기본적으로 XDG_RUNTIME_DIR 환경 변수를 사용하거나 Dir.tmpdir 사용한 다음 spring-$UID 라는 이름의 디렉토리를 만듭니다. Rails 애플리케이션의 tmp/ 디렉토리는 Unix 소켓을 지원하지 않는 파일 시스템에있을 수 있기 때문에 사용하지 않습니다.SPRING_APPLICATION_ID 고유 한 레일 응용 프로그램을 식별하는 데 사용됩니다. 기본적으로 그것은 현재 RUBY_VERSION 의 MD5 해시이며 Rails 프로젝트 루트로가는 경로입니다.SPRING_SOCKET 이 장기 실행되는 스프링 서버 프로세스와 통신하는 데 사용하는 UNIX 소켓에 사용해야하는 경로. 기본적으로 이것은 SPRING_TMP_PATH/SPRING_APPLICATION_ID 입니다.SPRING_PIDFILE 장기 실행 스프링 서버 프로세스의 PID를 저장하는 데 사용해야하는 경로. 기본적으로 이것은 소켓 경로와 관련이 있습니다. 소켓 경로가 /foo/bar/spring.sock 인 경우 pidfile은 /foo/bar/spring.pid 입니다.SPRING_SERVER_COMMAND 스프링 서버가 아직 실행되지 않을 때 실행하도록 실행하는 명령. 기본값으로 spring _[version]_ server --background . Spring이 무엇을하고 있는지에 대한 자세한 정보를 얻으려면 별도의 터미널에서 Spring을 명시 적으로 실행할 수 있습니다.
$ spring server
로깅 출력은 stdout에 인쇄됩니다. SPRING_LOG 환경 변수를 사용하여 로그 출력을 파일로 보낼 수도 있습니다.