Spring DI/AOP
POMファイル
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-integration</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
Spring MVC
POMファイル
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Spring 開発・テスト
ファイル保存時にビルド&リスタートを自動するには、開発ツール(自分の場合はIntelli J)の自動ビルド設定を実施する必要がある。Dependencyの設定と併せてMavenプラグインのaddResourcesをTrueに設定する必要がある。
POMファイル
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
Thymeleaf
使用はPOMファイルで依存関係を設定し、プロパティファイルで有効化、キャッシュ可否などを指定する。
POMファイル
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
application.properties
#Thyemleafの有効・無効
spring.thymeleaf.enabled=true
#キャッシュの有効・無効
spring.thymeleaf.cache=false
#テンプレートの保存場所
spring.thymeleaf.prefix=classpath:/templates/
#テンプレートファイルの拡張子
spring.thymeleaf.suffix=.html
Spring JPA (H2DB & Lombok利用の場合)
POMファイル
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
application.properties
#DB接続設定
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:testdb;MODE=MySQL
spring.datasource.username=sa
spring.datasource.password=
<br>
#DB設定その他(テーブル自動生成・SQL出力)
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
Spring Session (JDBCでDB保存する場合)
POMファイル
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
application.properties
spring.session.store-type=jdbc
spring.session.jdbc.initialize-schema=always
Spring session
そのうち追記する予定
多言語対応
多言語化対応はプロパティを設定するだけで動作するようになる。
application.properties
#メッセージファイルの保存場所
spring.messages.basename=messages
#キャッシュ設定
spring.messages.cache-duration=-1
spring.messages.basename
メッセージファイルの既定場所は「src/main/resources」で、指定した名称がメッセージファイルの規定名称になる。上の記載例の場合は「messages.properties」がメッセージファイル名になる。メッセージファイルをサブフォルダに配置する場合は、規定場所からのパスを記載する。下図のように「i18n/messages」と記載すると「src/main/resources/i18n」が配置場所に、「messages.properties」が規定のファイル名になる。
spring.messages.cache-duration
キャッシュ設定を「-1」にするとキャッシュが無効になる。
参照例(Thymeleafの場合)
以下は「login.html」用のメッセージ設定例とHTML記述例。Thymeleafの場合は「th:text="#{userId}"」のように記載する。また、HTMLのlang属性に「ja」を指定したので対応するメッセージファイル名は「messages_ja.properties」になる。
userId=ログインID
password=パスワード
<!DOCTYPE html>
<html lang="ja" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form name="LoginForm" action="/login" method="post">
<label th:text="#{userId}">User ID</label>
<input name="userId" type="text">
<br/>
<label th:text="#{password}">Password</label>
<input name="password" type="password">
<br/>
<button id="login" type="submit" >login</button>
</form>
</body>
</html>