@Configuration:
Annotation based configuration alternative to xml based configuration .This will be mostly used with @Bean to define bean at run time
for annotation-based configuration, as opposed to xml-based configuration.
@Bean:
define bean at run time, should be under class annotated with @configuration
It is used to explicitly declare a single bean, rather letting spring to do it while classpath scanning.
It decouple declaration from the class definition
When we don't own the class, it might make sense to use @bean wherever its being used
@Component:
Define bean at run time, should be under class annotated with @configuration
Requires @ComponentScan to allow autoscanning
By default, the bean instances of this class have the same name as the class name with a lowercase initial. On top of that, we can specify a different name using the optional value argument of this annotation.
@ConditionalOnMissingBean:
conditional that only matches when the specified bean / with name is not already in the bean container.Typically available in spring boot applciation
@ConditionalOnClass:
Will create bean if com.thetechiehouse.MyServiceClass class exist in the class path.To be used in Spring boot env.
@Bean
@ConditionalOnClass(name ="com.thetechiehouse.MyServiceClass")
@ConditionalOnProperty: @ConditionalOnProperty("app.somekey") // Bean registration hapen only when app.somekey is present
@ConditionalOnProperty(name='com.myproperty', havingValue = "true")
@ConditionalOnProperty(prefix = "notification", name = "service", havingValue = "sms")
//notification.service=sms
@Bean
Create bean only if a specific property value is true.To be used in Spring boot env.
@ConditionalOnExpression("${xxx.YYY.enabled:true}")
similar to @ConditionalOnProperty but it's a bit slower
Uses
//@Resources
@Resource (name="myBean")
//@Bean
@Bean(name = "myBean")
public SomeClass someMethod() {
return new SomeClass();
}
//@ Autowired
@Autowired
or
@Autowired
@Qualifier("myBean")
What are differences among @Autowired vs @Inject vs @Resource
@Resource | javax.annotation |
|
@Inject | javax.inject |
|
@Qualifier | javax.inject |
|
@Autowired | org.springframework.bean.factory |
@Resource:
annotation based on JSR-250. @Resource is quite similar to @Autowired
@Autowired (Spring specific)
@Autowired:
It is fundamentally about type-driven injection ie that a resource by-type,
with optional semantic qualifiers.
The bean name is considered a default qualifier value.
It does have the required field so this can be used if the field is null and required so.
@Inject:
Annotation based on JSR-330
replacement for Spring’s @Autowired annotation
It does not have the required field so if it does not find a suitable object it will fail.
It has @Named similar to qualifier in case of autowired.
@Resource will narrow down the search first by name then by type and finally by Qualifiers (ignored if match is found by name). @Autowired and @Inject will narrow down the search first by type then by qualifier and finally by the name.
Since @Repository, @Service, @Configuration, and @Controller are all meta-annotations of @Component, they share the same bean naming behaviour