Many Spring Boot developers want their applications to use auto-configuration, component scanning, and the ability to
define additional configuration in their "application class". To activate these three functions, you can use a
single annotation @SpringBootApplication
:
-
@EnableAutoConfiguration
: Enables the Spring Boot autoconfiguration mechanism -
@ComponentScan
: Enables scanning of the@Component
annotation for the package in which the application is located -
@SpringBootConfiguration
: Allows you to register additional beans in the context or import additional configuration classes. An alternative to the standard@Configuration
annotation from Spring that helps discover configurations in your integration tests.
// Same as @SpringBootConfiguration, @EnableAutoConfiguration, @ComponentScan
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
// same as @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan
@SpringBootApplication
class MyApplication
fun main(args: Array<String>) {
runApplication<MyApplication>(*args)
}
@SpringBootApplication
also provides aliases for configuring the
@EnableAutoConfiguration
and @ComponentScan
.
None of these functions are required, and you can replace this single annotation with any of the functions that it enables. For example, you can choose not to use component scanning or configuration property scanning in your application:
@SpringBootConfiguration(proxyBeanMethods = false)
@EnableAutoConfiguration
@Import({ SomeConfiguration.class, AnotherConfiguration.class })
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
@SpringBootConfiguration(proxyBeanMethods = false)
@EnableAutoConfiguration
@Import(SomeConfiguration::class, AnotherConfiguration::class)
class MyApplication
fun main(args: Array<String>) {
runApplication<MyApplication>(*args)
}
In this example, MyApplication
is the same as any other Spring Boot application, except that the
classes are annotated with @Component
and @ConfigurationProperties
are not detected
automatically, and custom beans are imported explicitly (see the @Import
annotation).
Running your application
One of the biggest benefits of packaging your application as a jar file and using the built-in HTTP server is that you can run your application just like any other. This sample is used to debug applications in Spring Boot. You don't need any special plugins or extensions for the IDE.
Execution from IDE
You can run a Spring Boot application from your IDE as a Java application. However, you must first import the
project. The import steps depend on your IDE and build system. Most IDEs are capable of importing Maven projects
directly. For example, Eclipse users can select Import...
→ Existing Maven Projects
from
the File
menu.
If you can't directly import your project into the IDE, you can generate IDE metadata using the build plugin. Maven includes plugins for Eclipse and IDEA. Gradle provides plugins for various IDEs.
Relaunch
button rather than the Run
button
to ensure that any existing instance is closed.
Execution as a packaged application
If you are using the Spring Boot plugins for Maven or Gradle to create an executable jar file, you can run your
application using the java -jar
command, as shown in the following example:
$ java -jar target/myapplication-0.0.1-SNAPSHOT.jar
You can also run a packaged application with remote debugging enabled. This allows you to attach a debugger to a packaged application, as shown in the following example:
$ java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n \
-jar target/myapplication-0.0.1-SNAPSHOT.jar
Using the Maven plugin
The Spring Boot Maven plugin includes a run
target that you can use to quickly compile and run your
application. Applications run in disassembled form, just like in your IDE. The following example shows a typical
Maven command to run a Spring Boot application:
$ mvn spring-boot:run
You can also use the operating system environment variable MAVEN_OPTS
, as shown in the following
example:
$ export MAVEN_OPTS=-Xmx1024m
Using the Gradle plugin
The Spring Boot Gradle plugin also contains a bootRun
task that can be used to run the application out
of the box. The bootRun
task is added whenever you use the org.springframework.boot
and
java
plugins, and is shown in the following example:
$ gradle bootRun
You can also use the operating system environment variable JAVA_OPTS
, as shown in the following example:
$ export JAVA_OPTS=-Xmx1024m
Hot swap
Since Spring Boot applications are regular Java applications, JVM hot swapping should work out of the box. Hot swapping in the JVM is somewhat limited in the bytecode it can replace. As a more complete solution, you can use JRebel.
The spring-boot-devtools
module also contains support for quickly restarting applications.
GO TO FULL VERSION