今までStrutsベースのTerasolunaというJavaフレームワークを使っていましたが、今回Spring Bootを使ってみました。
で、ハマったことやメモしておきたいをここに書いていきます。
開発環境
- Windows7 / Windows10
- Eclipse pleiades 4.7
- Spring Boot 1.5.7
- JDK8
Spring関係
パッケージ構成がよくないと起動時にエラーが出る
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
	private final CityDao cityDao;
	private final HotelMapper hotelMapper;
	public DemoApplication(CityDao cityDao, HotelMapper hotelMapper) {
		this.cityDao = cityDao;
		this.hotelMapper = hotelMapper;
	}
	@Override
	public void run(String... args) throws Exception {
		System.out.println(this.cityDao.selectCityById(1));
		System.out.println(this.hotelMapper.selectByCityId(1));
	}
}
@Component
public class CityDao {
	private final SqlSession sqlSession;
	public CityDao(SqlSession sqlSession) {
		this.sqlSession = sqlSession;
	}
	public City selectCityById(long id) {
		return this.sqlSession.selectOne("selectCityById", id);
	}
}
Description:
Parameter 0 of constructor in hoge.DemoApplication required a bean of type 'sample.mybatis.dao.CityDao' that could not be found.
Action:
Consider defining a bean of type 'sample.mybatis.dao.CityDao' in your configuration.
DemoApplicationのパッケージをhogeからsample(CityDaoのパッケージと先頭が同じ)に移動したら、起動できた。
@AutoConfigureTestDatabaseのパッケージ
Spring Boot 1.5.7では、@AutoConfigureTestDatabaseのパッケージが2種類ある。新しい方を使う事。
- (古)org.springframework.boot.test.autoconfigure.orm.jpa
- (新)org.springframework.boot.test.autoconfigure.jdbc
MyBatis関係
実際のDBでテストするときは、@AutoConfigureTestDatabaseを使う
@RunWith(SpringRunner.class)
@MybatisTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Import(CityDao.class)
public class CityDaoTest {
    @Autowired
    private CityDao cityDao;
	@Test
	public void test() {
		City city = cityDao.selectCityById(1);
		//比較
		fail("まだ実装されていません");
	}
}
The In-memory embedded databases generally work well for tests since they are fast and don’t require any developer installation. However if you prefer to run tests against a real database, you can use the @AutoConfigureTestDatabase as follow:
AutoConfigureTestDatabase.Replace.NONEは、「Don't replace the application default DataSource.」。
https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/autoconfigure/jdbc/AutoConfigureTestDatabase.Replace.html 引用
DAOクラスを@Autowiredで使う場合は、@Importを使う
コードは上記を参照。
アンダースコア区切りをキャメルケースに変換してくれる
<setting name="mapUnderscoreToCamelCase" value="true" />
データベースにある A_COLUMN のようなアンダースコアを含む列を Camel Case の Java プロパティ aColumn に自動的にマッピングする機能の有効/無効を切り替えます。
Thymeleaf関係
閉じタグがないとエラー
自動リロード機能を有効にする方法
Spring Developper Tool を使います。
spring boot devtoolsのAutomatic restartについて
- Thymeleafをブラウザのリロードで更新する方法(キャッシュオフ)
spring.thymeleaf.cache=false
https://qiita.com/uzresk/items/31a4585f7828c4a9334f
環境関係
Mavenのリポジトリが読み込めない
ビルド時に、以下のエラーが発生しました。
Archive for required library: '.../.m2/repository/.../*.jar' in project '<プロジェクト名>' cannot be read or is not a valid ZIP file  <プロジェクト名>     ビルド・パス  ビルド・パスの問題
ビルド・パスのエラーが解決されるまで、プロジェクトをビルドできません
リポジトリを一度消したら、解決しました。
Eclipsenのコンソールログを色付け
- Eclipseプラグイン「ANSI Escape in Console」をインストールする
To install the plugin in Eclipse select Help -> Install New Software… and click “Add…” to add the following URL:
http://www.mihai-nita.net/eclipse
- 
application.propertiesで「ANSI Color Code」を有効にする。
spring.output.ansi.enabled=ALWAYS
https://mihai-nita.net/2013/06/03/eclipse-plugin-ansi-in-console/
http://www.logicbig.com/tutorials/spring-framework/spring-boot/color-logs/
起動時のバナーを変更、オフ
src/main/resources/にbanner.txtというファイルを置くと、起動時のバナーを変更できる。
以下の設定を記述すると、バナーを無効にできる。
spring.main.banner-mode=off
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-spring-application.html
Spring Bootの起動バナーの変更
