SpringBoot
Spring Initializer
init
Mavenの場合。
pom.xml
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>sample.springboot</groupId>
<properties>
<java.version>11</java.version>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven.compiler.source>${java.version}</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
</plugin>
</plugins>
</build>
main
package sample.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main( String[] args ) {
SpringApplication.run(App.class, args);
}
}
test
package sample.springboot;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class AppTest {
@Test
public void contextLoads() {
}
}
実行
mvn clean spring-boot:run
Spring Boot CLI
プロジェクト作成
spring init --dependencies=${依存関係},${依存関係} --group-id=~${グループID} ${プロジェクト名}
参考
アノテーション
アノテーション | パッケージ | 説明 | リンク |
---|---|---|---|
@Bean | org.springframework.context.annotation.Bean | メソッドが Spring コンテナーによって管理される Bean を生成することを示します。 | SpringBoot:Javadoc |
@Entity | javax.persistence.Entity | JPAで使う。エンティティとテーブルのマッピングをする。 | JPA (Java Persistence API)のアノテーション:SE学院 |
SpringBoot + React.js
- SpringBoot_and_React.js
- React.js と Spring Data RESTの実装。上記のソースの大半を含んでいるため、Privateリポジトリに設定。READMEだけ公開。
SpringBoot + log4j2
SpringBootは標準でSLF4J + Logbackを使用している。Logbackではなく、log4j2を使いたい場合の設定例。
概要
spring-boot-starter-log4j2
を追加する。spring-boot-starter-logging
がある場合は削除する。- SpringBootのライブラリでLogbackを含んでいるものがある場合は、
<exclusion>
を使用して除外する。mvn dependency:tree
コマンドで確認する。
pom.xml
設定例。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
設定ファイル
- ファイル名は
log4j2-spring.xml
にしておく。 - いつもどおりにしておくと、SpringBootのログが大量に出るため、抑制する。
<Loggers>
<logger name="org.springframework" level="FATAL"/>
</Loggers>