Spring Boot

Spring%20Framework%20Docs 2088E9?logo=quickLook&logoColor=white Spring%20Framework%20API%20Docs DF7716?logo=devbox&logoColor=white

Spring%20Boot%20Docs 2088E9?logo=quickLook&logoColor=white Spring%20Boot%20API%20Docs DF7716?logo=devbox&logoColor=white

Maven Dependency

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

Spring Boot Application

@SpringBootApplication

Indicates a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning. This is a convenience annotation that is equivalent to declaring:

⚪ ApplicationContext

Central interface to provide configuration for an application. This is read-only while the application is running, but may be reloaded if the implementation supports this. It provides:

  • Bean factory methods for accessing application components. Inherited from ⚪ ListableBeanFactory.

  • The ability to load file resources in a generic fashion. Inherited from the ⚪ ResourceLoader.

  • The ability to publish events to registered listeners. Inherited from the ⚪ ApplicationEventPublisher.

  • The ability to resolve messages, supporting internationalization. Inherited from the ⚪ MessageSource.

  • Inheritance from a parent context. Definitions in a descendant context will always take priority. This means, for example, that a single parent context can be used by an entire web application, while each servlet has its own child context that is independent of that of any other servlet.

@Autowired

Marks a constructor, setter method, field, or method parameter as to be autowired by Spring container - typically the ⚪ ApplicationContext. See Dependency Injection.

Stereotypes

@Component

Indicates that the annotated class is a component. Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning.

@Controller

Indicates that an annotated class is a "Controller" (for example, a web controller). This annotation serves as a specialization of @Component.

@Service

Indicates that an annotated class is a "Service", originally defined by Domain-Driven Design (Evans, 2003) as "an operation offered as an interface that stands alone in the model, with no encapsulated state". This annotation serves as a specialization of @Component.

@Repository

Indicates that an annotated class is a "Repository", originally defined by Domain-Driven Design (Evans, 2003) as "a mechanism for encapsulating storage, retrieval, and search behavior which emulates a collection of objects". This annotation serves as a specialization of @Component.

Beans

@Primary

Indicates that a bean should be given preference when multiple candidates (e.g. two Services implementing the same interface) are qualified to autowire a single-valued dependency.

@Repository
public class JdbcFooRepository extends FooRepository {
    public JdbcFooRepository(DataSource dataSource) {
        // ...
    }
}

@Primary
@Repository
public class HibernateFooRepository extends FooRepository {
    public HibernateFooRepository(SessionFactory sessionFactory) {
        // ...
    }
}

@Service
public class FooService {
    private final FooRepository fooRepository;

    @Autowired
    public FooService(FooRepository fooRepository) { (1)
        this.fooRepository = fooRepository;
    }
}
1 Because 🟢 HibernateFooRepository is marked with @Primary, it will be injected preferentially over the 🟢 JdbcFooRepository assuming both are present as beans within the same Spring application context, which is often the case when component-scanning is applied liberally.
@Qualifier

May be used on a parameter or field as a qualifier for candidate beans when autowiring. It may also be used to annotate other custom annotations that can then in turn be used as qualifiers.

@Service("bestFooService") (1)
public class FooService {
    private final FooRepository fooRepository;

    @Autowired
    public FooService(@Qualifier("jdbcFooRepository") FooRepository fooRepository) { (2)
        this.fooRepository = fooRepository;
    }
}
1 You can set a custom component name, so later you can use this name in @Qualifier.
2 In this case 🟢 JdbcFooRepository will be injected (notice, it is class name starting lower case).
@Profile

Indicates that a component is eligible for registration when one or more specified profiles are active. You can activate profile with:

A profile string may contain a simple profile name (for example p1) or a profile expression. A profile expression allows for more complicated profile logic to be expressed, for example p1 & p2. See ⚪ Profiles.of(String…​) for more details about supported formats.

If no profile is active using one of those options, a default profile is enabled as a fallback. The name of the default profile is default: @Profile({"EN", "default"}). You can change default profile name with:

CommandLineRunner

⚪ CommandLineRunner is used to indicate that a bean should run when it is contained within a SpringApplication.

Example: use ⚪ CommandLineRunner to populate data for H2 Database during app startup
@Component
public class BootstrapData implements CommandLineRunner {

    @Transactional (1)
    @Override
    public void run(String... args) throws Exception {
        // Populate data for H2 Database using Repositories
    }

}
1 @Transactional exposes a transaction to all data access operations within the current execution thread.