Difference Between @Configuration and @Component, @ComponentScan | Spring Annotation Tutorial

preview_player
Показать описание
#Configuration vs #ComponentScan vs #ComponentScan

Instagram: techtalk_debu

if you like my video, please subscribe to my channel and share the video

#The main purpose of @Configuration annotation is to avoid XML based config.
#Component is Stereotype annotation.

(beans ...)
... other configuration ...
(/beans)
and:

@Configuration
public class RootConfig {
... other configuration ...
}

===================

@Configuration
public static class Config {

@Bean
public SimpleBean simpleBean() {
return new SimpleBean();
}

@Bean
public SimpleBeanConsumer simpleBeanConsumer() {
return new SimpleBeanConsumer(simpleBean());
}
}
@Component
public static class Config {

@Bean
public SimpleBean simpleBean() {
return new SimpleBean();
}

@Bean
public SimpleBeanConsumer simpleBeanConsumer() {
return new SimpleBeanConsumer(simpleBean());
}
}
The first piece of code works fine, and as expected, SimpleBeanConsumer will get a link to singleton SimpleBean. But unfortunately, it doesn’t work in a signed enviroment.

The second configuration is totally incorrect because spring will create a singleton bean of SimpleBean, but SimpleBeanConsumer will obtain another instance of SimpleBean which is out of the spring context control.

The reason for this behaviour can be explained as follows:

If you use @Configuration, all methods marked as @Bean will be wrapped into a CGLIB wrapper which works as if it’s the first call of this method, then the original method’s body will be executed and the resulting object will be registered in the spring context. All further calls just return the bean retrieved from the context.

In the second code block above, new SimpleBeanConsumer(simpleBean()) just calls a pure java method. To correct the second code block, we can do something like this:

Thanks & Regards,
Debu Paul
Рекомендации по теме
Комментарии
Автор

Saw this example on stackoverflow but not clear with the concept finding means what ?

swatinaroliya
Автор

Great but you should give practical demo.

sujitkumar
Автор

Quick translation tip. "Ataret" is referring to the @ sign which is usually pronounced "At".

davefordham
Автор

I appreciate your effort. A slight correction though. If you use @Component instead of @Configuration, the simpleBean() call inside the method simpleBeanConsumer() would still create a bean, but the simpleBean will not be a Spring-managed bean. It will only be a simple java bean.

skullwise
Автор

In order for @Component to work : we need to autowire simpleBean Below code will work:

@Component
public static class Config {
@Autowired
SimpleBean simpleBean;

@Bean
public SimpleBean simpleBean() {
return new SimpleBean();
}

@Bean
public SimpleBeanConsumer simpleBeanConsumer() {
return new
}
}

JitendraDas
Автор

Is that mean @Component will not be useful without @Configuration? We have to define the package path of @Component in a @Configuration's bean's ?

imeshdamith
Автор

Great tutorial, helped a lot. Thank you!

Bhartendujoshibj
Автор

Example is really confusing.. please make it clear

rahuldash
Автор

very hard to understand what you are telling, the difference between annotations remained unclear..

arin