@Service

@Service() marks a class as a bean that holds business logic.

Why

It does exactly what @Component does. The different name tells the reader which layer the class belongs to: the rules of the application, between the data access (@Repository) and the presentation (@Controller).

The name can also be used where annotations are matched: in scan filters and in AOP pointcuts (@within(Service)).

Use it for classes that coordinate work: place an order, register a user, compute a price. Nothing breaks if you use @Component() instead.

Example

import 'package:spring_core_dart/spring_core_dart.dart';

@Repository()
class OrderRepository {
  final _orders = <int, double>{};
  void save(int id, double total) => _orders[id] = total;
}

// The repository bean is injected through the constructor.
@Service()
class OrderService {
  OrderService(this.repository);

  final OrderRepository repository;

  void place(int id, double total) {
    if (total <= 0) {
      throw ArgumentError.value(total, 'total', 'must be positive');
    }
    repository.save(id, total);
  }
}

Parameters

Name Type Default Meaning
name (positional) String? null The bean name. null gives the class name in lowerCamelCase.

In Spring (Java)

@Service
class OrderService {
  OrderService(OrderRepository repository) { ... }
}

The meaning is the same. Service is a subclass of Component here, which plays the role of Spring's meta-annotation.

See also