3.2.4.1. Creating a Bean

See Create Business Logic in CUBA guide to learn how to put business logic as a Spring bean.

To create a Spring bean, add the @org.springframework.stereotype.Component annotation to the Java class. For example:

package com.sample.sales.core;

import com.sample.sales.entity.Order;
import org.springframework.stereotype.Component;

@Component(OrderWorker.NAME)
public class OrderWorker {
    public static final String NAME = "sales_OrderWorker";

    public void calculateTotals(Order order) {
    }
}

It is recommended to assign a unique name to the bean in the {project_name}_{class_name} form and to define it in the NAME constant.

The @javax.annotation.ManagedBean can also be used for the bean definition, but it can cause problems when deploying the application into some application servers. Therefore we recommend to use only @Component annotation from Spring Framework.

The bean class should be placed inside the package tree with the root specified in the context:component-scan element of the spring.xml file. For the bean in the example above, the spring.xml file should contain the element:

<context:component-scan base-package="com.sample.sales"/>

which means that the search for annotated beans for this application block will be performed starting with the com.sample.sales package.

Spring beans can be created on any application tier.