Spring – Beans using @Bean

Spring – Beans using @Bean

We have previously discussed an example of Spring configuration using an XML-based configuration. In the previous example, we have defined beans inside the bean-configuration file (applicationContext.xml). To make the code easier and understandable, we are going to use @Bean annotation (annotation-based configuration) for configuring Spring applications.  

@Bean – The @Bean is a method level annotation. It tells the Spring container that the annotated method will return a bean. To define a bean, annotate a method with @Bean annotation. The method name must be the same as the bean name. It also supports several attributes provided by the <bean> tag such as init-method, destroy-method, dependency, and autowiring.

Let’s understand the concept of @Bean annotation through an example.

Example of Spring beans using @Bean annotation

Here, we are going to create an example of Spring beans using @Bean annotation. The Fortune.java and Coach.java are the two interfaces that contain the unimplemented methods, and GoodLuckFortune.java and Cricket_Coach.java are the Java classes that implement the Fortune and Coach interface, respectively. The class AppConfig.java is a Java-based configuration class that contains the bean information.

Following are the steps to create an example of Spring beans using @Bean:

  • Create an Interface

In this step, we are going to create an interface named Fortune.java.

Fortune.java

public interface Fortune {
public String getDailyFortune();
}
  • Create a Java class

In this step, we are going to create a class named GoodLuckFortune.java that implements the Fortune interface.

GoodLuckFortune.java

import org.springframework.stereotype.Component;
@Component
public class GoodLuckFortune implements Fortune{
public String getDailyFortune() {
return "Goodluck For today..!!" ;
}
} 
  • Create another Interface

In this step, we are going to create another interface named Coach.java.

Coach.java

public interface Coach {
public String getDailyTraining();
public String getFortuneService();
} 
  • Create another Java class

In this step, we are going to create another Java class named Cricket_Coach.java that implements the Coach interface.

Cricket_Coach.java

import org.springframework.stereotype.Component;
@Component
public class Cricket_Coach implements Coach {
private Fortune fortuneService;
public Cricket_Coach(Fortune fortuneService) {
this.fortuneService = fortuneService;
}
public String getDailyTraining() { 
return "Daily 5 hours of running..!" ;
}
public String getFortuneService() {
return fortuneService.getDailyFortune() ;
}
} 
  • Create the Configuration class

In this step, we are going to create the configuration class named AppConfig.java.

AppConfig.java

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan( "com.app.SpringBean_usingBean" )
public class AppConfig {
// defining bean for GoodLuckFortune class 
@Bean
public Fortune goodLuckFortune() {
return new GoodLuckFortune();
}
//defining bean for Cricket_Coach class
@Bean 
public Coach cricket_Coach() {
return new Cricket_Coach(goodLuckFortune());
}
} 

In the AppConfig.java file, we have declared the bean classes, one is GoodLuckFortune, and the other is Cricket_Coach. The bean name and the method name (on which @Bean is defined) are must be the same as defined in the above code.

  • Create the main class

In this step, we are going the main class named App.java.

App.java

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class App 
{
public static void main( String[] args )
{
AnnotationConfigApplicationContext appcontext = new AnnotationConfigApplicationContext(AppConfig.class);
Coach newCoach = appcontext.getBean("cricket_Coach", Coach.class); 
System.out.println(newCoach.getDailyTraining());
System.out.println(newCoach.getFortuneService());
appcontext.close();
}
} 

Output

main class named App.java


Related Topics

Spring AOP – @AfterReturning Advice

Spring AOP – @AfterReturning Advice We have discussed earlier the types of advices available in Spring AOP. One of them is After Returning advice. It is an advice that executes after the normal execution...

3 minutes read.

Spring - Inversion of Control (IoC)

Spring - Inversion of Control (IoC) The Inversion of Control (IoC) is a process where the objects define their dependencies, that is, the dependencies of other objects with they are working. It is done...

4 minutes read.

Spring – Dependency Injection

