1. Building and Running
1.1. Create an executable JAR
The spring-boot-maven-plugin is already included in projects generated from start.spring.io. It repackages your JAR into a "fat JAR" containing all dependencies.
To build:
mvn clean package
To run:
java -jar target/carapp-0.1.0-SNAPSHOT.jar
In Spring Boot 4, support for embedded launch scripts (<executable>true</executable>) has been removed. You can no longer run a JAR directly as ./myapp.jar on Linux. Use java -jar instead, or look into alternatives such as systemd service units or Docker containers for deployment.
|
Your pom.xml should contain:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
| No additional configuration is needed on the plugin — the defaults are sufficient for creating a runnable fat JAR. |
1.2. Set a different environment (profile)
There are several ways to activate a specific Spring profile when running the JAR:
Using a JVM system property
java -Dspring.profiles.active=acceptance -jar target/carapp-0.1.0-SNAPSHOT.jar
Using an environment variable
export SPRING_PROFILES_ACTIVE=acceptance
java -jar target/carapp-0.1.0-SNAPSHOT.jar
Using a command-line argument
java -jar target/carapp-0.1.0-SNAPSHOT.jar --spring.profiles.active=acceptance
You can activate multiple profiles by separating them with a comma: --spring.profiles.active=acceptance,logging
|
1.3. Running with Docker (bonus)
A common modern approach for deploying Spring Boot applications is Docker. A minimal Dockerfile:
FROM eclipse-temurin:21-jre
COPY target/carapp-0.1.0-SNAPSHOT.jar /app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/app.jar"]
Build and run:
docker build -t carapp .
docker run -p 8080:8080 carapp
To activate a profile via Docker:
docker run -p 8080:8080 -e SPRING_PROFILES_ACTIVE=acceptance carapp
2. Additional methods in JPA
2.1. Dynamic finders
Spring Data JPA generates query implementations from method names automatically:
// Find a single car by license plate
Car findByLicensePlate(String licensePlate);
// Find all cars of a specific brand
List<Car> findByBrand(String brand);
// Find cars by brand and minimum mileage
List<Car> findByBrandAndMileageGreaterThan(String brand, int mileage);
2.2. Update all rows
Use @Modifying with a @Query to perform bulk updates:
@Modifying
@Transactional
@Query("UPDATE Car c SET c.mileage = 0")
void resetAllMileage();
@Modifying queries require an active transaction. Make sure you annotate the calling method (or the repository method itself) with @Transactional.
|
2.3. Delete some rows
@Modifying
@Transactional
@Query("DELETE FROM Car c WHERE c.mileage > :maxMileage")
int deleteByMileageGreaterThan(@Param("maxMileage") int maxMileage);
Returning int from a @Modifying query gives you the number of affected rows.
|
2.4. Delete all rows
@Modifying
@Transactional
@Query("DELETE FROM Car")
int clearAll();
| All your rows will be deleted from the table. Use with caution! |
2.5. More info
Spring Data JPA Reference |
|
Spring Data JPA Query Methods |
https://docs.spring.io/spring-data/jpa/reference/jpa/query-methods.html |