×
☰ See All Chapters

Spring @Bean Annotation

Spring @Bean annotation is used at the method level. When a method is annotated with @Bean annotation, then spring creates a bean for the return value of that method.  When a method is annotated with @Bean then the corresponding class should be annotated with @Configuration. @Bean annotation works with @Configuration to create spring beans.

package com.java4coding;

 

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import com.java4coding.EmployeeService;

 

@Configuration

public class SpringConfiguration {

        @Bean

        public EmployeeService employeeService() {

                return new EmployeeService();

        }

}

By default, the bean name will be same as the method name.

package com.java4coding;

 

import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

 

public class Demo {

  public static void main(String[] args) {

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.java4coding" );

        EmployeeService employeeService = (EmployeeService) context.getBean("employeeService");

        ((ConfigurableApplicationContext) context).close();

  }

}

When your bean has dependency on another bean, you can call respective bean method to inject the dependency.

package com.java4coding;

 

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import com.java4coding.EmployeeService;

 

@Configuration

public class SpringConfiguration {

        @Bean

        public EmployeeService employeeService() {

                return new EmployeeService(employeeBean());

        }

 

        @Bean

        public Employee employeeBean() {

                return new Employee();

        }

}

How to set name for Beans

The name attribute of @Bean annotation can be used to set the name for the bean. Also note that name attribute accepts an array of Strings. This is in order to allow for specifying multiple names (i.e., aliases) for a single bean.

package com.java4coding;

 

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import com.java4coding.EmployeeService;

 

@Configuration

public class SpringConfiguration {

        @Bean(name = { "serviceBean", "empService" })

        public EmployeeService employeeService() {

                return new EmployeeService(employeeBean());

        }

 

        @Bean

        public Employee employeeBean() {

                return new Employee();

        }

}

 

package com.java4coding;

 

import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

 

public class Demo {

        public static void main(String[] args) {

                AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.java4coding");

                EmployeeService serviceBean = (EmployeeService) context.getBean("serviceBean");

                serviceBean.getEmployee().printEmployeeName("Manu M");

               

                EmployeeService empService = (EmployeeService) context.getBean("empService");

                empService.getEmployee().printEmployeeName("Likitha M");

                ((ConfigurableApplicationContext) context).close();

        }

}


All Chapters
Author