SpringBootの初め方
SpringFrameworkですが、コアプロダクト以外にも関連プロダクトが多く、
どれを選択するかは、かなり慣れないと大変です。
また設定ファイルも多くなったりして記述量が多く、管理も大変だったりします。
SpringBootを使えばかなり楽になる気がします。
SPringBootの初め方ですが、まずは、Mavenプロジェクトを作成します。
作るプロジェクトのタイプはなんでもいいと思いますが、
クイックスタートを使って作ることにします。
mvn archetype:generate -DgroupId=sample -DartifactId=sample -DarchetypeArtifactId=maven-archetype-quickstart
生成されたプロジェクトのPOMへSpringBootの設定を追加します。
親プロジェクトとして、SpringBootの物があるので、それを指定します。
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.2.2.RELEASE</version> </parent>
次に必要な依存関係を定義します。
これもSpringBoot用のものがあるのでそれを指定します。
最低限必要なものは、下記の4つを指定するだけで良いはずです。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
ちなみにSpringBootのデフォルトの組み込みサーバーはTomcatなので、
Jettyを利用したい場合は、追加の設定が必要で、下記の設定を追加する必要があります。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency>
SpringMVCでRestでJSONを扱いたい場合は、デフォルトのままだと利用出来ないので、
JSONライブラリを追加で設定する必要があります。
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version><!--$NO-MVN-MAN-VER$--> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version><!--$NO-MVN-MAN-VER$--> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${jackson.version}</version><!--$NO-MVN-MAN-VER$--> </dependency>
SpringBootの設定は、以上です。
これで、実際SpringBootで作って行けるようになると思います。
SpringBootは、まずアプリケーション設定クラスを作成します。
ApplicationContext.xmlの代わりみたいなもので、
基本的にいままで、ApplicationContext.xmlの定義した内容は、
SpringBootの場合は、このクラスに定義して行きます。
@EnableAutoConfiguration @ComponentScan public class ApplicationConfiguration { }
@EnableAutoConfigurationのアノテーションが自動設定してくれます。
このアノテーションを付けておくと、SpringBootにあるデフォルトの設定ファイルを
自動的にロードしてくれます。
@ComponentScanのアノテーションは、このアノテーションを付けたクラスがある
パッケージ配下を全てスキャンして、コンテナに登録してくれます。
次に起動用のクラスを作成します。
Mainメソッド内で、起動定義を記述します。
@SpringBootApplication public class JettyApplication { /** * 起動処理 * * @param args コマンドライン引数 */ public static void main(String[] args) { // Jetty起動 SpringApplication.run(ApplicationConfiguration.class, args); } }
SpringBootのアプリケーションであることを明示的に書かないといけないので、
@SpringBootApplicationのアノテーションを付ける必要があります。
最後にコントローラークラスを作成すれば完了です。
@RequestMapping(value="/hello") @RestController public class HelloService { /** * Helloメッセージ * * @return helloメッセージ */ @RequestMapping(value = "/message", method = RequestMethod.GET) public String message() { return "HELLO"; } }
起動方法ですが、Eclipseを使っている場合は、Mainクラスを起動するだけで、起動します。
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.2.2.RELEASE) 2015-04-27 00:25:50.678 INFO 1979 --- [ main] j.c.y.s.developer.app.JettyApplication : Starting JettyApplication on MBP-15UAC-218.local with PID 1979 (/Users/toirie/project/workspace/corpit_spring_boot/target/classes started by toirie in /Users/toirie/project/workspace/corpit_spring_boot) 2015-04-27 00:25:50.712 INFO 1979 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4d3167f4: startup date [Mon Apr 27 00:25:50 JST 2015]; root of context hierarchy 2015-04-27 00:25:51.184 WARN 1979 --- [ main] o.m.s.mapper.ClassPathMapperScanner : No MyBatis mapper was found in '[jp.co.yahoo.system.developer.persistence]' package. Please check your configuration. 2015-04-27 00:25:51.237 INFO 1979 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'beanNameViewResolver': replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]] 2015-04-27 00:25:51.521 INFO 1979 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [class org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$2c77d480] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2015-04-27 00:25:51.534 INFO 1979 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'transactionAttributeSource' of type [class org.springframework.transaction.annotation.AnnotationTransactionAttributeSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2015-04-27 00:25:51.540 INFO 1979 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'transactionInterceptor' of type [class org.springframework.transaction.interceptor.TransactionInterceptor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2015-04-27 00:25:51.544 INFO 1979 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.config.internalTransactionAdvisor' of type [class org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2015-04-27 00:25:51.700 INFO 1979 --- [ main] e.j.JettyEmbeddedServletContainerFactory : Server initialized with port: 8080 2015-04-27 00:25:51.703 INFO 1979 --- [ main] org.eclipse.jetty.server.Server : jetty-9.2.9.v20150224
これで簡単なアプリとかは作れるようになるかと...。
データーベース周りとかは、また今度、書こうと思います。