Repository navigation
Expand file tree
/
Copy pathExpQualifier.java
More file actions
90 lines (76 loc) · 3.55 KB
/
Copy pathExpQualifier.java
File metadata and controls
90 lines (76 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import java.util.HashMap;
import java.util.Map;
// Simula @Primary y @Qualifier para resolver ambigüedad cuando hay varios beans del mismo tipo.
// Spring usa estos metadatos al inyectar; aquí los llevamos con anotaciones en comentarios.
public class ExpQualifier {
interface Notificador {
void enviar(String mensaje);
}
// @Component("email")
static class NotificadorEmail implements Notificador {
@Override
public void enviar(String mensaje) {
System.out.println("[EMAIL] " + mensaje);
}
}
// @Component("sms")
// @Primary ← gana cuando no hay @Qualifier explícito
static class NotificadorSMS implements Notificador {
@Override
public void enviar(String mensaje) {
System.out.println("[SMS] " + mensaje);
}
}
// Registro de beans con soporte para @Primary y @Qualifier
static class BeanRegistry {
private final Map<String, Notificador> beans = new HashMap<>();
private String primaryName;
void registrar(String nombre, Notificador bean) {
beans.put(nombre, bean);
}
// Marca cuál bean gana cuando se inyecta por tipo sin @Qualifier
void setPrimary(String nombre) {
this.primaryName = nombre;
}
// Inyección por tipo sin calificador → usa @Primary
Notificador getByType() {
if (primaryName == null) {
if (beans.size() == 1) return beans.values().iterator().next();
// Sin @Primary y más de un candidato → ambigüedad (Spring lanza NoUniqueBeanDefinitionException)
throw new IllegalStateException(
"Ambigüedad: " + beans.size() + " beans de tipo Notificador. Usa @Qualifier o @Primary.");
}
return beans.get(primaryName);
}
// Inyección con @Qualifier("nombre") → selección explícita
Notificador getByQualifier(String nombre) {
Notificador bean = beans.get(nombre);
if (bean == null) throw new IllegalStateException("No hay bean con qualifier: " + nombre);
return bean;
}
}
public static void main(String[] args) {
BeanRegistry registry = new BeanRegistry();
registry.registrar("email", new NotificadorEmail());
registry.registrar("sms", new NotificadorSMS());
System.out.println("=== Escenario 1: sin @Qualifier ni @Primary → ambigüedad ===");
try {
Notificador n = registry.getByType();
n.enviar("Hola");
} catch (IllegalStateException e) {
System.out.println("Error esperado → " + e.getMessage());
}
System.out.println("\n=== Escenario 2: con @Primary en SMS → SMS gana por defecto ===");
registry.setPrimary("sms");
Notificador porPrimary = registry.getByType();
porPrimary.enviar("Notificación por defecto");
System.out.println("\n=== Escenario 3: @Qualifier(\"email\") → email seleccionado explícitamente ===");
// @Qualifier tiene prioridad sobre @Primary
Notificador porQualifier = registry.getByQualifier("email");
porQualifier.enviar("Notificación explícita por email");
System.out.println("\n=== Resumen ===");
System.out.println("Sin @Primary, sin @Qualifier → NoUniqueBeanDefinitionException");
System.out.println("@Primary sms → SMS gana cuando no hay @Qualifier");
System.out.println("@Qualifier(\"email\") → Email seleccionado, ignora @Primary");
}
}