To create conditional beans within custom Auto Configuration, you can use the @_____ annotation with a specific condition.
- ConditionalOnBean
- ConditionalOnClass
- ConditionalOnMethod
- ConditionalOnProperty
To create conditional beans within custom Auto Configuration, you can use the @ConditionalOnClass annotation. This annotation allows you to specify that a particular bean should be created only if a specified class is present in the classpath. It's useful for scenarios where you want to conditionally configure beans based on the availability of certain classes.
To perform unit testing on the web layer of a Spring Boot application without loading the complete context, use the _____ annotation.
- @SpringBootTest
- @UnitTest
- @WebLayerTest
- @WebMvcTest
To perform unit testing on the web layer of a Spring Boot application without loading the complete context, you should use the @WebMvcTest annotation. This annotation focuses only on the web layer and is suitable for testing controllers.
In a Spring Boot application, the _____ annotation is used to demarcate transaction boundaries.
- @Autowired
- @Component
- @Service
- @Transactional
In a Spring Boot application, the @Transactional annotation is used to demarcate transaction boundaries. It is applied to methods, indicating that the method should be wrapped in a transaction, ensuring that either all operations within the method succeed or none of them do. This is crucial for maintaining data consistency in the database.
How does the @PreAuthorize annotation in Spring Security differ from the @Secured annotation in terms of the conditions that can be applied?
- @PreAuthorize allows for complex SpEL (Spring Expression Language) expressions for fine-grained control
- @PreAuthorize is deprecated, and @Secured should be used
- @PreAuthorize only works with roles while @Secured allows for custom conditions
- @Secured is more powerful than @PreAuthorize
The @PreAuthorize annotation in Spring Security allows for complex SpEL expressions to define fine-grained access control conditions. This means you can use expressions involving multiple variables and logic, making it more versatile than @Secured, which primarily works with roles.
How can you optimize the JVM (Java Virtual Machine) for a Spring Boot application?
- By disabling garbage collection.
- By minimizing the heap size.
- By tuning garbage collection settings.
- By using a custom class loader.
To optimize the JVM for a Spring Boot application, you should tune garbage collection settings. Garbage collection optimization is crucial because it helps manage memory efficiently. You can adjust parameters like heap size, garbage collection algorithms, and pause times to match your application's needs. Disabling garbage collection is not a practical solution as it will lead to memory issues. Minimizing the heap size without considering application requirements can result in performance problems. Using a custom class loader may have specific use cases but isn't a general JVM optimization technique.
In Spring Boot, the _____ annotation is used to conditionally enable a configuration based on the presence of a specific property.
- @ComponentScan
- @ConditionalOnProperty
- @ConfigurationProperties
- @EnableAutoConfiguration
In Spring Boot, the "@ConditionalOnProperty" annotation is used to conditionally enable a configuration based on the presence of a specific property. This annotation allows you to configure components or beans based on the values of properties, making it a powerful tool for conditional configuration in your application.
Which of the following is true about the deleteById method of a JpaRepository?
- It deletes an entity by its primary key.
- It marks the entity as deleted but does not remove it from the database.
- It deletes all entities in the repository.
- It deletes an entity based on a custom query.
The deleteById method of a JpaRepository deletes an entity from the database by its primary key. It is a convenient method for removing specific entities based on their unique identifier. The other options do not accurately describe the behavior of this method; in particular, it does not mark the entity as deleted without removing it from the database.
Which annotation is used in Spring Security to secure methods based on role-based conditions?
- @Permission
- @PostAuthorize
- @PreAuthorize
- @Secured
The @Secured annotation in Spring Security is used to secure methods based on role-based conditions. You can specify the required roles in the annotation, and only users with those roles can access the method.
In a Spring Boot application, how would you secure microservices using OAuth2 and JWT?
- It's not possible to secure microservices using OAuth2 and JWT in Spring Boot.
- Secure each microservice individually by implementing OAuth2 and JWT security for each service, and use a centralized authentication server for token validation.
- Secure microservices individually with OAuth2 and use API keys for JWT-based authentication.
- Use a single OAuth2 authentication server to issue JWT tokens and secure all microservices with the same token.
In a Spring Boot application, to secure microservices using OAuth2 and JWT, the best practice is to secure each microservice individually. Each microservice should implement OAuth2 and JWT security, and a centralized authentication server can be used for token validation. This approach ensures that each microservice has its own security context and can enforce its own authorization rules. Securing all microservices with a single token is not recommended, as it can lead to security vulnerabilities if one microservice is compromised.
What is the primary purpose of the @SpringBootApplication annotation in a Spring Boot application?
- To allow configuration classes.
- To define a main method.
- To enable Spring MVC support.
- To enable component scanning.
The @SpringBootApplication annotation in Spring Boot is a convenience annotation that adds all of the following: @Configuration, @EnableAutoConfiguration, and @ComponentScan. Thus, it enables component scanning, allowing Spring to automatically discover and register beans. This is crucial for allowing the Spring context to be aware of all the components, services, repositories, etc. available in the project.