Spring Boot触ってみる その5 ープロファイルを利用した環境ごと設定の切り替えー
Spring Boot触ってみる その4 ーSQLServerへの接続ー - endokのブログ
の続き。
DB接続設定など、実行する環境に応じて切り替えたいパラメータがある。
Springでは"Profile"という仕組みで切り替えることができる。
その1(http://endok.hatenablog.com/entry/2016/06/04/124011)で作成したサンプルプロジェクトを引き続き利用する。
続きを読むSpring Boot触ってみる その4 ーSQLServerへの接続ー
Spring Boot触ってみる その3 ーThymeleaf Layout Dialectでレイアウトを共通化ー - endokのブログ
の続き。
SpringBootでのSQLServerへの接続方法を確認する。
SpringBoot特有の設定ということはなく、Springであれば同じような設定となるだろう。
その1(http://endok.hatenablog.com/entry/2016/06/04/124011)で作成したサンプルプロジェクトを引き続き利用する。
続きを読むSpringで定義済のBean名一覧を出力する
SpringBootはSpringのBean設定を隠蔽してくれているのだが、SpringBoot側で定義しているBeanをInjectionしたいときにはどういったBeanが定義済なのかわからず困る。
定義済のBean名をすべて出力する方法を調べたのでメモ。
方法は下記の通り。
1. BeanFactoryAwareをimplementsして、BeanFactoryを取得する。
2. BeanFactoryをDefaultListableBeanFactoryにキャストして、getBeanDefinitionNamesメソッドを利用する。
Controllerで実装した場合の例はこのようになる。
@Controller public class HelloController implements BeanFactoryAware { private DefaultListableBeanFactory beanFactory; @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { this.beanFactory = (DefaultListableBeanFactory)beanFactory; } @RequestMapping(value = "/page1") public String page1() { for (String name : beanFactory.getBeanDefinitionNames()) { System.out.println(name); } return "page1"; } }
Spring Boot触ってみる その3 ーThymeleaf Layout Dialectでレイアウトを共通化ー
Spring Boot触ってみる その2 ーController,テンプレートエンジン(Thymeleaf)ー - endokのブログ
の続き。
Thymeleafにもincludeなどを使った共通化の仕組みはあるが、
各ページごとにincludeは書きたくない・・・。
Tilesのように共通レイアウトを定義する仕組みはないものかと調べたところ、
Thymeleaf Layout Dialect(https://github.com/ultraq/thymeleaf-layout-dialect)というものがあったため試してみる。
その1(http://endok.hatenablog.com/entry/2016/06/04/124011)で作成したサンプルプロジェクトを引き続き利用する。
続きを読むSpringBootの開発に自動リロード(ホットデプロイ)を導入する
目次
- 目次
- はじめに
- SpringLoaded
- 特徴
- 導入方法
- Spring Boot Dev Tools
- 特徴
- 導入方法
- まとめ
はじめに
SpringBootの開発の流れは、
1. コードを書く
2. [Run As]→[Spring Boot App]で起動
3. 動作確認する
4. コードを書く
5. Consoleから起動中のアプリをTerminate
6. [Run As]→[Spring Boot App]で起動
7. 3に戻る
としていたが、
いちいち修正を反映するのに停止して起動するのは煩わしい。
1つ処理を書くたびに動作確認したいタイプなので、開発効率が悪くなってしまう。
S2Containerにはホットデプロイあって楽だったなぁ・・・と思ったので同様の仕組みを調べてみた。
- JRebel(http://samuraism.com/products/zeroturnaround/jrebel)
- SpringLoaded(https://github.com/spring-projects/spring-loaded)
- SpringBootDevTools(http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-devtools.html)
Springという名前が付いているし、JRebelは商用(myJRebelというフリー版もあるようだが)なので、SpringLoadedとSpringBootDevToolsを試してみる。
SpringToolSuiteでpom.xmlのdependencyを追加しても実行時に認識されない
SpringBootでDBアクセスをしようとしていたときのこと。
pom.xmlに下記記述を追加した。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.hsqldb</groupId> <artifactId>hsqldb</artifactId> <scope>runtime</scope> </dependency>
Repositoryクラスを作って下記のようにinjectionしてみるが、実行時にエラーが発生する。
@Autowired
MemberRepository repository;
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.dataSource; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).
>Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath.
ということで、組み込みDBであればクラスパスに配置さえしていれば問題なさそうである。
今回pom.xmlにhsqldbを追加しているため、クラスパスは通っているはず。
同じような症状をググっていき、最終的には
maven - Error running Basic Spring Batch example - Caused by: java.lang.ClassNotFoundException: org.hsqldb.jdbcDriver - Stack Overflow
に記載のあった、
>learing my entire local repository and then rebuilding has fixed the problem
を試したら解消した。
具体的には、Mavenのローカルレポジトリ内のファイルをすべて消したあとに再度実行すると解消していた。(ソースコードや設定は変更なし)
Mavenのローカルレポジトリの場所はPreferencesの[Maven]→[User Settings]のLocal Repositoryにパスが書いてある。
EclipseでMavenを利用した際に起きるようだが、こういったキャッシュ周り?の問題は非常に原因が掴みづらくて困る。
Spring Boot触ってみる その2 ーController,テンプレートエンジン(Thymeleaf)ー
Spring Boot触ってみる その1 ー環境構築・サンプルプロジェクト作成ー - endokのブログ
の続き。
Controller作成と、Thymeleafを使ったテンプレート描画部分を確認する。
Spring BootではJSPではなくThymeleafの方がベタな模様。
(他のJava製Webフレームワークではどうなんでしょう)
前回作成したサンプルプロジェクトを引き続き利用する。
続きを読む