Spring – Dependency Injection One of the main features of Inversion of Control (IoC) is Dependency Injection (DI). It is a process of supplying the dependencies from one object to others. The Spring-Core container...

8 minutes read.

Spring - Bean lifecycle using annotations

Spring - Bean lifecycle using annotations We have already discussed the bean lifecycle using an XML-based configuration. In the previous case, we have declared the init-method and destroy-method attributes in the bean-configuration file. Here, we...

2 minutes read.

Spring Autowiring

Spring Autowiring Autowiring is an essential feature of the Spring framework. The autowiring function is used for wiring beans automatically, without injecting the beans using <constructor-arg> and <property> elements in the bean-config file. It can...

3 minutes read.

Spring Autowiring – Setter Injection

Spring Autowiring – Setter Injection We have previously discussed an example of Setter Dependency Injection (SDI) using an XML file. In the previous case, we have defined the property name for the dependency...

2 minutes read.

Spring AOP - @AfterThrowing Advice

Spring AOP - @AfterThrowing Advice As we have discussed in our previous tutorial, Advice is one of the essential concepts of the Spring AOP. The after throwing advice is one of the types of...

3 minutes read.

Spring MVC Tutorial

Spring MVC is a web Model-View-Controller framework built on Servlet API. It is used to build web applications. The MVC framework works around central servlet called DispatcherServlet. It dispatches the requests to the...

4 minutes read.

Spring - Bean Scopes with Annotations

Spring - Bean Scopes with Annotations We have previously discussed the Spring bean scopes using an XML-based configuration. In the XML-based configuration, we have defined the beans and its scope inside the bean-config...

3 minutes read.

Spring MVC Regular Expression Validation

Example of Regular Expression Validation Here, we are going to create an example of regular expression validation. Following are the steps to create an example of a regular expression: Create a request page In this step, we are going...

13 minutes read.

Spring AOP – @Around Advice

Spring AOP – @Around Advice We have discussed in our previous tutorial, the concepts, and terminology of the Spring AOP. Around advice is one of the types of advices. It can be applied to...

4 minutes read.

Spring MVC Example

Spring MVC Example We have previously discussed the Spring Web MVC framework. In this tutorial, we are going to create a simple example of Spring Web MVC. Following are the steps used to create an example...

2 minutes read.

Spring MVC Form Checkbox

Spring MVC provides several form tags used to develop web applications (Usually JSP pages). One of them is a checkbox tag. The checkbox tag offers us to choose multiple options at the same...

12 minutes read.

Spring Framework Annotations

Spring Framework Annotations Spring framework provides a variety of annotations for a better approach. Using annotations, we can develop Spring applications (small & large scale) easily with less code. Following are the annotations...

6 minutes read.

Spring – Bean Lifecycle

Spring – Bean Lifecycle Spring bean is responsible for managing the lifecycle of beans created by the Spring container.  The lifecycle of the bean is easy to understand. When a bean is created, it requires...

4 minutes read.

Spring AOP – @After Advice

Spring AOP – @After Advice We have previously discussed the concepts of Spring AOP. One of the AOP concepts is Advice, which is further divided into various types. After (finally) is a type of advice...

3 minutes read.

Spring – Beans using @Bean

Spring – Beans using @Bean We have previously discussed an example of Spring configuration using an XML-based configuration. In the previous example, we have defined beans inside the bean-configuration file (applicationContext.xml). To make the...

3 minutes read.

Spring MVC Form – Radio Button

A variety of form tags are available in Spring MVC for developing web applications, <form:radiobutton> tag is one of them. The radio button tag is used to choose only one option among a...

10 minutes read.

Spring - Injecting values from Property Files

Spring - Injecting values from Property Files In this section, we will discuss how to inject values from a property file using the Java-based configuration. Here, we will use two annotations @Value and @PropertySource...

3 minutes read.

Spring Model Interface

Model Interface Model is an interface that defines a holder for model attributes. It is also used to transfer data between various parts of the Spring MVC application. The Model interface is available...

8 minutes read.