JSR-330 & Spring
La JSR-330 spécifie le standard en matière d'annotations pour l'injection de dépendances::
- @Inject
- Google et SpringSource mène conjointement le lead
Spring implémente la spécification::
- @Inject couvre 80% des besoins
- Les autres besoins sont couverts par @Autowired
Spring supporte les annotations de la JSR-330.
@Named
public class PizzaService {
@Inject
private PizzaRepository pizzaRepository;
public String findAll() {
return pizzaRepository.findAll();
}
}
Annotations Spring vs JSR-330
| Spring | JSR-330 | Commentaires |
|---|---|---|
| @Autowired | @Inject | @Inject ne prend pas d’attributs. |
| @Component | @Named | Spring prend en compte cette annotation lors du scan. |
| @Scope | @Scope | Portée |
| @Scope("singleton") | @Singleton | En JSR-330, le scope par défaut correspond au « prototype » de Spring. |
| @Qualifier | @Named | Nommer |
| @Value | N/A | Injection de valeur |
| @Required | N/A | Requis |
| @Lazy | N/A | Chargement tardif |