LoginSignup
23
30

More than 5 years have passed since last update.

【備忘録】Spring Bootでハマったことなど

Last updated at Posted at 2017-10-02

今までStrutsベースのTerasolunaというJavaフレームワークを使っていましたが、今回Spring Bootを使ってみました。
で、ハマったことやメモしておきたいをここに書いていきます。

開発環境

  • Windows7 / Windows10
  • Eclipse pleiades 4.7
  • Spring Boot 1.5.7
  • JDK8

Spring関係

パッケージ構成がよくないと起動時にエラーが出る

hoge.DemoApplication.java
@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));
    }
}
sample.mybatis.CityDao.java
@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

Spring Boot 1.5の主な変更点 参考


MyBatis関係

実際のDBでテストするときは、@AutoConfigureTestDatabaseを使う

CityDaoTest.java
@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:

http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-test-autoconfigure/#Using_a_real_database 引用

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をブラウザのリロードで更新する方法(キャッシュオフ)
application.properties
spring.thymeleaf.cache=false

https://qiita.com/uzresk/items/31a4585f7828c4a9334f


環境関係

Mavenのリポジトリが読み込めない

ビルド時に、以下のエラーが発生しました。

error-message
Archive for required library: '.../.m2/repository/.../*.jar' in project '<プロジェクト名>' cannot be read or is not a valid ZIP file  <プロジェクト名>     ビルド・パス  ビルド・パスの問題
ビルド・パスのエラーが解決されるまで、プロジェクトをビルドできません

リポジトリを一度消したら、解決しました。

Eclipsenのコンソールログを色付け

  1. 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

  2. application.propertiesで「ANSI Color Code」を有効にする。

application.properties
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というファイルを置くと、起動時のバナーを変更できる。
以下の設定を記述すると、バナーを無効にできる。

application.properties
spring.main.banner-mode=off

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-spring-application.html
Spring Bootの起動バナーの変更

分からないこと(調査中)

application.propertiesとapplication.ymlの違い

Tomcatにデプロイする方法

23
30
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
23
30