Table of contents
1.
Introduction
2.
Using CDI Beans in Instantiated Components
3.
Using Router Components
3.1.
Example
4.
Using Components Injected Into Templates
4.1.
Example 
4.2.
Example 
4.3.
Example
5.
Using a Custom UI
6.
Frequently Asked Questions
6.1.
What is Vaadin?
6.2.
What are CDI Beans?
6.3.
What happens when CDI Beans in Instantiated Components are Injected into Template?
6.4.
What happens while using Router Components in CDI Beans?
6.5.
Is it necessary to define Custom UI?
7.
Conclusion 
Last Updated: Mar 27, 2024
Easy

Using CDI Beans in Instantiated Components

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Before starting CDI Beans in Instantiated Components, Let's understand what is exactly CDI Beans. CDI Beans are classes that CDI can Automatically instantiate, manage and inject to meet the requirement of the Other objects. CDI can manage and inject almost any Java Class. 

Now that we know what CDI Bean is, let's Start with our main topic, which is Using CDI Beans in Instantiated Components. I'd like you to concentrate completely for a greater understanding.

CDI Beans

Using CDI Beans in Instantiated Components

Most objects instantiated by the framework become managed beans when using Vaadin CDI. To obtain references to beans, the framework uses the CDI BeanManager. This implies they are fully functional CDI contextual instances.

The add-on searches for CDI beans based on type (component class) and @Any.

If the type cannot be discovered as a CDI bean, for example, because it is ambiguous or lacks a no-arguments public function Object(), instantiation reverts to the normal Vaadin behavior, and the component is created as a regular Java object (POJO). Dependency injection occurs after instantiation, and injections continue to function, but other CDI capabilities do not. This is because the instantiated component is not a contextual instance.

Router Components

Using Router Components

When the add-on is used, all route targets, router layouts, and exception targets become managed beans. The components appear and operate the same as before, but CDI capabilities are now available.

Example

Using the @Inject annotation on a simple route-target 

@Route
public class MyView extends childMyView
{
    @Inject
    public MyView(Greeter greeter) 
    {
        add(new Span(greeter.sayHi()));
    }
}
You can also try this code with Online Java Compiler
Run Code

Using Components Injected Into Templates

When the @id annotation is used to inject components into template classes, they become managed beans.

Example 

The Deplabel component is injected into the CodingNinjas class using the @Id annotation.

Public class CodingNinjas extends Ninjas. 
{
    @Id
    private Deplabel label;
}
You can also try this code with Online Java Compiler
Run Code

Example 

Class Deplabel

@Dependent
@Tag("dependent-label")
public class Deplabel extends Label {
    @Inject
    private Greeter greeter;

    @PostConstruct
    private void init() {
        setText(greeter.sayHi());
    }
}
You can also try this code with Online Java Compiler
Run Code

Example

CodingNinjas.html Polymer template

import { html, LitElement } from 'lit';
class CodingNinjas extends LitElement {
    get render() {
        return html`
            <div>
                <dependent-label id="label"/>
            </div>`;
    }
}
customElements.define(CodingNinjas.is, CodingNinjas);
You can also try this code with Online Java Compiler
Run Code

 

Custom UI

Using a Custom UI

It is not essential to specify a custom UI subclass for your application, but you can, if necessary, by utilizing the matching servlet option.

Vaadin instantiates the custom UI subclass as a POJO (rather than a managed bean), but dependency injection is still allowed. In your overridden UI.init() function, use BeanManager; for example, 

BeanProvider.injectFields(this) (in DeltaSpike).

Frequently Asked Questions

What is Vaadin?

Vaadin is an open-source framework for deve;oping contemporary web applications. It contains a vast component library and an easy-to-use Java API. It is also compatible with Spring.

What are CDI Beans?

CDI Beans are classes that CDI can Automatically instantiate, manage and inject to meet the requirement of the Other objects. CDI can manage and inject almost any Java Class.

What happens when CDI Beans in Instantiated Components are Injected into Template?

When Components are injected into template classes using the @id annotation, they become managed beans.

What happens while using Router Components in CDI Beans?

All route targets, router layouts, and exception targets become managed beans when an add-on is used. The components seem and function normally, but CDI capabilities are now accessible. 

Is it necessary to define Custom UI?

It is not required to define a Custom UI subclass for your application, but you can, if necessary, by using the Corresponding servlet option.

Conclusion 

In this article, we learned about CDI Beans and their Usage in Instantiated Components.

After reading about CDI Beans, and their Usage in Instantiated Components, are you not feeling excited to read/explore more articles on the topic of Ruby? Don't worry; Coding Ninjas has you covered. To learn, see Java Vs. Ruby, Java AWT Basics, and Java Keylistener.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! 

Do upvote our blogs if you find them helpful and engaging!

Live masterclass