Difference between @Component and @Bean in Spring

Posted on Updated on

spring-logo

This is also frequently asked interview question for experience Java interview question. Most of Java developer  get confused to deliver the right answer to interviewer. Though Spring also create a lot of confusion about annotation and manual base configuration and other things but good concept and knowledge are always appreciable.

Here, In this section we will discuss difference between @component (@service,@repository and @controller) and @bean annotation.

First of all @component is a generic annotation and it can be used across the application. In enterprises or multi-layer application it is classified into @service,@repository and @controller for more code readable and understandable.

Technically on multi-layer application has application layer, Service layer,business layer and persistence layer. Subsequently annotations are used to represent it.

Different between @Component and @Bean

@Component, @Service, @Repository and @Controller annotations are used for automatic bean detection using classpath scan in Spring framework. Spring container automatically scan and identify all beans annotated by @Component, @Service, @Repository and @Controller.
Service Layer :-

@Service("userService")
public class UserService {

....

}

Persistence Layer :-

@Repository("userDAO")
public class UserDAO {

....

}

Controller / Presentational Layer :-

@Controller("userController")
public class UserController {

....

}

Root component Package defined in spring configuration xml file then spring container scan and initialized all classes present in the classpath coming under the base-package annotated.

<context:component-scan base-package="com.service" />

@Bean annotation is used to explicitly declare a single bean rather than letting spring container do it automatically. It decouples the declaration of bean from bean definition.


public class AuthenticationService {
	
	private static final Logger logger = Logger.getLogger(AuthenticationService.class);
        ...
        ...
}
@Configuration
public class AppConfig {
@Bean(name="authenticationService")
    public AuthenticationService getAuthenticationService(){
    	return new AuthenticationService();
    }
}

 

More stereotype annotation details as below :- 

  • @Component – generic and can be used across application.
  • @Service – annotate classes at service layer level.
  • @Controller – annotate classes at presentation layers level, mainly used in Spring MVC.
  • @Repository – annotate classes at persistence layer, which will act as database repository.

Disable automatic scan of @Component, @Repository, @Service, @Controller:


<beans>
<context:component-scan use-default-filters = "false" base-package="com.service" /> 
</beans>

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s