DEV Community

Cover image for Cognitive Control Annotations in Spring Boot

Posted on

Cognitive Control Annotations in Spring Boot

Cognitive Control Annotations in Spring boot allows to annotation(s) with appropriate dependency injection.

The article mainly focus on Cognitive Control of annotation(s). Especially, @Annotation (@-at using widely in annotation in Spring), and practical implementation in real project. Indeed, you may use free open source here github.
Just click it

Indeed, we’ll explore list of annotations in Spring in down below.

@SpringBootApplication (see Figure-1. Annotation in Spring Boot)

  • Annotation indicates a configuration class that declares one or more @Bean methods, and also trigger auto-configuration and component scanning.
  • This is a convenience annotation that is equivalent to declaring @Configuration, @EnableAutoConfiguration, and @ComponentScan.

Alt Text
Figure-1. Annotation in Spring Boot.

More information in the annotation below in that side.

  • @EnableAutoConfiguration - enable Spring Boot's auto-configuration mechanism
  • @CompoenetScan - enable @Component scan on the package where the application is located.
  • @Configuration - allow to register extra beans in the context or import additional configuration classes.

In addition, we will annotate a method of a bean with @Async will make it execute in a separate thread i.e. the caller will not wait for the completion of the called method. (see Figure-2).
If you have been already working on Spring or Spring Boot Application and you have a requirement to use an Asynchronous mechanism, then these below three quick steps will help to set up.

Alt Text
Figure-2. Async Annotation to a method.

Note that Spring uses the Jackson JSON library to convert GitHub’s JSON response into a User object. The @JsonIgnoreProperties annotation signals Spring to ignore any attributes not listed in the class. This makes it easy to make REST calls and produce domain objects.
In this article, we are only grabbing the name and the blog URL for demonstration purposes.

Alt Text
Figure-3 Architecture Structure of Class. (see more code in github)

What we will build in here?

Indeed, We’ll build a lookup service that queries GitHub user information and retrieves data through GitHub’s API. One approach to scaling services is to run expensive jobs in the background and wait for the results using Java’s CompletableFuture interface. Java’s CompletableFuture is an evolution of the regular Future. It makes it easy to pipeline multiple asynchronous operations merging them into a single asynchronous computation.

The GitHubLookupService class uses Spring’s RestTemplate to invoke a remote REST point (, and then convert the answer into a User object. Spring Boot automatically provides a RestTemplateBuilder that customizes the defaults with any auto-configuration bits (i.e. MessageConverter).
The findUser method is flagged with Spring’s @Async annotation, indicating it will run on a separate thread. The method’s return type is CompletableFuture instead of User, a requirement for any asynchronous service. This code uses the completedFuture method to return a CompletableFuture instance which is already completed with a result of the GitHub query.

Running Application

  • Two ways we can start the standalone Spring boot application. We are using maven so just run the application using ./mvnw spring-boot:run. Or you can build the JAR file with ./mvnw clean package. Then you can run the JAR file:
  • Below diagram shows and how to run spring boot application from IDE - right click - run as - SpringbootAsyncApplication.main() method as a standalone Java class.

In addition, a list all annotation and brief explanation:

@Qualifier("laptop") this may use component regarding laptop name... (ex>= 
@Data ---> at the class level is provided by Lombok and tells Lombok to 
generate all of those missing methods. 
@ManyToMany (targetEntity=Ingredient.class_)
@NoArgsConstructor ( access=AccessLevel.PRIVATE, force=true)
@Slf4j - Simple Logging Facade for Java. Lombok's @Slf4j annotation to create 
a free SLF4J Logger object at runtime.
@RunWith(SpringRunner.class)---> prividing a test runner that guides JUnit in 
running a test. Plugin to JUnit to provide custom.
-- ----------------------
@WebMvcTest  - set up Spring support for testing Spring MVC. Although it 
could be made to start a server, mocking the mechanics of Spring MVC.

Likewise, you may use API github, just free use it. GithubAPI

Top comments (0)