IK.AM

@making's tech note


JRebel でSpring Bootアプリケーションの快適リローディング (IntelliJ IDEA編)

🗃 {Dev/IDE/IntelliJIDEA/JRebel}
🏷 IntelliJ IDEA 🏷 JRebel 🏷 Spring Boot 
🗓 Updated at 2015-05-17T06:54:04Z  🗓 Created at 2015-05-17T06:54:04Z   🌎 English Page

Javaアプリケーション実行中に動的にクラスをリロードをしてくれるJRebelがとても便利なので紹介します。特にSpring Bootのようなスタンドアローンアプリケーションの開発中にとても有用です。

ここではIntelliJ IDEAでセットアップします。Eclipseでも、NetBeansでも使えます。

File > Settingsを開き、Pluginsを選択します。 Browse Repositoriesをクリックし、JRebel Pluginを探し、Install pluginボタンをクリックします。

image

IntelliJ IDEAを再起動すると、Run with JRebelコマンドが追加されます。

次の簡単なSpring Bootを実行します。

package demo;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class DemoApplication {

    @Bean
    CommandLineRunner foo() {
        return args -> System.out.println("hoge!");
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

image

DemoApplicationクラスを右クリックして、**Run with JRebel "DemoApplication"**を実行しようとすると、初回はアクティベーションを求められます。

image

14日間のフリートライアルを使うか、ライセンスキーを入力する必要があります。わたしは非商用の個人利用に限り使用できる、myJRebelのライセンスキーを取得したので、それを入力します。

image

Activate JRebelボタンを押せば、アクティベーションが完了します。

image

再度、**Run with JRebel "DemoApplication"**を実行すれば、以下のようなJRebelのログが出力されたあと、アプリケーションが起動します。

2015-05-17 16:00:16 JRebel: Contacting myJRebel server ..
2015-05-17 16:00:18 JRebel:  
2015-05-17 16:00:18 JRebel:  #############################################################
2015-05-17 16:00:18 JRebel:  
2015-05-17 16:00:18 JRebel:  JRebel Legacy Agent 6.1.3 (201504281742)
2015-05-17 16:00:18 JRebel:  (c) Copyright ZeroTurnaround AS, Estonia, Tartu.
2015-05-17 16:00:18 JRebel:  
2015-05-17 16:00:18 JRebel:  Over the last 1 days JRebel prevented
2015-05-17 16:00:18 JRebel:  at least 6 redeploys/restarts saving you about 0.2 hours.
2015-05-17 16:00:18 JRebel:  
2015-05-17 16:00:18 JRebel:  Licensed to Toshiaki Maki (using myJRebel).
2015-05-17 16:00:18 JRebel:  
2015-05-17 16:00:18 JRebel:  
2015-05-17 16:00:18 JRebel:  #############################################################
2015-05-17 16:00:18 JRebel:  
2015-05-17 16:00:19 JRebel: Monitoring resource '/Users/maki/git/firstapp/target/classes/application.properties'.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.2.3.RELEASE)
...

アプリケーションを立ち上げたまま、以下のようにソースコードを修正します。

package demo;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication {

    @Bean
    CommandLineRunner foo() {
        return args -> System.out.println("hoge!");
    }

    @RequestMapping("/")
    String hello() {
        return "hello";
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

右クリックして、**Compile 'DemoApplication.java'*をクリックし、http://localhost:8080にアクセスすると、

2015-05-17 16:09:37 JRebel: Reloading class 'demo.DemoApplication'.
2015-05-17 16:09:37 JRebel: Reinitialized class 'demo.DemoApplication$$EnhancerBySpringCGLIB$$8d24a580'.
2015-05-17 16:09:38 JRebel: Reinitialized class 'demo.DemoApplication$$EnhancerBySpringCGLIB$$8d24a580'.
2015-05-17 16:09:38 JRebel: Reinitialized class 'demo.DemoApplication$$FastClassBySpringCGLIB$$7211a142'.
2015-05-17 16:09:38 JRebel: Reinitialized class 'demo.DemoApplication$$EnhancerBySpringCGLIB$$8d24a580$$FastClassBySpringCGLIB$$54aa9091'.
2015-05-17 16:09:38 JRebel: Reinitialized class 'demo.DemoApplication$$EnhancerBySpringCGLIB$$8d24a580$$FastClassBySpringCGLIB$$54aa9091'.
2015-05-17 16:09:38 JRebel: Reconfiguring bean 'demoApplication' [demo.DemoApplication$$EnhancerBySpringCGLIB$$8d24a580]
2015-05-17 16:09:38 JRebel: Reconfiguring bean 'demoApplication' [demo.DemoApplication$$EnhancerBySpringCGLIB$$8d24a580]
2015-05-17 16:09:38.927  INFO 1853 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2015-05-17 16:09:38.928  INFO 1853 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2015-05-17 16:09:38.970  INFO 1853 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 41 ms
2015-05-17 16:09:39.000  INFO 1853 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2015-05-17 16:09:39.007  INFO 1853 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto java.lang.String demo.DemoApplication.hello()
2015-05-17 16:09:39.009  INFO 1853 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2015-05-17 16:09:39.009  INFO 1853 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[text/html],custom=[]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest)

というように影響のあるクラスがリロードされていることがわかります。アプリケーション再起動に比べてかなり高速に動作するので、作業効率が大幅に向上すると思われます。

リロード時のGifアニメ貼っておきます。

一々、右クリックコンパイルするのは面倒なので、Ctrl+SMake Projectをマッピングさせておくと、便利です。

image

image

ソースを修正するだけ修正したあと、Ctrl+Sを実行し、アプリケーションにアクセスすればリロードされます。

再DIもそこそこ対応しており、Spring Loadedみたいに不安定ではないので、かなりオススメです。

気に入ったら商用ライセンスを買いましょう。お買い求めはSamuraismまで!


✒️️ Edit  ⏰ History  🗑 Delete