PrepZone Logo
PrepZone

@SpringBootApplication and Friends

The annotations that bootstrap your app and what each one actually enables.

Why this matters

  • Knowing what each meta-annotation does lets you troubleshoot startup issues — like beans not being found or unwanted auto-config loading.
  • Boot-specific annotations (@ConditionalOn*, @ConfigurationProperties) are the vocabulary for custom starters and library integration.
  • Interview questions often ask what @SpringBootApplication actually contains.
spring-boot-starter-web
spring-boot-starterCore Boot + logging
spring-webmvcControllers and REST
tomcat-embed-coreEmbedded server
jackson-databindJSON serialisation
spring-boot-starter-web pulls in Tomcat, Jackson, Spring MVC and more with tested versions.

Decomposing @SpringBootApplication

Java
@SpringBootApplication
public class BookStoreApplication { ... }

Expands to three annotations:

Java
@SpringBootConfiguration   // alias for @Configuration
@EnableAutoConfiguration   // load auto-config classes from META-INF
@ComponentScan             // scan current package and below

Each serves a distinct purpose in the startup sequence.

Core bootstrap annotations

  • @SpringBootConfiguration — Marks the class as a source of @Bean definitions; typically only on the main class.
  • @EnableAutoConfiguration — Imports AutoConfigurationImportSelector which loads conditional config from META-INF/spring/.
  • @ComponentScan — Discovers @Component, @Service, @Repository, @Controller in the package tree.

Customising the scan root

Java
@SpringBootApplication(scanBasePackages = "com.example.bookstore")
public class BookStoreApplication { ... }

Exclude specific auto-configurations:

Java
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class BookStoreApplication { ... }

Useful when running BookStore without a database in a specific profile.

@Configuration and @Bean

Java
@Configuration
public class WebConfig {

    @Bean
    public FilterRegistrationBean<RequestLoggingFilter> loggingFilter() {
        FilterRegistrationBean<RequestLoggingFilter> bean = new FilterRegistrationBean<>();
        bean.setFilter(new RequestLoggingFilter());
        bean.addUrlPatterns("/api/*");
        return bean;
    }
}

@Configuration classes are processed at startup. @Bean methods register return values in the container. By default, @Bean methods are proxied so repeated calls return the same singleton.

Conditional annotations preview

Boot's auto-config relies on conditions you can use in your own config:

Java
@Configuration
@ConditionalOnProperty(name = "bookstore.cache.enabled", havingValue = "true")
public class CacheConfig {
    @Bean
    public CacheManager cacheManager() {
        return new ConcurrentMapCacheManager("books", "authors");
    }
}

The CacheManager bean exists only when the property is true. The auto-configuration module covers the full @ConditionalOn* family.

@ConfigurationProperties

Java
@ConfigurationProperties(prefix = "bookstore")
@Validated
public record BookStoreProperties(
    @NotNull Pricing pricing,
    @NotNull Catalog catalog
) {
    public record Pricing(String serviceUrl, int timeoutMs) {}
    public record Catalog(int pageSize) {}
}

Enable with @EnableConfigurationProperties(BookStoreProperties.class) or @ConfigurationPropertiesScan.

Web layer annotations

Java
@RestController
@RequestMapping("/api/books")
public class BookController {

    @GetMapping("/{id}")
    public BookResponse get(@PathVariable Long id) { ... }

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public BookResponse create(@RequestBody @Valid CreateBookRequest request) { ... }
}
AnnotationPurpose
@GetMappingMap HTTP GET
@PostMappingMap HTTP POST
@PathVariableBind URI template variable
@RequestBodyDeserialize request body
@RequestParamBind query parameter
@ResponseStatusSet HTTP status on response
  • @GetMapping

    PurposeMap HTTP GET
  • @PostMapping

    PurposeMap HTTP POST
  • @PathVariable

    PurposeBind URI template variable
  • @RequestBody

    PurposeDeserialize request body
  • @RequestParam

    PurposeBind query parameter
  • @ResponseStatus

    PurposeSet HTTP status on response

Transaction and caching annotations

Java
@Service
public class BookService {

    @Cacheable("books")
    public Book findById(Long id) { ... }

    @Transactional
    public Book updatePrice(Long id, BigDecimal price) { ... }
}

@Transactional starts a transaction boundary. @Cacheable stores return values in the configured cache — both are AOP proxies applied by Spring.

Testing annotations

Java
@WebMvcTest(BookController.class)
class BookControllerTest {
    @Autowired MockMvc mockMvc;
    @MockBean BookService bookService;
}

@WebMvcTest loads only the web layer. @MockBean replaces a container bean with a Mockito mock.

Quick recall

Everything you need if you only revisit this box.

  • @SpringBootApplication combines configuration, auto-config, and component scanning.
  • @Configuration + @Bean register objects the container cannot discover via scanning.
  • @ConditionalOnProperty and related annotations gate bean creation on environment state.
  • @ConfigurationProperties binds typed config from application.yml.
  • Web annotations (@GetMapping, @PathVariable) map HTTP to Java methods.
  • @Transactional and @Cacheable are declarative cross-cutting concerns applied via proxies.

Test yourself

Answer these before moving on — recall is what makes it stick.