@Required
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;
}
@Autowired
@Autowired tells Spring:
“Find a bean of the required type from the Spring Application Context and inject it here.”
Spring scans your project for classes annotated with @Component, @Service, @Repository, or @Controller, creates their instances (beans), and stores them in the ApplicationContext.
When it encounters an @Autowired field, constructor, or setter, it looks up the matching bean and injects it.
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;
}
@Configuration
@Configuration is a class-level annotation that tells Spring:
“This class contains one or more bean definitions that should be managed by the Spring container.”
When Spring Boot starts:
It detects @Configuration during component scanning.
It instantiates AppConfig.
It calls the @Bean methods once and registers the returned objects in the ApplicationContext.
These beans can then be injected anywhere using @Autowired.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public PaymentService paymentService() {
return new PaymentService();
}
@Bean
public OrderService orderService(PaymentService paymentService) {
return new OrderService(paymentService);
} }@ComponentScan
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
@Bean
@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
@Qualifier
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) {
@Lazy
When the @Lazy annotation is used on a @Configuration class; it will initialize all the @Bean methods lazily.
@Value
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