
# @Service

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

## Why

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

The name can also be used where annotations are matched: in [scan filters](component-scan.md) 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

```dart
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)

```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

- [@Component](component.md)
- [@Repository](repository.md), [@Controller](controller.md)
- [AOP](../aop/index.md)
