spring2019년 12월 4일2 min read

Q&A Spring Boot Questions Collection

A collection of Q&A notes on Spring Boot, including configuration, Quartz, and testing.

FFrank Advenoh
#Q&A#faq#spring
Contents · 1

This is material where I personally jot down things I don't know and briefly organize the parts I come to understand. If you know any of the unanswered ones, please leave a comment. Thank you.

Full Q&A List

[Answered]

1. application.properties : What does the server.compression.enabled property mean?

In Spring Boot, GZip compression is disabled by default. However, if you set server.compression.enabled=true, web resources (e.g. html, css) are compressed and sent to the client, which has the advantage of reducing response time.

Reference

2. What does the @PersistJobDataAfterExecution annotation mean in Quartz?

If you modify JobDataMap data in the Job logic, it is not saved to the DB after execution. However, if you add the @PersistJobDataAfterExecution annotation to the class, the data you modified in the JobDataMap is reflected, and you can read the updated data on the next execution as well.

@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class StatefulDumbJob implements Job {

    public static final String NUM_EXECUTIONS = "NumExecutions";
    public static final String EXECUTION_DELAY = "ExecutionDelay";

    public StatefulDumbJob() {
    }
    public void execute(JobExecutionContext context)
        throws JobExecutionException {
        System.err.println("---" + context.getJobDetail().getKey()
                + " executing.[" + new Date() + "]");

        JobDataMap map = context.getJobDetail().getJobDataMap();

        int executeCount = 0;
        if (map.containsKey(NUM_EXECUTIONS)) {
            executeCount = map.getInt(NUM_EXECUTIONS);
        }

        executeCount++;
        map.put(NUM_EXECUTIONS, executeCount); //save it back into the JobDataMap

        long delay = 5000l;
        if (map.containsKey(EXECUTION_DELAY)) {
            delay = map.getLong(EXECUTION_DELAY);
        }

        try {
            Thread.sleep(delay);
        } catch (Exception ignore) {
        }

        System.err.println("  -" + context.getJobDetail().getKey()
                + " complete (" + executeCount + ").");
    }
}

3. What is the difference between @SpringBootTest and @DataJpaTest?

  • @SpringBootTest

    • can read all beans for testing
    • runs slowly
  • @DataJpaTest

    • scans only @Entity and @Repository and sets them as beans
    • does not scan @Configuration, @Component, or @Service.
    • uses an in-memory database to perform tests
    • already includes @Transactional

@SpringBootTest

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@BootstrapWith(SpringBootTestContextBootstrapper.class)
@ExtendWith({SpringExtension.class})
public @interface SpringBootTest {

@DataJpaTest

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@BootstrapWith(DataJpaTestContextBootstrapper.class)
@ExtendWith({SpringExtension.class})
@OverrideAutoConfiguration(
    enabled = false
)
@TypeExcludeFilters({DataJpaTypeExcludeFilter.class})
@Transactional
@AutoConfigureCache
@AutoConfigureDataJpa
@AutoConfigureTestDatabase
@AutoConfigureTestEntityManager
@ImportAutoConfiguration
public @interface DataJpaTest {

Reference


[Unanswered Questions]

관련 글