How do you configure a cache manager in Spring Boot?
- By adding the @Cacheable annotation to methods that should be cached.
- By modifying the 'application.properties' file with cache settings.
- By creating a custom caching class and injecting it into services.
- By disabling caching entirely in the Spring Boot application.
In Spring Boot, cache manager configuration is typically done by modifying the 'application.properties' file with cache-related settings. Spring Boot provides easy-to-use properties for configuring popular caching solutions like EhCache, Caffeine, and more. The other options are not the standard way to configure a cache manager in Spring Boot.
How can you create a custom query method in a Spring Data JPA repository?
- By defining a method with a specific naming convention.
- By using a native SQL query.
- By annotating a method with @Query and providing the JPQL query.
- By creating a new repository interface for custom queries.
In Spring Data JPA, you can create custom query methods by defining a method in your repository interface with a specific naming convention. Spring Data JPA generates the query based on the method name, eliminating the need to write explicit queries. The other options represent alternative ways to create custom queries but are not the typical approach in Spring Data JPA.
In Spring, the _____ annotation is used to indicate that a method should be invoked after the bean has been constructed and injected.
- @PostConstruct
- @Autowired
- @BeanPostProcessor
- @Inject
In Spring, the @PostConstruct annotation is used to indicate that a method should be invoked after the bean has been constructed and injected. It is commonly used for initialization tasks that need to be performed after the bean's dependencies have been injected. The other options, such as @Autowired, @BeanPostProcessor, and @Inject, serve different purposes and are not used for the same scenario.
How can you encrypt and decrypt property values in Spring Boot to secure sensitive information?
- By using the @EncryptProperty annotation.
- By configuring property encryption in application.properties.
- By using the spring.security module for encryption.
- By using the Jasypt library and configuring it in application.properties.
To encrypt and decrypt property values in Spring Boot, you can use the Jasypt library and configure it in the application.properties file. This library provides a straightforward way to secure sensitive information such as database passwords. While there are other security-related options in Spring Boot, the Jasypt library is commonly used for property encryption.
In what scenarios would you choose to implement a custom validator instead of using the standard Bean Validation annotations?
- When you need to perform complex validation logic that can't be expressed using standard annotations.
- When you need to validate simple data types like integers and strings.
- When you want to achieve better performance in your application.
- When you want to minimize the use of custom code in your application.
Custom validators are preferred when complex validation logic is required, which can't be achieved with standard Bean Validation annotations. While standard annotations are suitable for many cases, custom validators are necessary for scenarios where specific and intricate validation rules are needed. Custom validators may increase code complexity but allow for highly tailored validation logic.
How can you use Mockito to verify that a method was called a specific number of times?
- verifyMethod(atLeast(callCount))
- verifyMethod(atMost(callCount))
- verifyMethod(callCount)
- verifyMethod(times(callCount))
In Mockito, you can use verify along with times(callCount) to verify that a method was called a specific number of times. This is useful for testing the behavior of methods.
In a reactive Spring Boot application, _____ is used to handle back pressure in a reactive stream.
- Backpressure
- Flux
- Mono
- Reactor
In a reactive Spring Boot application, Backpressure is used to handle back pressure in a reactive stream. Backpressure is a mechanism that allows a subscriber to signal to a publisher how many items it can consume at a time. This is essential for preventing overload and resource exhaustion in reactive streams when the publisher emits data faster than the subscriber can handle.
How can you customize Ribbon’s load-balancing strategy in a Spring Cloud application?
- By configuring the ribbon.strategy property in application.properties.
- By implementing a custom IRule and configuring it as a bean.
- By using the @LoadBalanced annotation on RestTemplate.
- By setting the ribbon.loadBalancer property in the service configuration.
In a Spring Cloud application, you can customize Ribbon's load-balancing strategy by implementing a custom IRule and configuring it as a bean. Ribbon uses IRule to determine which instance to route a request to. By creating a custom IRule, you can define your own load-balancing logic. The other options are not used to customize Ribbon's load-balancing strategy. The @LoadBalanced annotation is used to enable client-side load balancing with RestTemplate. The ribbon.strategy and ribbon.loadBalancer properties are not used for customizing the load-balancing strategy directly.
Which interface in Spring Boot is used to create custom validators for a class?
- Validator
- Validatable
- ValidationInterface
- SpringValidator
In Spring Boot, the interface used to create custom validators for a class is Validator. You can implement this interface to define custom validation logic for your domain objects. It allows you to specify the conditions under which an object is considered valid. The other options (Validatable, ValidationInterface, and SpringValidator) are not standard Spring Boot interfaces for creating custom validators.
For creating a Spring Boot project, the _____ website provides a user-friendly interface to generate project structure with desired configurations.
- Spring Boot Creator
- Spring Framework
- Spring Generator
- Spring Initializr
To create a Spring Boot project with desired configurations, the Spring Initializr website provides a user-friendly interface. Spring Initializr allows developers to select project settings, dependencies, and configurations, and it generates a project structure accordingly. This simplifies the process of setting up a Spring Boot project.