In what scenario would you use the @Modifying annotation in a Spring Data JPA repository method?
- When creating a new entity instance in the repository.
- When performing a read operation on an entity.
- When executing a non-selecting (e.g., UPDATE or DELETE) query.
- When retrieving a collection of entities.
The @Modifying annotation is used in a Spring Data JPA repository method when you want to execute a non-selecting query, such as an UPDATE or DELETE operation, on the database. This annotation informs Spring that the method will modify the database, allowing it to manage the transaction appropriately. The other options are not suitable scenarios for using @Modifying.
The _____ is a specialized form of the @Component annotation intended to represent the application logic in Spring Boot.
- @Repository
- @Controller
- @Service
- @Configuration
In Spring Boot, the "@Service" annotation is a specialized form of the "@Component" annotation used to represent the application's business logic. It helps Spring identify the class as a service component, allowing it to be automatically detected and used within the application context. The other options, such as "@Repository," "@Controller," and "@Configuration," serve different purposes and are not specifically intended for application logic in the same way as "@Service."
Which Spring Boot Actuator endpoint is specifically used for exposing application metrics?
- /env
- /health
- /info
- /metrics
The /metrics endpoint in Spring Boot Actuator is specifically used for exposing application metrics. This endpoint provides valuable information about your application's performance, such as memory usage, garbage collection statistics, and custom metrics you can define. Monitoring these metrics is crucial for ensuring the health and performance of your Spring Boot application.
How can you map application-specific exceptions to HTTP status codes in a Spring Boot application?
- Using the @ResponseStatus annotation in custom exception classes.
- Modifying the application.properties file to specify exception-to-status code mappings.
- Creating custom HTTP error responses for each exception type.
- Wrapping exceptions in RuntimeExceptions and relying on Spring Boot defaults.
In a Spring Boot application, you can map application-specific exceptions to HTTP status codes using the @ResponseStatus annotation in custom exception classes. This allows you to define the specific HTTP status code to return when a particular exception is thrown, providing fine-grained control over error responses. The other options are not standard practices for mapping exceptions to HTTP status codes in Spring Boot.
Which annotation is used to secure methods in Spring Security?
- @Authorize
- @PreAuthorize
- @Secure
- @Secured
The correct annotation to secure methods in Spring Security is @Secured. This annotation allows you to specify which roles or authorities are required to access a particular method.
For testing the persistence layer in Spring Boot, the _____ annotation is used to test slicing the application context and loading only relevant beans related to data JPA.
- @DataJpaTest
- @MockBean
- @SpringBootTest
- @WebMvcTest
The @DataJpaTest annotation in Spring Boot is used for testing the persistence layer. It slices the application context and loads only the relevant beans related to data JPA, making it efficient for testing data access operations.
To manually wire a bean, you would use the _____ method of the ApplicationContext in Spring Boot.
- getBean()
- registerBean()
- wireBean()
- fetchBean()
In Spring Boot, to manually wire a bean, you would use the "getBean()" method of the ApplicationContext. This method allows you to retrieve a bean from the Spring container by its name or class. The other options, such as "registerBean()," "wireBean()," and "fetchBean()," do not represent the correct method used for manual bean retrieval and wiring in Spring Boot.
In Spring Boot, the _____ annotation can be used to inject the value of a specific property into a field.
- @Autowired
- @Inject
- @Property
- @Value
In Spring Boot, you can use the @Value annotation to inject the value of a specific property into a field. This annotation allows you to inject property values from your application.properties or application.yml file directly into your Spring components, making it convenient to access configuration properties within your application code.
Consider a scenario where you are tasked with performing integration tests on a Spring Boot application consisting of multiple microservices. How would you approach testing interactions between these microservices while isolating external dependencies?
- Use a mocking framework like Mockito to simulate external dependencies.
- Deploy the entire microservices architecture for testing.
- Disable external dependencies during testing.
- Create custom stubs for external services.
Option 1 is the most common approach. Mockito is a popular Java mocking framework that allows you to mock external dependencies, isolating the microservice being tested.
To represent an asynchronous computation result in Spring Boot reactive programming, the _____ class is used.
- AsyncTask
- CompletableFuture
- DeferredResult
- Future
In Spring Boot reactive programming, the DeferredResult class is used to represent an asynchronous computation result. It allows a controller to start processing a request and return a DeferredResult immediately, deferring the actual result processing to a later time. This is useful for handling long-running or asynchronous tasks in a non-blocking manner.