Spring @Bean Annotation

Spring @Bean annotation is used to declare a single bean explicitly, rather than automatically created by the Spring. It decouples the declaration of the bean from the class definition and lets us create and configure beans exactly how we choose.

The configuration file(file marked with @configuration annotation) is used to declare the bean which is then fetched by the Spring container. Let's understand it by an example. In the AppConfig file, we created a bean called manager by using the @Bean annotation. We did not use any XML file and no <bean> tag to create a bean because the @Bean annotation is will do all itself. Our project contains the following files.

  • BankApp.java
  • AppConfig.java
  • Manager.java
  • Employee.java
  • pom.xml

// AppConfig.java

This is a configuration file in Java which is an alternate of the applicationContext.xml file that we created for the XML-based configuration example. The @Configuration annotation indicates that this is not a simple class but a configuration class and the @ComponentScan annotation is used to indicate the component location in our spring project.

package com.studytonight.community;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

public class AppConfig {
	public Manager manager() {
		return new Manager();


This file contains the code to create an IOC container for our application. The AnnotationConfigApplicationContext class is used to create an object for application context.

package com.studytonight.community;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class BankApp {

	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
		Employee employee = context.getBean("manager",Employee.class);

// Manager.java

This is another component class that is marked using the @Component annotation and implements the Employee interface. In this class, we are implementing a setter dependency injection. See, the Manager class calls a method of Accountant class by using the Accountant class object which is instantiated inside a setter method. See the example below.

package com.studytonight.community;

import org.springframework.stereotype.Component;

public class Manager implements Employee{

	public void work() {
		System.out.println("Manage the branch office");


// Employee.java

This is an interface Employee that contains a doWork() abstract method. Each class that implements this interface will have to override the doWork() method.

package com.studytonight.community;

public interface Employee {
	void work();


// pom.xml

This file contains all the dependencies of this project such as spring jars, servlet jars, etc. Put these dependencies into your project to run the application.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->


Run the Application

After successfully completing the project and adding the dependencies run the application and you will get the output as below.

Manage the branch office