Basic Spring Annotations

Defining Components

@Component:It is a basic auto component scan annotation, it indicates annotated class is a auto scan component. It is generic and can be used across application.

@Component has three specializations (@Controller, @Service and @Repository)

@Repository: This annotation designated the bean class as a spring managed component in the persistence layer/DAO layer.

@Service: This annotation designated the bean class as a spring managed component in the business layer. It notify the spring that the class contains the business logic.

@Controller: This annotations designates the bean class as the controller in the presentation layer (Spring MVC Controller).

@Bean : It is used to explicitly declare a single bean, rather than letting Spring do it automatically using @Component. It decouples the declaration of the bean from the class definition and lets you create and configure beans exactly how you choose.

Also read @Component vs @Bean

Spring Bean Life Cycle


Defined over the bean initialization call back method.

public void init(){
System.out.println(“HelloBean init method called”);


public void destroy(){
System.out.println(“HelloBean destroy method called”);

Defined over the bean destruction call back method.

Please read Spring Bean Life Cycle

Bean scopes

@Scope(“singleton”) – This scopes the bean definition to a single instance per Spring IoC container (default).

@Scope(“prototype”) – A bean with prototype scope will return a different instance every time it is requested from the container.

@Scope(“request”)-This scopes a bean definition to an HTTP request.

@Scope(“session”) – This scopes a bean definition to an HTTP session.


In application scope, container creates one instance per web application runtime. It is almost similar to singleton scope, with only two differences i.e.

application scoped bean is singleton per ServletContext, whereas singleton scoped bean is singleton per ApplicationContext. Please note that there can be multiple application contexts for single application.
application scoped bean is visible as a ServletContext attribute.


WebSocket-scoped beans when first accessed are stored in the WebSocket session attributes. The same instance of the bean is then returned whenever that bean is accessed during the entire WebSocket session.

We can also say that it exhibits singleton behavior but limited to a WebSocket session only.

Scanning Components


@ComponentScan(basePackages = “com.spring.example.bean”)

The @ComponentScan annotation is used to tell Spring the packages to scan for annotated components- @Component, @Controller, @Service, and @Repository


@Configuration: Indicates that a class declares one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime.

Use @Import to load multiple configuration files.

@Import({ Config1.class, Config2.class })
public class AppConfig {


Injecting dependencies
  • @Resource – Defined in the javax.annotation package and this annotation is part of the JSR-250 annotation collection and is packaged with Java EE.
  • @Inject – Defined in the javax.inject package.In order to access the @Inject annotation, the javax.inject library has to be declared as a Maven dependency.
  • @Autowired – Defined in the package org.springframework.bean.factory and part of Spring framework.

Also read @Autowired vs @Inject vs @Resource






Leave a Reply

Your email address will not be published. Required fields are marked *