Core Spring Boot Annotations Flashcards

1
Q

@Required

A

The @Required annotation is applied on the setter method of the bean file. Use of this annotation indicates that the annotated bean must be populated at configuration time with the required property. Otherwise, it will throw a BeanInitializationException.

@Required
public void setId(Integer id) {
this.id = id;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

@Autowired

A

The @Autowired annotation is applied on fields, instance variables, setter methods, and constructors. It provides auto wiring to bean properties. When we apply the @autowire to a field, Spring contain will automatically assign the fields with assigned values. We can also use this annotation on private fields.
@Autowired
private Person p;

When we use @Autowired on setter methods, Spring will try to perform the by Type autowiring on the method. It means we are instructing Spring that it should initiate this property using the setter method where you can add your custom code, such as initializing any other property with this property.
@Autowired
public void setPerson (Person person) {
this.person=person;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

@Configuration

A

In Spring Boot, the @Configuration annotation is used to indicate that a class provides one or more bean definitions for the Spring IoC (Inversion of Control) container. When a class is annotated with @Configuration, Spring treats it as a configuration class, similar to an XML configuration file, but in Java. This allows developers to define beans and their dependencies programmatically.

Any method ()annotated with @Bean)within a class annotated with @Configuration will define a bean in the Spring context. For example, when a method is annotated with @Bean, Spring will register the method’s return value as a Spring bean.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

@ComponentScan

A

In Spring Boot, @ComponentScan is an annotation that tells the Spring framework where to look for components (i.e., classes annotated with @Component, @Service, @Repository, and @Controller) and register them as beans in the application context.

By default, if you annotate your main application class (the one annotated with @SpringBootApplication) with @ComponentScan (or rely on @SpringBootApplication itself, which implicitly includes @ComponentScan), it will scan for components in the same package and all sub-packages.

You can specify other packages by passing them as arguments to @ComponentScan, like @ComponentScan(“com.example.app.services”), if you want to include classes outside the main application’s package.

You can use @ComponentScan.Filter along with includeFilters and excludeFilters to selectively scan specific classes or exclude unwanted ones.

AppConfig is a configuration class annotated with @ComponentScan, instructing Spring to scan the com.example.app.services package and all its sub-packages for any Spring components

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

@Bean

A

@Bean is applied to a method, and Spring registers the return value of this method as a bean in the application context.
@Bean allows you to explicitly define beans within Java configuration, which is particularly useful for registering beans from third-party libraries or classes that you don’t want to annotate with @Component, @Service, or other stereotype annotations.

@Configuration
public class AppConfig {

@Bean
public MyService myService() {
    return new MyServiceImpl();
}

@Bean
public MyRepository myRepository() {
    return new MyRepositoryImpl();
} }

beans registered are myService and myRepository

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

@Qualifier

A

The @Qualifier annotation in Spring is used to resolve the ambiguity that arises when multiple beans of the same type exist in the application context. By specifying @Qualifier, you can indicate which specific bean should be injected when there are several beans of the same type, avoiding NoUniqueBeanDefinitionException.

@Service(“creditCardPaymentService”)
public class CreditCardPaymentService implements PaymentService {

@Service(“paypalPaymentService”)
public class PaypalPaymentService implements PaymentService {

public PaymentProcessor(@Qualifier(“creditCardPaymentService”) PaymentService paymentService) {

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

@Lazy

A

When the @Lazy annotation is used on a @Configuration class; it will initialize all the @Bean methods lazily.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

@Value

A

The @Value annotation allows you to inject a value directly into a field, method, or constructor by specifying a property key or literal. Spring resolves this value from property sources such as: application.properties or application.yml files
@Value(“${app.name}”)
private String appName;
app.name=MySpringApp
app.version=1.0

@Value(“${JAVA_HOME}”) for environment variables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly