Spring Core Annotations
By AmarSivas | | Updated : 2021-05-29 | Viewed : 316 times

As a spring developer, it is required to know all the spring core annotations. I tried to bring all the spring core annotation in one article. I hope these will be useful for every spring developer.
Table of Contents:
Spring DI Annotations
@Bean
With the annotated
@Bean
public ASpringBean aSpringBean() {
return new ASpringBean("ASpringBean");
}
@Bean vs @Component
-
@Bean is method-level annotation whereas @Comoponent is a class-level annotation.
-
@Component annotation will be preferred for bean's auto-detection by using classpath scanning whereas @Bean annotation is certainly used for declaring the single bean explicitly.
-
Unlike @Component, @Bean should be declared in the @Configuraton class. Bean instance logic can be controlled with @Bean whereas it is not possible with @Component.
-
Unlike @Component, @Bean annotation does not allow to wire the dependencies dynamically So dependencies injection should explicitly happen in the case of @Bean.
@Autowired
Spring inject mandatory beans when annotated
@Component
public class ASpringBean {
private String aSpringBeanName;
@Autowired
private BSpringBean bSpringBean;
//other methods
}
@Qualifier
@Component
public class ASpringBean {
private String aSpringBeanName;
@Autowired
@Qualifier("bSpringBean")
private BSpringBean bSpringBean;
//other methods
}
@Required
@Value
Typically used for
@Service
public class CarService {
@Value("${car.brand}")
private String carBrand
}
@DependsOn
It defines the bean on which the current bean depends. This means it describes the bean which is to be created first. So once this bean initiation happens then the current bean\'s initiation is next.
@Component
public class Car {
@Autowired
@DependsOn ("engine")
private Engine engine;
}
@Primary
Spring will give priority to annotated beans When there are multiple beans of the same type for autowiring. So priority will be given to
@Component
public class Audi extends Car {
}
@Primary
@Component
public class Benz extends Car {
}
@Scope
Spring recognizes bean scope when encountering this
@Bean
@Scope("singleton")
public Person aSpringBean() {
return new ASpringBean();
}
Context Configuration Annotations
@Profile
@Configuration
public class AppConfig
{
@Bean
@Profile("DEV")
public DataSource devDataSource() {
}
}
@Import
Indicate that other config classes can also be imported into the current configuration class. Means other component\'s configurations can be used in the current configuration class.
@Import(AppConfig.class)
public class AppMainConfig {
//other annoatated configuration
}
@ImportResource
Indicate that other config-bean.xml can be imported into the current configuration classes.
@Configuration
@ImportResource( { "spring-beans.xml" } )
public class AppMainConfig {
//other annoatated configuration
}
@PropertySource
Using this annotation defined property file can be used in the current config class.
@Configuration
@PropertySource("classpath:/mainapp.properties")
public class AppMainConfig {
}
@PropertySources
using this annotation multiple defined property files can be used in the current config class.
@Configuration @PropertySources({
@PropertySource("classpath:/mainapp1.properties"),
@PropertySource("classpath:/mainapp2.properties") })
public class AppMainConfig {}
@Lookup
Lookup method injection is used to override spring bean definition so that spring will create bean based on Lookup method definition. Spring can not handle a scenario like when a prototype scoped bean injection into singleton scoped bean injection. As you know dependency injection will happen only once for singleton scoped beans. So prototyped scoped bean will be created only once which is Here it is required to create the bean for a prototype for each and every time.
@Component
public class ASpringBean {
@Lookup
public BSpringBean bSpringBean() {
return null;
}
}
@Lazy
Conceptually all
@Lazy
@Configuration
public class SpringAppConfig {
@Bean
public ASpringBean aSpringBean() {
return new ASpringBean("ASpringBean", bSpringBean());
}
@Bean
public BSpringBean bSpringBean() {
return new BSpringBean("BSpringBean");
}
}
Please refer to the Docsconsole GitHub repo for examples.