Spring @Bean Annotation is applied on a method to specify that it returns a bean to be managed by Spring context. Spring Bean annotation is usually declared in Configuration classes methods. In this case, bean methods may reference other @Bean methods in the same class by calling them directly.
Let’s say we have a simple class as below.
package com.journaldev.spring;
public class MyDAOBean {
@Override
public String toString() {
return "MyDAOBean"+this.hashCode();
}
}
Here is a Configuration class where we have defined a @Bean method for MyDAOBean
class.
package com.journaldev.spring;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyAppConfiguration {
@Bean
public MyDAOBean getMyDAOBean() {
return new MyDAOBean();
}
}
We can get MyDAOBean
bean from Spring context using below code snippet.
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.scan("com.journaldev.spring");
context.refresh();
//Getting Bean by Class
MyDAOBean myDAOBean = context.getBean(MyDAOBean.class);
We can specify the @Bean name and use it to get them from spring context. Let’s say we have MyFileSystemBean
class defined as:
package com.journaldev.spring;
public class MyFileSystemBean {
@Override
public String toString() {
return "MyFileSystemBean"+this.hashCode();
}
public void init() {
System.out.println("init method called");
}
public void destroy() {
System.out.println("destroy method called");
}
}
Now define a @Bean method in the configuration class:
@Bean(name= {"getMyFileSystemBean","MyFileSystemBean"})
public MyFileSystemBean getMyFileSystemBean() {
return new MyFileSystemBean();
}
We can get this bean from context by using the bean name.
MyFileSystemBean myFileSystemBean = (MyFileSystemBean) context.getBean("getMyFileSystemBean");
MyFileSystemBean myFileSystemBean1 = (MyFileSystemBean) context.getBean("MyFileSystemBean");
We can also specify spring bean init method and destroy method. These methods are called when spring bean is being created and when the context is being closed respectively.
@Bean(name= {"getMyFileSystemBean","MyFileSystemBean"}, initMethod="init", destroyMethod="destroy")
public MyFileSystemBean getMyFileSystemBean() {
return new MyFileSystemBean();
}
You will notice that “init” method is being called when we invoke the context refresh
method and “destroy” method is called when we invoke context close
method.
Spring @Bean annotation is widely used in annotation-driven spring applications.
You can download the complete spring project from our GitHub Repository.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Is creating a AnnotationConfigApplicationContext the only way to get the bean Which inject with @Bean Annotatiion?
- xunyan
Thanks, nice post
- Binh Thanh Nguyen
Thanks for the article pankaj. does context.getBean(“MyFileSystemBean”) call create new object every time? or is it singleton?
- Hemachandra