楽落人の備忘録

楽して生きていきたい。知識とか覚えておきたくない。

an unknown property

spring boot アプリケーション作成時に、独自プロパティを設定してjava内で利用しようとした際に、

「'test.test.tset' is an unknown property.」と警告が表示された。

それを回避する方法をメモとして残しておく。

参考:

Spring Boot 独自のプロパティを定義して使う - 山崎屋の技術メモ

 

src/main/resources/META-INF/spring-configuration-metadata.json

というファイルを作成して中に、

{
	"properties": [
		{
			"name": "test.test.test",
			"type": "java.lang.String",
			"description": "A description for 'test.test.test'"
		}
	]
}

と記載しておけば警告は消える。

ただし、消してしまって問題ないかは確認が必要な気がする。

参考:

Configuration Metadata

 

推測ではあるが上記のMETA-INF/spring-configuration-metadata.jsonにてそもそもフレームワーク側でも許容するプロパティ値を持っていると考えられるため、追加して警告対象外とした項目が既存フレームワークで許容している項目と重複すると都合が悪い気がする。

が、今回はいったん回避方法として記載を残す。

 

補足:

上記プロパティはapplication.propertiesに記載し、JAVA側は以下のように実装。

import org.springframework.beans.factory.annotation.Value;
~中略~
@Value("${test.test.test}") private String password;

コネクションプールサイズ設定

spring bootアプリケーションでコネクションプールの設定方法を調べていると

設定しているつもりが設定が反映されない

という情報をいくつか見た。

参考:

spring-boot - SpringブートでHikariCPを使用する方法Flywayと組み合わせて2つのデータソースを使用する

 

そのため設定後に確認が必要だと思い、確認方法を調査し野たのでメモ。

参考:

Spring Boot パフォーマンス関連の設定 - Qiita

 

アプリ起動後に、コンソールで「jconsole」を実行。
あとは設定した項目っぽいやつを探す。

この時はhikaricpで設定していたのでそれを探した。

mbeansに設定している箇所の数値が属性として出ているので該当箇所を見ながら設定を変更したところjconsole側にも変更した値で出力されることを確認。

 

Invocation of init method failed; nested exception is javax.validation.ValidationException

タイトルわかりにくいですが

SpringBootアプリケーションの起動エラーに関してです。

少しはまったのと、別件調査・試行錯誤中に発生してイラっとしたので備忘録として記載します。

構成:SpringBoot[v2.3.3.RELEASE]、Maven[3.8]、Java[1.8]

 

SpringBootアプリケーション作成中に挙動が訝しいので「.m2」配下のrepositoryフォルダ内を削除して再度ライブラリをダウンロードしたところ、mavenのinstall実行でエラーが発生するようになった。

 

エラーは以下の通り

2020/09/16 11:59:10 WARN  [main] - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultValidator' defined in class path resource [org/springframework/boot/autoconfigure/validation/ValidationAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.validation.ValidationException: Unable to instantiate Configuration.
Exception in thread "task-2" java.lang.IllegalStateException: EntityManagerFactory is closed
	at org.hibernate.internal.SessionFactoryImpl.validateNotClosed(SessionFactoryImpl.java:509)
	at org.hibernate.internal.SessionFactoryImpl.getProperties(SessionFactoryImpl.java:503)
	at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher.findDataSource(DataSourceInitializedPublisher.java:105)
	at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher.publishEventIfRequired(DataSourceInitializedPublisher.java:97)
	at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher.access$100(DataSourceInitializedPublisher.java:50)
	at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher$DataSourceSchemaCreatedPublisher.lambda$postProcessEntityManagerFactory$0(DataSourceInitializedPublisher.java:200)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:834)

Invocation of init method failed; nested exception is javax.validation.ValidationException

が主原因のように見える。。。

 

????

 

ググってみるとどうもバリデーションが正常に言っていないようです。
チェック処理が邪魔になるとは。。
参考:

Java - Spring Boot -プロジェクト作成し、起動した後のエラー|teratail

spring - Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath - Stack Overflow

 

どうもmavenで記載しているライブラリのバージョンが古いせいでエラーが出ている模様。

バージョンの古いライブラリがかみ合わないorバグ の影響でエラーが発生しているものと解釈。

ライブラリって便利だけどこういう時の更新忘れや不一致は困りものですね。
pom.xmlの以下の記述を修正。

修正前:

<dependency>
	<groupid>org.hibernate</groupid>
	<artifactid>hibernate-validator</artifactid>
	<version>5.2.0.Alpha1</version>
</dependency>

修正後:

<dependency>
	<groupId>org.hibernate</groupId>
	<artifactId>hibernate-validator</artifactId>
	<version>5.3.5.Final</version>
</dependency>

 

そもそもalphaってあるのに使うのが危ない。。

利用時は試行錯誤していたのでそこまで気が回っていなかった。

新規構築時などは一通り構築後にリポジトリの再構成やPOMの見直しをしたほうがいいですね。

そのあと運用になるとなかなか手を入れる機会もなくなりますし。

 

補足:m2フォルダのrepository内から利用していないバージョンのhibernate-validatorのライブラリ削除しておくことを推奨します。利用時にどちらを利用しているのかを明確にするため。たぶんバージョン指定しているから問題ないとは思うけど。

springboot postgres

久しぶりにローカル環境でpostgresに接続しようとしたらエラー。

 

アプリはspringbootで作成。

作成の参考サイト:

https://intellectual-curiosity.tokyo/2019/04/21/spring-boot%E3%81%A7%E3%83%87%E3%83%BC%E3%82%BF%E3%83%99%E3%83%BC%E3%82%B9%E3%81%AB%E3%82%A2%E3%82%AF%E3%82%BB%E3%82%B9%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95/

 

途中調子に乗ってpom.xmleclipseの自動生成に任せて作成。

それっぽくできてたのでブラウザからDBアクセスしようとしたら

以下のエラーが発生した。

[/hello1] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)] with root cause

 

どうも別プロジェクトのキャッシュが悪さをしている?模様。

情報サイト:

http://www.code-magagine.com/?p=2911

 

m2のrepository削除して再生成(maven install)してみたが、同様のエラーが発生。

別の情報サイトを発見。

情報サイト2:

https://poridiet.com/2020/02/10/%E3%80%90java%E3%80%91spring%E3%81%A7%E4%BD%9C%E3%82%8Bweb%E3%82%A2%E3%83%97%E3%83%AA%E3%82%B1%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3%E3%80%90%E3%81%9D%E3%81%AE%EF%BC%91%E3%80%91/

 

情報サイト2だとpomの記述が足りない。

<dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>

追加してみたけどだめ。だけど、エラーがかわった。

 

2020-09-01 12:45:08.032 ERROR 15040 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-8080-exec-1] Exception processing template "hello1": Error resolving template [hello1], template might not exist or might not be accessible by any of the configured Template Resolvers

org.thymeleaf.exceptions.TemplateInputException: Error resolving template [hello1], template might not exist or might not be accessible by any of the configured Template Resolvers

 

?テンプレートが解決できないっぽいので、テンプレートのhtmlファイル名とアクセスURLの名前、サービス記載のURLパスをすべて統一してhello1に変更。

 

アクセスできた!

 

テンプレートのファイル名をきちんと合わせる必要があった模様。

 

postgres pgadmin4

最近、最新のpostgresをインストールしておなじみpgadminどれくらい重いかなと起動を試したところ、

なんとブラウザになっていた!

すかさずアドレスをブックマークして後日再度利用しようとしたところブックマークからはアクセス拒否となりアクセスできなかった。。。

 

(postgres version : PostgreSQL 13beta3, compiled by Visual C++ build 1914, 64-bit)

 

なんでだろうと調べてみると、おそらく毎回アクセス用のキーや認証などが必要でブックマークからのアクセスはできないようにしている模様。

だからインストールしたプログラムから起動するとアクセスできる。

 

アクセス方法:

タスクバーのwindowsボタンからpgadmin4を起動する

 

これだけでアクセスできた。

 

 

pom.xmlにローカルに存在するjarを取り込ませる

springbootでDB接続のためにojdbc8.jarが必要になり、以下のサイトを参考にダウンロードしたjarをmavenでビルド時に利用するように指定をしていた。

 

参考サイト:

https://intellectual-curiosity.tokyo/2019/04/30/spring-boot%E3%81%A7oracle-%E3%83%87%E3%83%BC%E3%82%BF%E3%83%99%E3%83%BC%E3%82%B9%E3%81%AB%E6%8E%A5%E7%B6%9A%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95/

 

上記でも動作には問題がなく、正常にDB接続も行うことができるが、

maven clean

maven install

を実行した際に以下のような警告がコンソールに発生する。

 警告内容:

[WARNING] 'dependencies.dependency.systemPath' for com.oracle:ojdbc8:jar should not point at files within the project directory, ${basedir}/lib/ojdbc8.jar will be unresolvable by dependent projects 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.

 

どうもパスの指定している箇所が悪さをしているようには見えるが原因がよくわからない。

以下に同様のエラーに遭遇し対応しているサイトを発見。

 

参考サイト:

https://www.greptips.com/posts/1094/

 

 どうやら非推奨のスコープとされているsystemを利用していることが警告の原因の用だった。

sysytemを利用せず、サイトを参考にpulugin側にパスを記載。
以下は詳細未調査のため、推測だがplugin側に記載することで、ローカルのパスを参照してのライブラリ取り込みをできるようにした模様。(今度ちゃんと調べよう。

spring.jpa.open-in-view

springbootのアプリケーション作成中、毎回起動時に警告が発生していた。

警告内容:

spring.jpa.open-in-view is enabled by default.
Therefore, database queries may be performed during view rendering.
Explicitly configure spring.jpa.open-in-view to disable this warning

 

上記の主原因は、どうやら「spring.jpa.open-in-view」がデフォルト設定になっていることのよう。
「spring.jpa.open-in-view」とは?
以下のサイトがとても分かりやすくまとめてくれているので大変参考になった。
参考サイト:

Open Session in Viewは使用すべきなのか?

 

対応方法:
結局警告出ないようにするにはどうするのか。

対応方法は以下の参考サイトに記載されたプロパティの設定を記載して、デフォルト(有効)設定から、手動で無効設定に変更する。
参考サイト:

java - Spring Bootのこのspring.jpa.open-in-view = trueプロパティとは何ですか?

 

設定記載例:
spring.jpa.open-in-view=false