endokのブログ

IT・プログラミングネタ

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.xmlhsqldbを追加しているため、クラスパスは通っているはず。
を外してみるなど試行錯誤するも変わらず同じエラーが出続けた。

同じような症状をググっていき、最終的には
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にパスが書いてある。

EclipseMavenを利用した際に起きるようだが、こういったキャッシュ周り?の問題は非常に原因が掴みづらくて困る。