🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this tutorial, we will learn everything about Spring Boot @Bean annotation with examples.
@Bean Annotation Overview
The @Bean annotation indicates that a method produces a bean to be managed by the Spring container.
The @Bean annotation is usually declared in the Configuration class to create Spring Bean definitions.
YouTube Video - @Bean Annotation
@Bean Annotation Example
Student and Address
class Student{
private Address address;
public Student(Address address){
this.address = address;
}
public void print(){
System.out.println("Student class method called ...");
address.print();
}
}
class Address{
public void print(){
System.out.println("Address class method called ...");
}
}AppConfig Class
@Configuration
class AppConfig{
@Bean
public Address address(){
return new Address();
}
@Bean
public Student student(){
return new Student(address());
}
}Bean Names
By default, the bean name is the same as the method name. We can specify the bean names using @Bean(name = “beanName”).
For example:
@Bean(name = "addressBean")
public Address address(){
return new Address();
}
@Bean(name = "studentBean")
public Student student(){
return new Student(address());
}Define Mulitple Bean Names
@Bean(name = {"studentBean", "studentDemo", "studentComponent"})
public Student student(){
return new Student(address());
}Student student = (Student) applicationContext.getBean("studentBean");Student student = (Student) applicationContext.getBean("studentDemo");Student student = (Student) applicationContext.getBean("studentComponent");@Bean initMethod and destroyMethod attributes
For example, the Student Spring bean has initMethod and destroyMethod attributes:
@Configuration
class AppConfig{
@Bean(name = "addressBean")
public Address address(){
return new Address();
}
@Bean(name = "studentBean", initMethod = "init", destroyMethod = "destroy")
public Student student(){
return new Student(address());
}
}
Next, create init() and destroy() methods in Student class:class Student{
private Address address;
public Student(Address address){
this.address = address;
}
public void print(){
System.out.println("Student class method called ...");
address.print();
}
public void init(){
System.out.println("Intialization logic");
}
public void destroy(){
System.out.println("Destruction logic");
}
}Testing
public class BeanAnnotationDemo {
public static void main(String[] args) {
try(var applicationContext = new AnnotationConfigApplicationContext(AppConfig.class)){
//Student student = applicationContext.getBean(Student.class);
Student student = (Student) applicationContext.getBean("studentBean");
String[] beanNames = applicationContext.getBeanDefinitionNames();
for (String bean: beanNames){
System.out.println(bean);
}
student.print();
}
}
}Complete Code
package com.spring.core.beans;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
class Student{
private Address address;
public Student(Address address){
this.address = address;
}
public void print(){
System.out.println("Student class method called ...");
address.print();
}
public void init(){
System.out.println("Intialization logic");
}
public void destroy(){
System.out.println("Destruction logic");
}
}
class Address{
public void print(){
System.out.println("Address class method called ...");
}
}
@Configuration
class AppConfig{
@Bean(name = "addressBean")
public Address address(){
return new Address();
}
@Bean(name = "studentBean", initMethod = "init", destroyMethod = "destroy")
public Student student(){
return new Student(address());
}
}
public class BeanAnnotationDemo {
public static void main(String[] args) {
try(var applicationContext = new AnnotationConfigApplicationContext(AppConfig.class)){
//Student student = applicationContext.getBean(Student.class);
Student student = (Student) applicationContext.getBean("studentBean");
String[] beanNames = applicationContext.getBeanDefinitionNames();
for (String bean: beanNames){
System.out.println(bean);
}
student.print();
}
}
}Conclusion
Related Spring and Spring Boot Annotations
- Spring Boot @Bean Annotation Example
- Spring @Qualifier Annotation Example
- Spring @Autowired Annotation with Example
- Spring @Bean Annotation with Example
- Spring @Configuration Annotation with Example
- Spring @PropertySource Annotation with Example
- Spring @Import Annotation with Example
- Spring @ImportResource Annotation Example
- Spring - @Lazy Annotation Example
- Spring - @Primary Annotation Example
- Spring @PostConstruct and @PreDestroy Example
- Spring @Repository Annotation
- Spring @Service Annotation
- The Spring @Controller and @RestController Annotations
- Spring Boot @Component, @Controller, @Repository and @Service
- Spring @Scope annotation with Prototype Scope Example
- Spring @Scope annotation with Singleton Scope Example
- Spring Boot @PathVariable
- Spring Boot @ResponseBody
- Spring @RequestBody - Binding Method Parameters to Request Body
- Spring Boot @ResponseStatus Annotation
- Spring Boot - Creating Asynchronous Methods using @Async Annotation
- @SpringBootTest Spring Boot Example
- @SpringBootTest vs @WebMvcTest
- @DataJpaTest Spring Boot Example
- Spring @PostConstruct and @PreDestroy Example
- Spring @GetMapping, @PostMapping, @PutMapping, @DeleteMapping and @PatchMapping
- Spring Boot @EnableAutoConfiguration Annotation with Example
- Spring Boot @SpringBootApplication Annotation with Example
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment