mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-07-15 01:07:30 +08:00
Polish
This commit is contained in:
parent
595fc13b34
commit
ae90add7c7
@ -22,6 +22,7 @@ import java.util.Collection;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
@ -37,6 +38,7 @@ import org.springframework.aot.hint.MemberCategory;
|
||||
import org.springframework.aot.hint.RuntimeHints;
|
||||
import org.springframework.aot.hint.RuntimeHintsRegistrar;
|
||||
import org.springframework.aot.hint.TypeHint;
|
||||
import org.springframework.aot.hint.TypeHint.Builder;
|
||||
import org.springframework.aot.hint.TypeReference;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
@ -250,19 +252,16 @@ class HibernateJpaConfiguration extends JpaBaseConfiguration {
|
||||
|
||||
static class HibernateRuntimeHints implements RuntimeHintsRegistrar {
|
||||
|
||||
private static final Consumer<Builder> INVOKE_DECLARED_CONSTRUCTORS = TypeHint
|
||||
.builtWith(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
|
||||
|
||||
@Override
|
||||
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
|
||||
for (String clazz : NO_JTA_PLATFORM_CLASSES) {
|
||||
hints.reflection()
|
||||
.registerType(TypeReference.of(clazz),
|
||||
TypeHint.builtWith(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS));
|
||||
for (String noJtaPlatformClass : NO_JTA_PLATFORM_CLASSES) {
|
||||
hints.reflection().registerType(TypeReference.of(noJtaPlatformClass), INVOKE_DECLARED_CONSTRUCTORS);
|
||||
}
|
||||
hints.reflection()
|
||||
.registerType(SpringImplicitNamingStrategy.class,
|
||||
TypeHint.builtWith(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS));
|
||||
hints.reflection()
|
||||
.registerType(CamelCaseToUnderscoresNamingStrategy.class,
|
||||
TypeHint.builtWith(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS));
|
||||
hints.reflection().registerType(SpringImplicitNamingStrategy.class, INVOKE_DECLARED_CONSTRUCTORS);
|
||||
hints.reflection().registerType(CamelCaseToUnderscoresNamingStrategy.class, INVOKE_DECLARED_CONSTRUCTORS);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -487,10 +487,11 @@ class HibernateJpaAutoConfigurationTests extends AbstractJpaAutoConfigurationTes
|
||||
void registersHintsForJtaClasses() {
|
||||
RuntimeHints hints = new RuntimeHints();
|
||||
new HibernateRuntimeHints().registerHints(hints, getClass().getClassLoader());
|
||||
for (String clazz : Arrays.asList("org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform",
|
||||
for (String noJtaPlatformClass : Arrays.asList(
|
||||
"org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform",
|
||||
"org.hibernate.service.jta.platform.internal.NoJtaPlatform")) {
|
||||
assertThat(RuntimeHintsPredicates.reflection()
|
||||
.onType(TypeReference.of(clazz))
|
||||
.onType(TypeReference.of(noJtaPlatformClass))
|
||||
.withMemberCategories(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)).accepts(hints);
|
||||
}
|
||||
}
|
||||
@ -499,10 +500,10 @@ class HibernateJpaAutoConfigurationTests extends AbstractJpaAutoConfigurationTes
|
||||
void registersHintsForNamingClasses() {
|
||||
RuntimeHints hints = new RuntimeHints();
|
||||
new HibernateRuntimeHints().registerHints(hints, getClass().getClassLoader());
|
||||
for (Class<?> clazz : Arrays.asList(SpringImplicitNamingStrategy.class,
|
||||
for (Class<?> noJtaPlatformClass : Arrays.asList(SpringImplicitNamingStrategy.class,
|
||||
CamelCaseToUnderscoresNamingStrategy.class)) {
|
||||
assertThat(RuntimeHintsPredicates.reflection()
|
||||
.onType(clazz)
|
||||
.onType(noJtaPlatformClass)
|
||||
.withMemberCategories(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)).accepts(hints);
|
||||
}
|
||||
}
|
||||
|
@ -36,6 +36,7 @@ import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.event.SimpleApplicationEventMulticaster;
|
||||
import org.springframework.core.log.LogMessage;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Manages the lifecycle for docker compose services.
|
||||
@ -125,11 +126,8 @@ class DockerComposeLifecycleManager {
|
||||
protected DockerComposeFile getComposeFile() {
|
||||
DockerComposeFile composeFile = (this.properties.getFile() != null)
|
||||
? DockerComposeFile.of(this.properties.getFile()) : DockerComposeFile.find(this.workingDirectory);
|
||||
if (composeFile == null) {
|
||||
File dir = (this.workingDirectory != null) ? this.workingDirectory : new File(".");
|
||||
throw new IllegalStateException("No Docker Compose file found in directory '%s'"
|
||||
.formatted(dir.toPath().toAbsolutePath().toString()));
|
||||
}
|
||||
Assert.state(composeFile != null, () -> "No Docker Compose file found in directory '%s'".formatted(
|
||||
((this.workingDirectory != null) ? this.workingDirectory : new File(".")).toPath().toAbsolutePath()));
|
||||
logger.info(LogMessage.format("Using Docker Compose file '%s'", composeFile));
|
||||
return composeFile;
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
[[deployment.efficient]]
|
||||
== Efficient deployments
|
||||
|
||||
|
||||
|
||||
[[deployment.efficient.unpacking]]
|
||||
=== Unpacking the Executable JAR
|
||||
If you are running your application from a container, you can use an executable jar, but it is also often an advantage to explode it and run it in a different way.
|
||||
@ -28,6 +30,8 @@ Once you have unpacked the jar file, you can also get an extra boost to startup
|
||||
NOTE: Using the `JarLauncher` over the application's main method has the added benefit of a predictable classpath order.
|
||||
The jar contains a `classpath.idx` file which is used by the `JarLauncher` when constructing the classpath.
|
||||
|
||||
|
||||
|
||||
[[deployment.efficient.aot]]
|
||||
=== Using Ahead-of-time Processing With the JVM
|
||||
|
||||
|
@ -1083,9 +1083,9 @@ TIP: You can use the `@ServiceConnection` annotation on `Container` fields to es
|
||||
You can also add <<features#features.testing.testcontainers.dynamic-properties,`@DynamicPropertySource` annotated methods>> to your declaration class.
|
||||
|
||||
|
||||
|
||||
[[features.testing.testcontainers.at-development-time.devtools]]
|
||||
===== Using DevTools with Testcontainers at Development Time
|
||||
|
||||
When using devtools, you can annotate beans and bean methods with `@RestartScope`.
|
||||
Such beans won't be recreated when the devtools restart the application.
|
||||
This is especially useful for Testcontainer `Container` beans, as they keep their state despite the application restart.
|
||||
@ -1095,6 +1095,8 @@ include::code:MyContainersConfiguration[]
|
||||
WARNING: If you're using Gradle and want to use this feature, you need to change the configuration of the `spring-boot-devtools` dependency from `developmentOnly` to `testImplementation`.
|
||||
With the default scope of `developmentOnly`, the `bootTestRun` task will not pick up changes in your code, as the devtools are not active.
|
||||
|
||||
|
||||
|
||||
[[features.testing.utilities]]
|
||||
=== Test Utilities
|
||||
A few test utility classes that are generally useful when testing your application are packaged as part of `spring-boot`.
|
||||
|
@ -13,9 +13,10 @@ Doing so generates a new project structure so that you can <<getting-started#get
|
||||
Check the https://github.com/spring-io/start.spring.io/blob/main/USING.adoc[start.spring.io user guide] for more details.
|
||||
====
|
||||
|
||||
|
||||
|
||||
[[getting-started.first-application.prerequisites]]
|
||||
=== Prerequisites
|
||||
|
||||
Before we begin, open a terminal and run the following commands to ensure that you have a valid version of Java installed:
|
||||
|
||||
[source,shell,indent=0,subs="verbatim"]
|
||||
@ -30,9 +31,9 @@ NOTE: This sample needs to be created in its own directory.
|
||||
Subsequent instructions assume that you have created a suitable directory and that it is your current directory.
|
||||
|
||||
|
||||
|
||||
[[getting-started.first-application.prerequisites.maven]]
|
||||
==== Maven
|
||||
|
||||
If you want to use Maven, ensure that you have Maven installed:
|
||||
|
||||
[source,shell,indent=0,subs="verbatim"]
|
||||
@ -43,30 +44,32 @@ If you want to use Maven, ensure that you have Maven installed:
|
||||
Java version: 17.0.4.1, vendor: BellSoft, runtime: /Users/developer/sdkman/candidates/java/17.0.4.1-librca
|
||||
----
|
||||
|
||||
|
||||
|
||||
[[getting-started.first-application.prerequisites.gradle]]
|
||||
==== Gradle
|
||||
|
||||
If you want to use Gradle, ensure that you have Gradle installed:
|
||||
|
||||
[source,shell,indent=0,subs="verbatim"]
|
||||
----
|
||||
$ gradle --version
|
||||
$ gradle --version
|
||||
|
||||
------------------------------------------------------------
|
||||
Gradle 8.1.1
|
||||
------------------------------------------------------------
|
||||
------------------------------------------------------------
|
||||
Gradle 8.1.1
|
||||
------------------------------------------------------------
|
||||
|
||||
Build time: 2023-04-21 12:31:26 UTC
|
||||
Revision: 1cf537a851c635c364a4214885f8b9798051175b
|
||||
Build time: 2023-04-21 12:31:26 UTC
|
||||
Revision: 1cf537a851c635c364a4214885f8b9798051175b
|
||||
|
||||
Kotlin: 1.8.10
|
||||
Groovy: 3.0.15
|
||||
Ant: Apache Ant(TM) version 1.10.11 compiled on July 10 2021
|
||||
JVM: 17.0.7 (BellSoft 17.0.7+7-LTS)
|
||||
OS: Linux 6.2.12-200.fc37.aarch64 aarch64
|
||||
Kotlin: 1.8.10
|
||||
Groovy: 3.0.15
|
||||
Ant: Apache Ant(TM) version 1.10.11 compiled on July 10 2021
|
||||
JVM: 17.0.7 (BellSoft 17.0.7+7-LTS)
|
||||
OS: Linux 6.2.12-200.fc37.aarch64 aarch64
|
||||
----
|
||||
|
||||
|
||||
|
||||
[[getting-started.first-application.pom]]
|
||||
=== Setting up the project with Maven
|
||||
We need to start by creating a Maven `pom.xml` file.
|
||||
@ -125,35 +128,36 @@ You can test it by running `mvn package` (for now, you can ignore the "`jar will
|
||||
NOTE: At this point, you could import the project into an IDE (most modern Java IDEs include built-in support for Maven).
|
||||
For simplicity, we continue to use a plain text editor for this example.
|
||||
|
||||
|
||||
|
||||
[[getting-started.first-application.gradle]]
|
||||
=== Setting up the project with Gradle
|
||||
|
||||
We need to start by creating a Gradle `build.gradle` file.
|
||||
The `build.gradle` is the build script that is used to build your project.
|
||||
Open your favorite text editor and add the following:
|
||||
|
||||
[source,gradle,indent=0,subs="verbatim,attributes"]
|
||||
----
|
||||
plugins {
|
||||
id 'java'
|
||||
id 'org.springframework.boot' version '{spring-boot-version}'
|
||||
id 'io.spring.dependency-management' version '1.1.0'
|
||||
}
|
||||
plugins {
|
||||
id 'java'
|
||||
id 'org.springframework.boot' version '{spring-boot-version}'
|
||||
id 'io.spring.dependency-management' version '1.1.0'
|
||||
}
|
||||
|
||||
group = 'com.example'
|
||||
version = '0.0.1-SNAPSHOT'
|
||||
sourceCompatibility = '17'
|
||||
group = 'com.example'
|
||||
version = '0.0.1-SNAPSHOT'
|
||||
sourceCompatibility = '17'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
repositories {
|
||||
mavenCentral()
|
||||
ifeval::["{artifact-release-type}" != "release"]
|
||||
maven { url 'https://repo.spring.io/milestone' }
|
||||
maven { url 'https://repo.spring.io/snapshot' }
|
||||
maven { url 'https://repo.spring.io/milestone' }
|
||||
maven { url 'https://repo.spring.io/snapshot' }
|
||||
endif::[]
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
}
|
||||
dependencies {
|
||||
}
|
||||
----
|
||||
|
||||
The preceding listing should give you a working build.
|
||||
@ -162,15 +166,17 @@ You can test it by running `gradle classes`.
|
||||
NOTE: At this point, you could import the project into an IDE (most modern Java IDEs include built-in support for Gradle).
|
||||
For simplicity, we continue to use a plain text editor for this example.
|
||||
|
||||
|
||||
|
||||
[[getting-started.first-application.dependencies]]
|
||||
=== Adding Classpath Dependencies
|
||||
|
||||
Spring Boot provides a number of "`Starters`" that let you add jars to your classpath.
|
||||
"`Starters`" provide dependencies that you are likely to need when developing a specific type of application.
|
||||
|
||||
|
||||
|
||||
[[getting-started.first-application.dependencies.maven]]
|
||||
==== Maven
|
||||
|
||||
Most Spring Boot applications use the `spring-boot-starter-parent` in the `parent` section of the POM.
|
||||
The `spring-boot-starter-parent` is a special starter that provides useful Maven defaults.
|
||||
It also provides a <<using#using.build-systems.dependency-management,`dependency-management`>> section so that you can omit `version` tags for "`blessed`" dependencies.
|
||||
@ -201,9 +207,10 @@ To add the necessary dependencies, edit your `pom.xml` and add the `spring-boot-
|
||||
|
||||
If you run `mvn dependency:tree` again, you see that there are now a number of additional dependencies, including the Tomcat web server and Spring Boot itself.
|
||||
|
||||
|
||||
|
||||
[[getting-started.first-application.dependencies.gradle]]
|
||||
==== Gradle
|
||||
|
||||
Most Spring Boot applications use the `org.springframework.boot` Gradle plugin.
|
||||
This plugin provides useful defaults and Gradle tasks.
|
||||
The `io.spring.dependency-management` Gradle plugin provides <<using#using.build-systems.dependency-management, dependency management>> so that you can omit `version` tags for "`blessed`" dependencies.
|
||||
@ -213,13 +220,13 @@ Before that, we can look at what we currently have by running the following comm
|
||||
|
||||
[source,shell,indent=0,subs="verbatim"]
|
||||
----
|
||||
$ gradle dependencies
|
||||
$ gradle dependencies
|
||||
|
||||
> Task :dependencies
|
||||
> Task :dependencies
|
||||
|
||||
------------------------------------------------------------
|
||||
Root project 'myproject'
|
||||
------------------------------------------------------------
|
||||
------------------------------------------------------------
|
||||
Root project 'myproject'
|
||||
------------------------------------------------------------
|
||||
----
|
||||
|
||||
The `gradle dependencies` command prints a tree representation of your project dependencies.
|
||||
@ -228,14 +235,15 @@ To add the necessary dependencies, edit your `build.gradle` and add the `spring-
|
||||
|
||||
[source,gradle,indent=0,subs="verbatim"]
|
||||
----
|
||||
dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
}
|
||||
dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
}
|
||||
----
|
||||
|
||||
If you run `gradle dependencies` again, you see that there are now a number of additional dependencies, including the Tomcat web server and Spring Boot itself.
|
||||
|
||||
|
||||
|
||||
[[getting-started.first-application.code]]
|
||||
=== Writing the Code
|
||||
To finish our application, we need to create a single Java file.
|
||||
@ -296,6 +304,8 @@ The `args` array is also passed through to expose any command-line arguments.
|
||||
[[getting-started.first-application.run]]
|
||||
=== Running the Example
|
||||
|
||||
|
||||
|
||||
[[getting-started.first-application.run.maven]]
|
||||
==== Maven
|
||||
At this point, your application should work.
|
||||
@ -329,9 +339,10 @@ If you open a web browser to `http://localhost:8080`, you should see the followi
|
||||
|
||||
To gracefully exit the application, press `ctrl-c`.
|
||||
|
||||
|
||||
|
||||
[[getting-started.first-application.run.gradle]]
|
||||
==== Gradle
|
||||
|
||||
At this point, your application should work.
|
||||
Since you used the `org.springframework.boot` Gradle plugin, you have a useful `bootRun` goal that you can use to start the application.
|
||||
Type `gradle bootRun` from the root project directory to start the application.
|
||||
@ -364,6 +375,7 @@ If you open a web browser to `http://localhost:8080`, you should see the followi
|
||||
To gracefully exit the application, press `ctrl-c`.
|
||||
|
||||
|
||||
|
||||
[[getting-started.first-application.executable-jar]]
|
||||
=== Creating an Executable Jar
|
||||
We finish our example by creating a completely self-contained executable jar file that we could run in production.
|
||||
@ -382,9 +394,10 @@ It can also be problematic if the same filename is used (but with different cont
|
||||
Spring Boot takes a <<executable-jar#appendix.executable-jar, different approach>> and lets you actually nest jars directly.
|
||||
****
|
||||
|
||||
|
||||
|
||||
[[getting-started.first-application.executable-jar.maven]]
|
||||
==== Maven
|
||||
|
||||
To create an executable jar, we need to add the `spring-boot-maven-plugin` to our `pom.xml`.
|
||||
To do so, insert the following lines just below the `dependencies` section:
|
||||
|
||||
@ -458,9 +471,10 @@ To run that application, use the `java -jar` command, as follows:
|
||||
|
||||
As before, to exit the application, press `ctrl-c`.
|
||||
|
||||
|
||||
|
||||
[[getting-started.first-application.executable-jar.gradle]]
|
||||
==== Gradle
|
||||
|
||||
To create an executable jar, we need to run `gradle bootJar` from the command line, as follows:
|
||||
|
||||
[source,shell,indent=0,subs="verbatim,attributes"]
|
||||
|
@ -28,6 +28,7 @@ import org.gradle.api.Plugin;
|
||||
import org.gradle.api.Project;
|
||||
import org.gradle.api.artifacts.Configuration;
|
||||
import org.gradle.api.file.FileCollection;
|
||||
import org.gradle.api.java.archives.Manifest;
|
||||
import org.gradle.api.plugins.ExtensionAware;
|
||||
import org.gradle.api.plugins.JavaPlugin;
|
||||
import org.gradle.api.plugins.JavaPluginExtension;
|
||||
@ -122,8 +123,15 @@ class NativeImagePluginAction implements PluginApplicationAction {
|
||||
private void configureJarManifestNativeAttribute(Project project) {
|
||||
project.getTasks()
|
||||
.named(SpringBootPlugin.BOOT_JAR_TASK_NAME, BootJar.class)
|
||||
.configure((bootJar) -> bootJar
|
||||
.manifest(((manifest) -> manifest.getAttributes().put("Spring-Boot-Native-Processed", true))));
|
||||
.configure(this::addNativeProcessedAttribute);
|
||||
}
|
||||
|
||||
private void addNativeProcessedAttribute(BootJar bootJar) {
|
||||
bootJar.manifest(this::addNativeProcessedAttribute);
|
||||
}
|
||||
|
||||
private void addNativeProcessedAttribute(Manifest manifest) {
|
||||
manifest.getAttributes().put("Spring-Boot-Native-Processed", true);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user