Tutorials  /  JavaScript

Spring Bean Annotation

Ccentron Redaktion · February 2024 ·3 min read ·JavaScript, Tutorial

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.

Spring Bean Example

Let’s say we have a simple class as below.

Go
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.

Go
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.

Code
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.scan("com.journaldev.spring");
context.refresh();
        
//Getting Bean by Class
MyDAOBean myDAOBean = context.getBean(MyDAOBean.class);
VM

Matching infrastructure at centron

Node applications need somewhere to run: ccloud³ VMs with full root access from €3.12 per month – or as a managed server if you would rather not run it yourself. Rent a cloud server →

Spring Bean Name

We can specify the @Bean name and use it to get them from spring context. Let’s say we have MyFileSystemBean class defined as:

Go
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:

Code
@Bean(name= {"getMyFileSystemBean","MyFileSystemBean"})
public MyFileSystemBean getMyFileSystemBean() {
    return new MyFileSystemBean();
}

We can get this bean from context by using the bean name.

Code
MyFileSystemBean myFileSystemBean = (MyFileSystemBean) context.getBean("getMyFileSystemBean");

MyFileSystemBean myFileSystemBean1 = (MyFileSystemBean) context.getBean("MyFileSystemBean");

Spring @Bean initMethod and destroyMethod

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.

Code
@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.

Summary

Spring @Bean annotation is widely used in annotation-driven spring applications.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie JavaScript
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren