Basic bootstrap class

package com.example.myapp;

public class Application {

     * A main method to start this application.
    public static void main(String[] args) {, args);


Basic Controller class

public class WebController {

    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(WebController.class);



Spring Boot annotations, and what they mean:

Annotation Use on Description Use for
@Autowired Constructors Injects any dependencies into a Constructor
@Bean Methods Declares a method that returns a bean. Executes the annotated method and registers the return value as a bean within a BeanFactory. Equivalent to <bean...> in XML configuration.
@Component Classes A generic stereotype for any Spring-managed component.
@Controller Classes A stereotype for a controller component in the presentation layer  
@Repository Classes A stereotype for DAO component in the persistence layer
@Service Classes A service component in the business layer  
@SpringBootApplication Class Equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan together


Use this runner:


(SpringRunner is the new name for SpringJUnit4ClassRunner)

Or use this runner for Camel:



Specifying Spring configuration in a test class

@SpringBootTest(classes = { MyApplicationTest.class, MyApplicationTest.TestConfig.class })
public class MyApplicationTest {

    public static class TestConfig {

        RoutesBuilder route() { ..... }

        MyBean myBean() { ..... }


Mixing XML and Java based configuration in a test class

Create a test class named MyApplicationTest:

        classes = MyApplicationTest.Config.class)
public class MyApplicationTest {

    //@ImportResource(value = {"classpath:your-config.xml"})
    static class Config {
        // any further beans can be defined here
        // @Bean ...


If the class is named MyApplicationTest, Spring will look by default for an XML config file in MyApplicationTest’s classpath. In practice, this means for Maven projects, that the maven-resources-plugin should be configured, and then the test Spring XML config file placed in:


The maven-resources-plugin will then copy resources files to target/test-classes, so they’re visible from the compiled classes. Alternatively, you can use the @ImportResource annotation (as commented above) to specify an exact location for the XML file.

NB: Don’t specify @ImportResource if the file is already in the default location, or it will be loaded twice.


Logging can be set at a profile level. For example, to set logging when using a profile named local, add this to = DEBUG

Set logging configuration for a test class like this:

@SpringBootTest(classes = MyApplication.class, properties = {
        " = DEBUG"})
public class MyApplicationTest { ... }

Configuring Logback

spring-boot-starter pulls in Logback (ch.qos.logback:logback-classic) as a dependency.

A sample logback.xml configuration:


    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are assigned the type
             ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>

    <root level="info">
        <appender-ref ref="STDOUT" />

Logback sets the logging level to DEBUG by default, unless it has been configured with a logback.xml on the classpath. This behaviour is detailed in the Logback docs:

Assuming the configuration files logback-test.xml or logback.xml are not present, logback will default to invoking BasicConfigurator which will set up a minimal configuration … Moreover, by default the root logger is assigned the DEBUG level.


Configuration properties cookbook


Change the console log format - e.g. include the full thread name:

logging.pattern.console=%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint} %t %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:-}){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}

Log all SQL statements produced by JdbcTemplate:

Log Spring Transaction events:

Spring Boot 2.x migration

  • The Spring Boot Maven plugin now forks by default since Spring Boot 2.2 (Release Notes).
    • From the docs: “By default, the Gradle and Maven plugins fork the application process.”
    • This means that command line arguments such as will not override application properties, as the Spring Boot app runs in a separate JVM.
    • In Spring Boot 1.x, fork was only enabled “if an agent, jvmArguments or working directory are specified, or if devtools is present.”
    • You can disable forking using
    • Or override properties using:,--other.prop=qux